Printing QWidget window using python
-
No, I do not want to print only just labels. I need to print whole window. Please see following image.
http://theweblogics.com/screen.png
On click of print button, I need to get this window by printer.
-
@khakhil
im confused.
so what is wrong with
http://stackoverflow.com/questions/10705712/screenshot-of-a-window-using-python
take image of whole thing and print that image?? -
@khakhil
HI
you will need a way to select real printer
then load the image from file
and draw onto a painter connected to the selected printer.try to play around with this sample
http://stackoverflow.com/questions/8193920/print-a-text-through-a-printer-using-pyqt4
its with preview and all so maybe over the tophere is c++ function that grabs screenshot and print it.
(thx to @Ni-Sumi)void MainWindow::printPage() { QPixmap pix = QPixmap::grabWindow(QApplication::desktop()->winId()); QPrinter printer(QPrinter::HighResolution); printer.setOrientation(QPrinter::Landscape); QPainter painter; painter.begin(&printer); double xscale = printer.pageRect().width() / double(pix.width()); double yscale = printer.pageRect().height() / double(pix.height()); double scale = qMin(xscale, yscale); painter.translate(printer.paperRect().x() + printer.pageRect().width() / 2, printer.paperRect().y() + printer.pageRect().height() / 2); painter.scale(scale, scale); painter.translate(-width() / 2, -height() / 2); painter.drawPixmap(0, 0, pix); painter.end(); }
I assume u can read enough to translate to py. :)
-
The link http://stackoverflow.com/questions/8193920/print-a-text-through-a-printer-using-pyqt4
is very helpful. I solved 90% of my problem.
now, the problem is that as i select the image to print, the printer not getting the image.
instead of image it takes "ÿØÿà" and prints it.what i do?
-
if info.completeSuffix() == 'html': self.editor.setHtml(text) else: self.editor.setPlainText(text)
is there any way like this for .jpg or .tif format ???
for the same issue i am also trying the following as @mrjj suggest
class Window(QtGui.QWidget): def __init__(self): super(Window, self).__init__() self._new_window = None printer = QtGui.QPrinter() Pixmap = QtGui.QPixmap() pix = QPixmap.grabWindow(QApplication.desktop().winId()) printer.setOrientation(printer.Landscape) Painter= QtGui.QPainter() Painter.begin(printer) xscale = printer.pageRect().width() / (pix.width()) yscale = printer.pageRect().height() / (pix.height()) scale = qmin(xscale, yscale) Painter.translate(printer.paperRect().x() + printer.pageRect().width() / 2, printer.paperRect().y() + printer.pageRect().height() / 2); Painter.scale(scale, scale) Painter.translate(-width() / 2, -height() / 2) Painter.drawPixmap(0, 0, pix) Painter.end()
but not succeed.
-
so it just print "ÿØÿà on piece of paper?
or? -
@khakhil said:
def init(self):
can you try to move the code to a button? ( to a clicked slot)
I assume
def init(self):
is the constructor?
Might not be the best spot as object might not be fully shown or
the (QApplication.desktop().winId()) dont return what we think.Could u also add a Painter.drawText(200,200, "hello") and see if that comes.?
-
@mrjj
hey thanks..
this code is working properly.printer = QtGui.QPrinter() Pixmap = QtGui.QPixmap() pix= QPixmap.grabWindow(loginwindow.winId()) Painter= QtGui.QPainter() Painter.begin(printer) Painter.drawPixmap(50, 490, pix) Painter.end()
It saves images every time i ran the code .
what should i do if i don't want to save it but directly print it???? -
hi
it should print the image.
code looks ok.could u try display the image in a label?
self.label.setPixmap(pix)to see what u grab
-
@mrjj
thank you so much for help !!
problem solved :) :)printer = QtGui.QPrinter() Pixmap = QtGui.QPixmap() pix= QPixmap.grabWindow(loginwindow.winId()) Painter= QtGui.QPainter() Painter.begin(printer) Painter.drawPixmap(50, 490, pix) Painter.end()
same code works for grab screenshot and print it.
thanks @mrjj
-
@khakhil
super! :)
as a little note.
If printer has high DPI, image might become really small.
if that happens u can use the scale function of the pixmap to make it bigger or
use the Painter.translate() as c++ sample showed :)