我想要一个有轻微改变行为的QAction。我希望QAction只在信号被选中时才发出信号,而且我希望它的复选框始终是可检查的,即使QAction已被禁用。例如,如果将QAction设置为setEnabled(False),则不能单击QAction或检查/取消检查。我喜欢它不能再被点击,但不喜欢我不能切换QAction内的复选框。有可能修改它以得到我想要的东西吗?
下面我附上了一个示例文件。目标是制定一些解决方案,将QAction设置为禁用(setEnabled(False)),但用户仍然可以检查/取消检查。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''An example file with a QAction that stays checkable.'''
# IMPORT STANDARD LIBRARIES
import sys
# IMPORT THIRD-PARTY LIBRARIES
from Qt import QtWidgets
from Qt import QtCore
class AlwaysOpenAction(QtWidgets.QAction):
def __init__(self, *args, **kwargs):
super(AlwaysOpenAction, self).__init__(*args, **kwargs)
self.toggled.connect(self.printout)
def printout(self):
if self.isChecked():
print('Checkbox was checked')
class WindowTest(QtWidgets.QWidget):
'''A basic window.'''
def __init__(self, parent=None):
'''Init the window.'''
super(WindowTest, self).__init__(parent=parent)
self.setLayout(QtWidgets.QHBoxLayout())
self.menubar = QtWidgets.QMenuBar(parent=self)
self.menu = self.menubar.addMenu('Some menu')
action = AlwaysOpenAction('something', self.menu)
action.setCheckable(True)
action.setEnabled(False)
self.menu.addAction(action)
self.layout().addWidget(self.menubar)
self.resize(500, 400)
def main():
'''Do the window test.'''
qapp = QtWidgets.QApplication(sys.argv)
window = WindowTest()
window.show()
sys.exit(qapp.exec_())
if __name__ == '__main__':
main()发布于 2017-05-03 12:41:33
由于您希望仍然能够在QAction中单击以检查和取消检查,所以我不认为有必要禁用QAction。相反,你可以改变它的外观。你试过乱七八糟地处理样式表吗?
只需尝试在代码中添加以下简单行:
self.menu.setStyleSheet("""color: gray;""")所以我想说的是,你可以根据你的意愿改变你的行为方式。
您还可以尝试更详细、更具体的内容,上面的行将将菜单中的每一个文本更改为灰色,您可以指定要更改字体颜色的对象类型,例如:
self.menu.setStyleSheet("""QPushButton{background-color: red}""")等等..。
你可以尝试一些其他的方法来达到你想要的,有时我们只是被困在一些事情上,并且有其他的方法去做同样的事情。在我修改的这段代码中快速查看一下,使之与您想要的类似,我认为")
'''An example file with a QAction that stays checkable.'''
# IMPORT STANDARD LIBRARIES
import sys
# IMPORT THIRD-PARTY LIBRARIES
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QCheckBox
from PyQt5.QtWidgets import QHBoxLayout
from PyQt5.QtWidgets import QMenuBar
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QWidgetAction
class CheckBox(QCheckBox):
def __init__(self, name):
super(CheckBox, self).__init__()
self.update_color()
self.setText(name)
def mousePressEvent(self, event):
self.update_color()
super(CheckBox, self).mousePressEvent(event)
def update_color(self):
if self.isChecked():
self.setStyleSheet("color: gray;")
else:
self.setStyleSheet("")
class WindowTest(QWidget):
'''A basic window.'''
def __init__(self, parent=None):
'''Init the window.'''
super(WindowTest, self).__init__(parent=parent)
self.setLayout(QHBoxLayout())
self.menubar = QMenuBar(parent=self)
self.menu = self.menubar.addMenu('Some menu')
self.menu.setContentsMargins(10,10,10,10)
self.widget_action = QWidgetAction(self)
self.button = CheckBox("some")
self.button.setCheckable(True)
self.button.setFixedSize(100, 20)
self.widget_action.setDefaultWidget(self.button)
self.menu.addAction(self.widget_action)
self.setFixedSize(500,200)
def main():
'''Do the window test.'''
qapp = QApplication(sys.argv)
window = WindowTest()
window.show()
sys.exit(qapp.exec_())
if __name__ == '__main__':
main(),因为我给出的第一个样式表示例有一个缺陷,因为QAction不是一个Widget,所以我不能真正更改它的样式表(它没有样式表),所以我不得不更改菜单样式表,这会影响到其他人。第二个例子使用QCheckBox,它是一个小部件,因此我可以具体地更改它的样式表,并且我可以更好地控制菜单中的每一项:D。
https://stackoverflow.com/questions/43596710
复制相似问题