在 raspbian/linux 上打印错误 Pyqt Qwebkit

Print error Pyqt Qwebkit on raspbian/ linux

Traceback (most recent call last):
  File "pos.py", line 57, in printDocument
    self.page.print(printer, printPreview)
    AttributeError: 'QWebPage' object has no attribute 'print'
    Aborted

我正在使用 raspbian os。并尝试使用 QWebPage 从 QWebkit 打印我将在下面共享代码块。请分享您对如何克服此错误的想法。

class PrintHandler(QObject):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.m_page = None
        self.m_inPrintPreview = False

    @property
    def page(self):
        return self.m_page

    @page.setter
    def page(self, page):
        if isinstance(page, QWebPage):
            self.m_page = page
            self.page.printRequested.connect(self.printPreview)
        else:
            raise TypeError("page must be a QWebEnginePage")

    @pyqtSlot()
    def print(self):
        printer = QPrinter()
        dialog = QPrintDialog(printer, self.page.view())
        if dialog.exec_() != QDialog.Accepted:
            return
        self.printDocument(printer)

    @pyqtSlot()
    def printPreview(self):
        if self.page is None:
            return
        if self.m_inPrintPreview:
            return
        self.m_inPrintPreview = True
        printer = QPrinter()
        preview = QPrintPreviewDialog(printer, self.page.view())
        preview.paintRequested.connect(self.printDocument)
        preview.exec_()
        self.m_inPrintPreview = False

    @pyqtSlot(QPrinter)
    def printDocument(self, printer):
        result = False
        loop = QEventLoop()

        def printPreview(sucess):
            nonlocal result
            result = sucess
            loop.quit()
        self.page.print(printer, printPreview)
        loop.exec_()
        if not result:
            painter = QPainter()
            if painter.begin(printer):
                font = painter.font()
                font.setPixelSize(20)
                painter.setFont(font)
                painter.drawText(QPointF(10, 25), "Could not generate print preview.")
                painter.end()


class Mainwindow(QMainWindow):
    def __init__(self,*args,**kwargs):
        super(Mainwindow,self).__init__(*args,**kwargs)
    
        self.web = QWebView()
        self.web.loadProgress.connect(self.print_percent)
        self.web.loadFinished.connect(self.print_load_finished)
        url="https://www.google.com"
        self.web.load(QUrl(url))
        
        self.setCentralWidget(self.web)
        
        menu = QToolBar("Toolbar")
        self.addToolBar(menu)
        menu.setIconSize(QSize(40,40))
        
        Print = QAction(QIcon(os.path.join('icons','print.png')),"Print",self)
        Print.setStatusTip("Refresh the page")
        menu.addAction(Print)
        handler = PrintHandler(self)
        handler.page = self.web.page()
        Print.triggered.connect(handler.printPreview)

代码在 windows 中使用 QWebEngine 视图,因为我找不到如何在 raspbian 中适应 QWebEngine 我迁移到 QWebKit。 almost 一切正常。

但是打印时出现错误。

QtWebEngine 与QtWebkit 不兼容,所以不要指望模拟组件有相同的方法,另外如果你想将一种技术移植到另一种技术,建议使用两种技术的文档,从而避免盲目翻译。

考虑到上述情况,如果修改了文档,发现QtWebkit的QWebPage没有打印方法,所以你将不得不使用QWebView。

另一个区别是 QtWebkit 中的打印是同步的,而 QtWebEngine 是异步的,因此不需要 QEventLoop。

class PrintHandler(QObject):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.m_page = None
        self.m_inPrintPreview = False

    @property
    def page(self):
        return self.m_page

    @page.setter
    def page(self, page):
        if isinstance(page, QWebPage):
            self.m_page = page
            self.page.printRequested.connect(self.printPreview)
        else:
            raise TypeError("page must be a QWebEnginePage")

    @pyqtSlot()
    def print(self):
        printer = QPrinter()
        dialog = QPrintDialog(printer, self.page.view())
        if dialog.exec_() != QDialog.Accepted:
            return
        self.printDocument(printer)

    @pyqtSlot()
    def printPreview(self):
        if self.page is None:
            return
        if self.m_inPrintPreview:
            return
        self.m_inPrintPreview = True
        printer = QPrinter()
        preview = QPrintPreviewDialog(printer, self.page.view())
        preview.paintRequested.connect(self.printDocument)
        preview.exec_()
        self.m_inPrintPreview = False

    @pyqtSlot(QPrinter)
    def printDocument(self, printer):
        self.page.view().print_(printer)