如何将WindowContextHelpButtonHint连接到小部件单击?

时间:2017-02-15 10:04:34

标签: qt python-3.x pyqt4

我可以将self.setWindowFlags(Qt.Window | Qt.WindowContextHelpButtonHint | Qt.WindowCloseButtonHint)添加到QMainWindow的构造函数中,该构造函数会添加'?'按钮。现在,我想向几乎所有UI组件添加上下文帮助信息。如何点击'?'按钮,然后单击某个ui元素(比如qlistview),弹出一条帮助信息?

以下是我的程序的一部分,以便轻松实现。如何在用户点击“'?'然后显示帮助消息的QDoubleSpinBoxQListView或任何QPushButtons

我已经搜索了这个,但似乎有大量的问题和答案是关于如何删除'?'按钮或如何添加它。实际上没有人处理如何改变按钮的行为。

我还尝试使用setInputMethodHints()为ui组件分配提示,但这种方法并不像我想象的那样接受自定义字符串。

顺便说一下:我也想要'?'按钮不替换最小化按钮。我还没弄明白如何实现这一目标。

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

APPNAME = 'Mesoscale Brain Explorer'
class MainWindow(QMainWindow):
  def __init__(self, parent=None):
    super(MainWindow, self).__init__(parent)
    self.setWindowTitle(APPNAME)
    self.sidebar = Sidebar()
    self.setWindowFlags(Qt.Window | Qt.WindowContextHelpButtonHint | Qt.WindowCloseButtonHint)
    self.setup_ui()

  def setup_ui(self):
    self.pl_frame = QFrame()
    splitter = QSplitter(self)
    self.enable = lambda yes: splitter.setEnabled(yes)
    splitter.setHandleWidth(3)
    splitter.setStyleSheet('QSplitter::handle {background: #cccccc;}')
    splitter.addWidget(self.sidebar)
    splitter.addWidget(self.pl_frame)
    self.setCentralWidget(splitter)
    self.menu = self.menuBar()
    m = self.menu.addMenu('&File')

    a = QAction('&New project', self)
    a.setShortcut('Ctrl+N')
    a.setStatusTip('Create new project')
    m.addAction(a)

    a = QAction('&Open project', self)
    a.setShortcut('Ctrl+O')
    a.setStatusTip('Open project')
    m.addAction(a)

    a = QAction("&Quit", self)
    a.setShortcut("Ctrl+Q")
    a.setStatusTip('Leave The App')
    a.setIcon(QIcon('pics/quit.png'))
    m.addAction(a)

    about_action = QAction('&About ' + APPNAME, self)
    about_action.setStatusTip('About ' + APPNAME)
    about_action.setShortcut('F1')

    m = self.menu.addMenu('&Project')
    m.setEnabled(False)
    a = QAction("&Close", self)
    a.setStatusTip('Close project')
    m.addAction(a)
    self.project_menu = m

    help_menu = self.menu.addMenu('&Help')
    help_menu.addAction(about_action)


class Sidebar(QWidget):
    open_pipeconf_requested = pyqtSignal()
    open_datadialog_requested = pyqtSignal()
    automate_pipeline_requested = pyqtSignal()
    x_origin_changed = pyqtSignal(float)
    y_origin_changed = pyqtSignal(float)
    units_per_pixel_changed = pyqtSignal(float)

    def __init__(self, parent=None):
        super(Sidebar, self).__init__(parent)
        self.x_origin = QDoubleSpinBox()
        self.y_origin = QDoubleSpinBox()
        self.units_per_pixel = QDoubleSpinBox()
        self.setup_ui()

    def setup_ui(self):
        self.setContentsMargins(4, 6, 5, 0)

        vbox = QVBoxLayout()

        hbox = QHBoxLayout()
        hbox.addWidget(QLabel('Origin:'))
        hbox.addWidget(QLabel('Units per pixel:'))
        vbox.addLayout(hbox)
        hbox2 = QHBoxLayout()
        hhbox = QHBoxLayout()
        hhbox2 = QHBoxLayout()
        hhbox.addWidget(QLabel("X:"))
        hhbox.addWidget(self.x_origin)
        hhbox.addWidget(QLabel("Y:"))
        hhbox.addWidget(self.y_origin)
        hhbox2.addWidget(self.units_per_pixel)
        hbox2.addLayout(hhbox)
        hbox2.addLayout(hhbox2)
        vbox.addLayout(hbox2)
        self.units_per_pixel.setDecimals(5)
        self.units_per_pixel.setMaximum(100000)
        self.x_origin.setMaximum(100000)
        self.y_origin.setMaximum(100000)

        self.pl_list = QListView()
        self.pl_list.setIconSize(QSize(18, 18))
        self.pl_list.setSelectionMode(QAbstractItemView.ExtendedSelection)
        self.pl_list.setEditTriggers(QAbstractItemView.NoEditTriggers)
        vbox.addWidget(QLabel('Pipeline:'))
        vbox.addWidget(self.pl_list)

        pb = QPushButton('&Automation')
        pb.clicked.connect(self.automate_pipeline_requested)
        vbox.addWidget(pb)

        vbox.addSpacerItem(QSpacerItem(0, 1, QSizePolicy.Minimum, QSizePolicy.Expanding))

        pb = QPushButton('&Configure Pipeline')
        pb.clicked.connect(self.open_pipeconf_requested)
        vbox.addWidget(pb)
        pb = QPushButton('&Manage Data')
        pb.clicked.connect(self.open_datadialog_requested)
        vbox.addWidget(pb)

        vbox.setStretch(0, 0)
        vbox.setStretch(1, 0)
        vbox.setStretch(2, 0)

        self.setLayout(vbox)

if __name__ == '__main__':
  app = QApplication(sys.argv)
  app.setApplicationName(APPNAME)
  app.setOrganizationName('University of British Columbia')

  w = MainWindow()
  w.resize(1060, 660)
  w.show()

  app.exec_()
  app.deleteLater()
  del w
  sys.exit()

0 个答案:

没有答案