<bdo id='vSnqE'></bdo><ul id='vSnqE'></ul>
<tfoot id='vSnqE'></tfoot>
    1. <legend id='vSnqE'><style id='vSnqE'><dir id='vSnqE'><q id='vSnqE'></q></dir></style></legend>

      <small id='vSnqE'></small><noframes id='vSnqE'>

    2. <i id='vSnqE'><tr id='vSnqE'><dt id='vSnqE'><q id='vSnqE'><span id='vSnqE'><b id='vSnqE'><form id='vSnqE'><ins id='vSnqE'></ins><ul id='vSnqE'></ul><sub id='vSnqE'></sub></form><legend id='vSnqE'></legend><bdo id='vSnqE'><pre id='vSnqE'><center id='vSnqE'></center></pre></bdo></b><th id='vSnqE'></th></span></q></dt></tr></i><div id='vSnqE'><tfoot id='vSnqE'></tfoot><dl id='vSnqE'><fieldset id='vSnqE'></fieldset></dl></div>

        如何在 PyQt5 中创建 QWebEngineView 的打印预览?

        时间:2023-08-05
          1. <legend id='otrzm'><style id='otrzm'><dir id='otrzm'><q id='otrzm'></q></dir></style></legend>

              <small id='otrzm'></small><noframes id='otrzm'>

              <i id='otrzm'><tr id='otrzm'><dt id='otrzm'><q id='otrzm'><span id='otrzm'><b id='otrzm'><form id='otrzm'><ins id='otrzm'></ins><ul id='otrzm'></ul><sub id='otrzm'></sub></form><legend id='otrzm'></legend><bdo id='otrzm'><pre id='otrzm'><center id='otrzm'></center></pre></bdo></b><th id='otrzm'></th></span></q></dt></tr></i><div id='otrzm'><tfoot id='otrzm'></tfoot><dl id='otrzm'><fieldset id='otrzm'></fieldset></dl></div>

                  <tbody id='otrzm'></tbody>
              • <tfoot id='otrzm'></tfoot>
                  <bdo id='otrzm'></bdo><ul id='otrzm'></ul>
                • 本文介绍了如何在 PyQt5 中创建 QWebEngineView 的打印预览?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试创建 QWebEngineView 的打印预览,但无法正常工作.

                  I'm trying to create a print preview of a QWebEngineView but I can't get it to work.

                  这是我的代码:

                  ...
                  self.view = QWebEngineView()
                  ...
                  
                  def handle_preview(self):
                      dialog = QPrintPreviewDialog()
                      dialog.paintRequested.connect(self.view.print_)
                      dialog.exec_()
                  

                  代码给了我这个错误:

                  AttributeError: 'QWebEngineView' 对象没有属性 'print_'

                  AttributeError: 'QWebEngineView' object has no attribute 'print_'

                  当我使用 QTextEdit 时,代码运行良好.但这不是我想要的.我想使用 QWebEngineView.

                  The code works perfectly when I use QTextEdit. But that's not what I want. I want to use QWebEngineView.

                  推荐答案

                  基于官方示例:WebEngine Widgets PrintMe Example 您可以使用以下代码实现预览.

                  Based on the official example: WebEngine Widgets PrintMe Example you can implement the preview using the following code.

                  from PyQt5.QtCore import (QCoreApplication, QEventLoop, QObject, QPointF, Qt,
                                         QUrl, pyqtSlot)
                  from PyQt5.QtGui import QKeySequence, QPainter
                  from PyQt5.QtPrintSupport import QPrintDialog, QPrinter, QPrintPreviewDialog
                  from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineView
                  from PyQt5.QtWidgets import QApplication, QDialog, QLabel, QProgressBar, QProgressDialog, QShortcut
                  
                  
                  class PrintHandler(QObject):
                      def __init__(self, parent = None):
                          super().__init__(parent)
                          self.m_page = None
                          self.m_inPrintPreview = False
                  
                      def setPage(self, page):
                          assert not self.m_page
                          self.m_page = page
                          self.m_page.printRequested.connect(self.printPreview)
                  
                      @pyqtSlot()
                      def print(self):
                          printer = QPrinter(QPrinter.HighResolution)
                          dialog = QPrintDialog(printer, self.m_page.view())
                          if dialog.exec_() != QDialog.Accepted:
                              return
                          self.printDocument(printer)
                  
                      @pyqtSlot()
                      def printPreview(self):
                          if not self.m_page:
                              return
                          if self.m_inPrintPreview:
                              return
                          self.m_inPrintPreview = True
                          printer = QPrinter()
                          preview = QPrintPreviewDialog(printer, self.m_page.view())
                          preview.paintRequested.connect(self.printDocument)
                          preview.exec()
                          self.m_inPrintPreview = False
                  
                      @pyqtSlot(QPrinter)
                      def printDocument(self, printer):
                          loop = QEventLoop()
                          result = False
                  
                          def printPreview(success):
                              nonlocal result
                              result = success
                              loop.quit()
                          progressbar = QProgressDialog(self.m_page.view())
                          progressbar.findChild(QProgressBar).setTextVisible(False)
                          progressbar.setLabelText("Wait please...")
                          progressbar.setRange(0, 0)
                          progressbar.show()
                          progressbar.canceled.connect(loop.quit)
                          self.m_page.print(printer, printPreview)
                          loop.exec_()
                          progressbar.close()
                          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()
                  
                  
                  def main():
                      import sys
                  
                      QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
                      app = QApplication(sys.argv)
                      app.setApplicationName("Previewer")
                  
                      view = QWebEngineView()
                      view.setUrl(QUrl("https://stackoverflow.com/questions/59438021"))
                      view.resize(1024, 750)
                      view.show()
                  
                      handler = PrintHandler()
                      handler.setPage(view.page())
                  
                      printPreviewShortCut = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_P), view)
                      printShortCut = QShortcut(QKeySequence(Qt.CTRL + Qt.SHIFT + Qt.Key_P), view)
                  
                      printPreviewShortCut.activated.connect(handler.printPreview)
                      printShortCut.activated.connect(handler.print)
                  
                      sys.exit(app.exec_())
                  
                  
                  if __name__ == "__main__":
                      main()
                  

                  注意:对于 PySide2,您只需将 pyqtSlot 更改为 Slot.

                  Note: For PySide2 you only have to change pyqtSlot to Slot.

                  这篇关于如何在 PyQt5 中创建 QWebEngineView 的打印预览?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 html 中为 PyQt5 网络引擎使用本地文件 下一篇:AttributeError:QDialog 对象没有属性 setCentralWidget

                  相关文章

                  最新文章

                    <bdo id='3m0Va'></bdo><ul id='3m0Va'></ul>
                  <i id='3m0Va'><tr id='3m0Va'><dt id='3m0Va'><q id='3m0Va'><span id='3m0Va'><b id='3m0Va'><form id='3m0Va'><ins id='3m0Va'></ins><ul id='3m0Va'></ul><sub id='3m0Va'></sub></form><legend id='3m0Va'></legend><bdo id='3m0Va'><pre id='3m0Va'><center id='3m0Va'></center></pre></bdo></b><th id='3m0Va'></th></span></q></dt></tr></i><div id='3m0Va'><tfoot id='3m0Va'></tfoot><dl id='3m0Va'><fieldset id='3m0Va'></fieldset></dl></div>
                  1. <legend id='3m0Va'><style id='3m0Va'><dir id='3m0Va'><q id='3m0Va'></q></dir></style></legend>
                    <tfoot id='3m0Va'></tfoot>

                    <small id='3m0Va'></small><noframes id='3m0Va'>