Printing QWidget window using python
-
hey....
I want to print window. print command works well but return a blank page.
i have tried following -def printer(self): self.handlePrint() def handlePrint(self): dialog = QtGui.QPrintDialog() if dialog.exec_() == QtGui.QDialog.Accepted: self.handlePaintRequest(dialog.printer()) def handlePaintRequest(self, printer): db = MySQLdb.connect("localhost","root","","kailash") db_cursor = db.cursor() try: a="SELECT * FROM customer_list WHERE id = '%s'" % ( self.receipt_id) db_cursor.execute(a) result = db_cursor.fetchall() for row in result: date = row[1] name = row[2] envoice_no =row [3] weight = row[4] sample = row[5] purity = row[6] self.label_23.setText(date) self.label_18.setText(name) self.label_20.setText(envoice_no) self.label_22.setText(weight) self.label_19.setText(sample) self.label_21.setText(purity) except Exception, e: print e db.close() document = QtGui.QTextDocument() cursor = QtGui.QTextCursor(document) #cursor.insertText('hi') #cursor.insertBlock(self.window) cursor.document() document.print_(printer)
if I using cursor.insertText('hi') then printer returns a page with hi . but not return data fetching from database.
-
@khakhil said:
Hi
have you tested with a fixed select so u know it 100% works"SELECT * FROM customer_list WHERE id =666"
to see if it does enter the
" for row in result:" part.if yes, then next question is
label_23 is a Label?Im asking as later you print the document
document.print_(printer)
Those Labels in not part of that and hence dont print.instead of labels, do you mean
self.label_23.setText(date) -> cursor.insertText(date)so data from DB is printed as a document ?
Or do you mean to take a screenshots of the labels and sort of print that?
http://stackoverflow.com/questions/10705712/screenshot-of-a-window-using-pythonps. im python noob
-
@khakhil
but if u dont want screen shot of the labels (and print that),
how do you mean to print them?
a Label is just some text, so you might as well just print it yourself?Currently you are only printing the document. so unless you insert into that, its
not printed.in c++ you can draw text to printer like this
QPrinter printer(QPrinter::HighResolution);
QPainter painter;
painter.begin(&printer);
painter.drawText(xx)
...
http://doc.qt.io/qt-5/printing.htmlis it what you want?
a Label cannot just print it self
but you can ask it to draw to the painter
with the render function.
http://doc.qt.io/qt-5/qwidget.html#render -
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 :)