Printing QWidget window using python
-
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 :)