0

我想使用 QtHelp 和 QHelpEngine 制作帮助文件。我目前有一些代码,但我不确定我的错误,而且我认为文档有点模糊。尽管如此,我目前面临的问题是连接信号和插槽之一。我目前得到的错误是AttributeError: 'builtin_function_or_method' object has no attribute 'linkActivated'不确定原因,因为文档指出 contentWidget 确实有一个名为linkActivated. 并且这里发生错误textViewer = QtWidgets.QTextBrowser(helpEngine)

参考

from PyQt5 import QtCore, QtGui, QtWidgets, QtHelp
import os

class Ui_HelpSetupClass(QtWidgets.QDockWidget):

    def __init__(self):
        super().__init__()
        self.setupUi(self)
    
    def setupUi(self, HelpSetupClass):
        qUrl = "qthelp://ut.tool.help/tool/index.html"
        HELP_DIR = os.getcwd()
        HELP_PATH = HELP_DIR + "\\" + "Help" + "\\" + "help_file.qhc"
        helpEngine = QtHelp.QHelpEngine(HELP_PATH)
        helpEngine.setupData()

        tableWidget = QtWidgets.QTabWidget()
        tableWidget.setMaximumWidth(200)
        tableWidget.addTab(helpEngine.contentWidget(), "Contents")
        tableWidget.addTab(helpEngine.indexWidget(), "Index")

        textViewer = QtWidgets.QTextBrowser(helpEngine)
        textViewer.setSource(qUrl)

        helpEngine.contentWidget.linkActivated(qUrl ).connect(textViewer.setSource(qUrl))
        helpEngine.indexWidget.documentActivated(qUrl ).connect(textViewer.setSource(qUrl))
        
        horizSplitter = QtWidgets.QSplitter(QtCore.Qt.Horizontal)
        horizSplitter.insertWidget(0, tableWidget)
        horizSplitter.insertWidget(1, textViewer)
        horizSplitter.hide()

        helpWindow = QtWidgets.QDockWidget("Help", self)
        helpWindow.setWidget(horizSplitter)
        helpWindow.hide()
        QtWidgets.QMainWindow.addDockWidget(QtCore.Qt.BottomDockWidgetArea, helpWindow)


    
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    ui = Ui_HelpSetupClass()
    ui.show()
    sys.exit(app.exec_())
4

1 回答 1

1

在您将 C++ 代码翻译为 Python 时,它显示了许多错误,因此我将避免指出它,我只会显示正确的翻译:

import os
from PyQt5 import QtCore, QtGui, QtWidgets, QtHelp


CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))


