Reputation: 7078
I am trying to get a PrinterDialog for users to choose all the options for printing.
When I try this with PySide, the returned printer is always the same (default I guess), no matter what changes the user makes in the dialog. The same happens when I pass it a printer initially.
I have come up with this small self-contained example to see what I am talking about.
USE_PYQT4 = True
if USE_PYQT4:
from PyQt4 import QtGui, QtCore
else:
from PySide import QtGui, QtCore
import sys
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.printBtn = QtGui.QPushButton("Print Preview")
self.printBtn.clicked.connect(self.onPrint)
# Will store the QPrinter instance
self.printer = None
# Will store the QPrintDialog, because it
# might get garbage-collected, and the QPrinter with it
self.pdialog = None
layout = QtGui.QVBoxLayout()
layout.addWidget(self.printBtn)
self.setLayout(layout)
def printState(self):
print '*'*20
if self.printer is None:
print '-- no printer -- '
else:
print 'printer name', self.printer.printerName()
print 'page size', self.printer.pageSize()
print 'paper size', self.printer.paperSize()
print
print
def askPrinter(self):
# Either like this:
#
# self.printer = QtGui.QPrinter()
# self.pdialog = dialog = QtGui.QPrintDialog(self.printer)
# Or like that:
#
self.pdialog = dialog = QtGui.QPrintDialog()
dialog.setOption(QtGui.QAbstractPrintDialog.PrintCollateCopies, True)
dialog.setOption(QtGui.QAbstractPrintDialog.PrintCurrentPage, True)
dialog.setOption(QtGui.QAbstractPrintDialog.PrintPageRange, True)
dialog.setOption(QtGui.QAbstractPrintDialog.PrintSelection, True)
dialog.setOption(QtGui.QAbstractPrintDialog.PrintShowPageSize, True)
dialog.setOption(QtGui.QAbstractPrintDialog.PrintToFile, True)
print 'before PrintDialog'
self.printState()
if dialog.exec_() != QtGui.QDialog.Accepted:
return False
# And that, if not set before
self.printer = dialog.printer()
print 'after PrintDialog'
self.printState()
return True
def onPrint(self):
if not self.askPrinter():
print 'oops'
return
self.preview()
def preview(self):
dialog = QtGui.QPrintPreviewDialog(self.printer)
dialog.paintRequested.connect(self.handlePaintRequest)
dialog.exec_()
def handlePaintRequest(self, printer):
doc = QtGui.QTextDocument()
cursor = QtGui.QTextCursor(doc)
cursor.insertText("""
SOME TEXT
""")
cursor.insertText("-"*20)
data = [('Line {}'.format(i), '{} times'.format(i*2)) for i in xrange(20)]
table = cursor.insertTable(len(data), max(len(d) for d in data))
for row in range(table.rows()):
for column in range(table.columns()):
cursor.insertText(unicode(data[row][column]))
cursor.movePosition(QtGui.QTextCursor.NextCell)
cursor.movePosition(QtGui.QTextCursor.NextBlock)
cursor.insertText("-"*20)
cursor.insertText("""
And that's it
""")
doc.print_(printer)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
app.window = Window()
app.window.show()
sys.exit(app.exec_())
Note that when using PyQt4, the issue is different:
In PySide, if the page size is not set manually, it takes A4
(maybe it's the default for the first printer). Also, the options set from setOption
seem to be ignored...
In PyQt4, the page size is set to 0
, whatever that is, unless the page size dialog is opened. When the page size is 0
, it's definitely different than when the page size dialog is opened. Also, the page margins are set to 0 here. However, the options are respected and it takes the correct default values (set through the CUPS admin page).
What is weird is that I checked the Kate print dialog, and it's completely correct. Kate is a Qt application in C++. I couldn't test that myself on C++ though.
I'm on Python 2.7.3 with PySide 1.1.0 and PyQt4 4.10.1 on Linux.
Thanks in advance for any input.
Update:
I tried the example provided with PySide, and it has the same issues; so most probably I'm not doing anything wrong.
Also, I checked bug reports. There are lots of them, none of which lead to anywhere. Some are fixed for Qt5, some ignored.
Upvotes: 3
Views: 1447
Reputation: 159
I tried your piece of code. It works perfectly and returns the correct printer and page setup. on win xp, PySide 1.1.2 and python 3.3.2. it may be a problem with your qt libraries or some os mis-configurations.
Upvotes: 0