简体   繁体   English

有没有办法在pyqt5中显示帮助选项卡?

[英]Is there any way to show help tab in pyqt5?

I am writing code for my collage project in pyqt5 where I need to make one help tab.我正在 pyqt5 中为我的拼贴项目编写代码,我需要在其中制作一个帮助选项卡。 I am planning to make help content-wise as most the software have as shown in the below image(the help of onlyoffice).我计划在内容方面提供帮助,因为大多数软件都如下图所示(onlyoffice 的帮助)。 Is there any way to write it easily?有什么方法可以轻松写出来吗?

在此处输入图片说明

The problem with that kind of interface, which shows multiple "tabs" embedded in the title bar, is that it's not easily doable with Qt, and you should implement the whole title bar by hand, which is not easy.这种在标题栏中显示多个“选项卡”的界面的问题在于,使用 Qt 不容易实现,您应该手动实现整个标题栏,这并不容易。

If you're looking for a simpler solution, I'd suggest to use a QTabWidget that doesn't show the tab bar if there's only one tab.如果您正在寻找更简单的解决方案,我建议您使用 QTabWidget,如果只有一个选项卡,则不显示选项卡栏。 If you're not already using a tabbed interface with closable tabs, you can set the tab widget to allow closable tabs and override the default methods in order to hide the close button if not really required.如果您还没有使用带有可关闭选项卡的选项卡式界面,您可以将选项卡小部件设置为允许可关闭选项卡并覆盖默认方法,以便在不需要时隐藏关闭按钮。

class TabWidget(QtWidgets.QTabWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setDocumentMode(True)
        self.setTabsClosable(True)
        self.tabCloseRequested.connect(self.removeTab)
        self.tabBar().hide()

    def addTab(self, *args, **kwargs):
        self.insertTab(-1, *args, **kwargs)

    def insertTab(self, *args, **kwargs):
        super().insertTab(*args)
        closable = kwargs.get('closable', False)
        if not closable:
            index = args[0]
            if index < 0:
                index = self.count() - 1
            for side in QtWidgets.QTabBar.LeftSide, QtWidgets.QTabBar.RightSide:
                widget = self.tabBar().tabButton(index, side)
                if isinstance(widget, QtWidgets.QAbstractButton):
                    self.tabBar().setTabButton(index, side, None)
                    break
        self.tabBar().setVisible(self.count() > 1)

    def removeTab(self, index):
        super().removeTab(index)
        self.tabBar().setVisible(self.count() > 1)


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.tabWidget = TabWidget()
        self.setCentralWidget(self.tabWidget)

        self.main = QtWidgets.QWidget()
        self.tabWidget.addTab(self.main, 'My program')
        layout = QtWidgets.QGridLayout(self.main)

        someButton = QtWidgets.QPushButton('Some button')
        layout.addWidget(someButton, 0, 0)

        layout.addWidget(QtWidgets.QLabel('Some label'), 0, 1)

        helpButton = QtWidgets.QPushButton('Show help!')
        layout.addWidget(helpButton, 0, 2)

        textEdit = QtWidgets.QTextEdit()
        layout.addWidget(textEdit, 1, 0, 1, 3)

        self.helpTab = QtWidgets.QTextBrowser()
        self.helpTab.setHtml('Hello, this is <b>help</b>!')

        helpButton.clicked.connect(self.showHelp)

    def showHelp(self):
        for i in range(self.tabWidget.count()):
            if self.tabWidget.widget(i) == self.helpTab:
                break
        else:
            self.tabWidget.addTab(self.helpTab, 'Help!', closable=True)
        self.tabWidget.setCurrentWidget(self.helpTab)
        self.tabWidget.tabBar().show()

Now, since you also want some context-based help, you could hack your way through the whatsThis() feature.现在,由于您还需要一些基于上下文的帮助,您可以通过whatsThis()功能进行破解 The "what's this" feature allows to show some context-based help in a small overlayed window when the window is in the "what's this mode" and the user clicks on a widget.当窗口处于“这是什么模式”并且用户单击小部件时,“这是什么”功能允许在覆盖的小窗口中显示一些基于上下文的帮助。 We can use an event filter to detect when the user clicks on a widget and use the whatsThis() property as a context for showing the related help.我们可以使用事件过滤器来检测用户何时单击小部件并使用whatsThis()属性作为显示相关帮助的上下文。

In the following example I'm using a simple dictionary that fills the QTextBrowser, but you can obviously use access to local documentation files or even the Qt Help framework .在下面的示例中,我使用了一个填充 QTextBrowser 的简单字典,但您显然可以使用对本地文档文件甚至Qt 帮助框架的访问

Note that in order to use this approach, I had to install an event filter on all child widgets, and that's because Qt is able to react to "what's this" events if the widget actually has a whatsThis() property set.请注意,为了使用这种方法,我必须在所有子小部件上安装一个事件过滤器,这是因为如果小部件实际上具有whatsThis()属性集,Qt 能够对“这是什么”事件做出反应。 The trick is to set a whatsThis property for all child widgets when the window enters the what's this mode and install a specialized event filter on each of them, then uninstall the event filter as soon as the what's this mode is left.诀窍是在窗口进入what's this 模式时为所有子小部件设置一个whatsThis属性,并在每个小部件上安装一个专门的事件过滤器,然后在剩下这个模式时立即卸载事件过滤器。

NoWhatsThisText = '__NoWhatsThis'
NoWhatsThisValue = 'There is no help for this object'
HelpData = {
    NoWhatsThisText: NoWhatsThisValue, 
    'someButton': 'Do something with the button', 
    'helpButton': 'Click the button to show this help', 
    'textEdit': 'Type <b>some text</b> to <i>read</i> it', 
    'mainWindow': 'A main window is cool!', 
}

class WhatsThisWatcher(QtCore.QObject):
    whatsThis = QtCore.pyqtSignal(str)
    def eventFilter(self, source, event):
        if event.type() == QtCore.QEvent.WhatsThis:
            whatsThis = source.whatsThis()
            while whatsThis == NoWhatsThisText:
                if not source.parent():
                    break
                source = source.parent()
                whatsThis = source.whatsThis()
            self.whatsThis.emit(whatsThis)
            event.accept()
            return True
        return super().eventFilter(source, event)


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        # ...

        whatsThisAction = self.menuBar().addAction('What\'s this?')
        whatsThisAction.triggered.connect(
            QtWidgets.QWhatsThis.enterWhatsThisMode)

        self.watchedWhatsThis = []
        self.whatsThisWatcher = WhatsThisWatcher()
        self.whatsThisWatcher.whatsThis.connect(self.showHelp)
        self.installEventFilter(self.whatsThisWatcher)

        someButton.setWhatsThis('someButton')
        helpButton.setWhatsThis('helpButton')
        textEdit.setWhatsThis('textEdit')
        self.setWhatsThis('mainWindow')

    def showHelp(self, context=''):
        # ...
        if context:
            self.helpTab.setHtml(HelpData.get(context, NoWhatsThisValue))
        if QtWidgets.QWhatsThis.inWhatsThisMode():
            QtWidgets.QWhatsThis.leaveWhatsThisMode()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM