PyQt - 即使禁用了QAction也可以检查它

Col*_*edy 8 python pyqt

我想要一个行为略有改变的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()
Run Code Online (Sandbox Code Playgroud)

yur*_*snm 2

因为您希望仍然能够单击 QAction 来选中和取消选中,所以我认为没有必要禁用 QAction。相反,您可以只更改它的外观。您是否尝试过使用样式表?

只需尝试在代码中添加这一行简单的代码:

self.menu.setStyleSheet("""color: gray;""")
Run Code Online (Sandbox Code Playgroud)

所以我在这里想说的是,你可以根据你的意愿改变你的qaction的风格。

您还可以尝试更详细、更具体的操作,上面的行会将菜单中的每个文本更改为灰色,您可以指定要更改字体颜色的对象类型,等等,例如:

self.menu.setStyleSheet("""QPushButton{background-color: red}""")
Run Code Online (Sandbox Code Playgroud)

等等...

你可以尝试一些其他方法来实现你想要的,有时我们只是陷入困境,但还有其他方法可以做到同样的事情。快速浏览一下我修改过的这段代码,它与您想要的类似,我认为“)

'''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()
Run Code Online (Sandbox Code Playgroud)

由于我给你的第一个样式表示例有一个缺陷,因为 QAction 不是一个小部件,所以我无法真正更改它的样式表(它没有样式表),所以我必须更改菜单样式表,这会影响其他所有人。第二个示例使用 QCheckBox,它是一个小部件,因此我可以专门更改其样式表,并且我可以更好地控制菜单中的每一项:D