class HelpBrowser(QtWidgets.QTextBrowser):
    def __init__(self, helpEngine, parent=None):
        super().__init__(parent)
        self.helpEngine = helpEngine

    def loadResource(self, _type, name):
        if name.scheme() == "qthelp":
            return self.helpEngine.fileData(name)
        else:
            return super().loadResource(_type, name)


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.scene = QtWidgets.QGraphicsScene()
        self.scene.setSceneRect(0, 0, 400, 200)
        self.view = QtWidgets.QGraphicsView(self.scene)
        self.view.setDragMode(QtWidgets.QGraphicsView.RubberBandDrag)
        self.view.setRenderHints(QtGui.QPainter.Antialiasing)
        self.setCentralWidget(self.view)

        self.createHelpWindow()
        self.createActions()
        self.createMenus()
        self.createConnections()

        self.setWindowTitle(self.tr("QGraphicsScene Help Example"))
        self.resize(640, 480)

    def createHelpWindow(self):
        self.helpEngine = QtHelp.QHelpEngine(
            os.path.join(CURRENT_DIR, "documentation", "qgraphicshelpexample.qhc")
        )
        self.helpEngine.setupData()

        tWidget = QtWidgets.QTabWidget()
        tWidget.setMaximumWidth(200)
        tWidget.addTab(self.helpEngine.contentWidget(), "Contents")
        tWidget.addTab(self.helpEngine.indexWidget(), "Index")

        textViewer = HelpBrowser(self.helpEngine)
        textViewer.setSource(
            QtCore.QUrl("qthelp://walletfox.qt.helpexample/doc/index.html")
        )

        self.helpEngine.setUsesFilterEngine(True)
        self.helpEngine.contentWidget().linkActivated.connect(textViewer.setSource)
        self.helpEngine.indexWidget().linkActivated.connect(textViewer.setSource)

        horizSplitter = QtWidgets.QSplitter(QtCore.Qt.Horizontal)
        horizSplitter.insertWidget(0, tWidget)
        horizSplitter.insertWidget(1, textViewer)
        horizSplitter.hide()

        self.helpWindow = QtWidgets.QDockWidget(self.tr("Help"), self)
        self.helpWindow.setWidget(horizSplitter)
        self.helpWindow.hide()
        self.addDockWidget(QtCore.Qt.BottomDockWidgetArea, self.helpWindow)

    def createActions(self):

        self.insertEllipseAction = QtWidgets.QAction(self.tr("Insert &Ellipse"), self)
        self.insertEllipseAction.setIcon(QtGui.QIcon(":/icons/ellipse.png"))

        self.insertRectangleAction = QtWidgets.QAction(
            self.tr("Insert &Rectangle"), self
        )
        self.insertRectangleAction.setIcon(QtGui.QIcon(":/icons/rectangle.png"))

        self.helpAction = QtWidgets.QAction(self.tr("Help Contents..."), self)
        self.helpAction.setShortcut(QtGui.QKeySequence.HelpContents)

        self.aboutAction = QtWidgets.QAction(self.tr("&About"), self)

    def createMenus(self):
        self.itemMenu = QtWidgets.QMenu(self.tr("&Item"), self)
        self.itemMenu.addAction(self.insertEllipseAction)
        self.itemMenu.addAction(self.insertRectangleAction)

        self.helpMenu = QtWidgets.QMenu(self.tr("&Help"), self)
        self.helpMenu.addAction(self.helpAction)
        self.helpMenu.addAction(self.aboutAction)

        self.menuBar().addMenu(self.itemMenu)
        self.menuBar().addMenu(self.helpMenu)

    def insertItem(self):
        action = self.sender()
        if isinstance(action, QtWidgets.QAction):
            itemToAdd = None
            mPen = QtGui.QPen(QtCore.Qt.black, 3, QtCore.Qt.SolidLine)
            eBrush = QtGui.QBrush(QtGui.QColor("#FF7F50"))
            rBrush = QtGui.QBrush(QtGui.QColor("#CC0000"))
            if action.iconText() == "Insert Ellipse":
                itemToAdd = self.scene.addEllipse(0, 0, 150, 75, mPen, eBrush)
            elif action.iconText() == "Insert Rectangle":
                itemToAdd = self.scene.addRect(0, 0, 100, 100, mPen, rBrush)
            if itemToAdd is not None:
                itemToAdd.setFlags(
                    QtWidgets.QGraphicsItem.ItemIsSelectable
                    | QtWidgets.QGraphicsItem.ItemIsMovable
                )

    def about(self):
        QtWidgets.QMessageBox.about(
            self,
            self.tr("About QGraphicsScene Help Example"),
            self.tr(
                "This example demonstrates how to implement\n"
                "help for a Qt application."
            ),
        )

    def createConnections(self):
        self.insertEllipseAction.triggered.connect(self.insertItem)
        self.insertRectangleAction.triggered.connect(self.insertItem)
        self.helpAction.triggered.connect(self.helpWindow.show)
        self.aboutAction.triggered.connect(self.about)

    def keyPressEvent(self, event):
        if event.key() == QtCore.Qt.Key_A:
            for item in self.scene.selectedItems():
                self.scene.removeItem(item)
        else:
            super().keyPressEvent(event)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

注意:使用原始文件“qgraphicshelpexample.qhp”会导致我出错,因为像“./insertobject.html”这样的“./foo”路径没有正确解析,所以我将它们更改为“foo”。

在此处输入图像描述

完整的例子可以在这里找到。

于 2020-07-06T23:33:39.313 回答