Python 3:PyQt:禁用复选框+不灰显+显示工具提示

时间:2016-07-28 09:49:04

标签: python checkbox pyqt pyqt4 disabled-control

我发现这样做的唯一方法是here: How to make QCheckBox readonly, but not grayed-out。但是,这会禁用鼠标与控件的交互。但是当鼠标悬停在控件上时,我需要显示工具提示。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

#If your are not expecting this answer, sorry.

self.checkBox = QtGui.QCheckBox()
self.checkBox.setEnabled (False)
self.checkBox.setToolTip ('my checkBox')

答案 1 :(得分:1)

如果我已正确理解,这就是您要求的内容,一个显示工具提示的已禁用复选框:

 import sys
 from PyQt4 import QtGui, QtCore


 class Example(QtGui.QWidget):

      def __init__(self):
           super(Example, self).__init__()
           self.initUI()

      def initUI(self):
           self.cb = QtGui.QCheckBox('Disabled CheckBox showing tooltips', self)
           self.cb.move(20, 20)
           self.cb.toggle()
           # self.cb.setEnabled(False)
           # self.cb.setStyleSheet("color: black")
           # self.cb.setAttribute(QtCore.Qt.WA_AlwaysShowToolTips)
           self.cb.setToolTip ('my checkBox')
           self.cb.toggled.connect(self.prevent_toggle)

           self.setGeometry(300, 300, 250, 50)
           self.setWindowTitle('QtGui.QCheckBox')
           self.show()

      def prevent_toggle(self):
           self.cb.setChecked(QtCore.Qt.Checked)

 def main():
      app = QtGui.QApplication(sys.argv)
      ex = Example()
      sys.exit(app.exec_())


 if __name__ == '__main__':
      main()