我正在尝试使用QtPainter
预览我的整个QMainWindow
(所有内容),以便我可以从预览菜单中打印它。我所有的尝试都失败了。下面是示例代码。
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5 import *
from PyQt5 import uic, QtPrintSupport
from PyQt5.QtPrintSupport import QPrintDialog, QPrinter
from PyQt5 import QtCore, QtWidgets, QtPrintSupport, QtGui
import sys, os
class mainwindowUI(QMainWindow):
resized = QtCore.pyqtSignal()
def __init__(self, parent = None):
super(mainwindowUI, self).__init__(parent)
self.text = QLineEdit('Hello World', self)
self.print_()
self.show()
def print_(self):
dialog = QtPrintSupport.QPrintPreviewDialog() # PyQt5
# dialog = QPrintPreviewDialog()
dialog.paintRequested.connect(self.handlePaintRequest)
dialog.exec_()
def handlePaintRequest(self, printer):
painter = QtGui.QPainter()
# Start painter
painter.begin(printer)
# Grab a widget you want to print
screen = self.grab()
# Draw grabbed pixmap
painter.drawPixmap(10, 10, screen)
# End painting
painter.end()
self.graphicsView = QGraphicsView(painter)
self.graphicsView.render(QPainter(printer))
self.viewer.render(QPainter(printer))
def handlePrint(self):
dialog = QtPrintSupport.QPrintDialog()
if dialog.exec_() == QDialog.Accepted:
self.editor.document().print_(dialog.printer())
if __name__ == '__main__':
app = QApplication(sys.argv)
window = mainwindowUI()
sys.exit(app.exec_())
我想要什么,QtPainter
预览窗口显示我的QMainWindow
和里面的一切。它似乎QtPainter
需要一个QGraphicsView
所以我的主窗口必须以某种方式转换为那个......(我.ui
在我的最终项目中使用我在 QtCreator 中设计的一个文件,以防万一这让一些事情变得更加困难)