qt.checkstate.Checked!= 2和qt.checkstate.checked!= 0

发布于 2025-01-27 02:30:19 字数 917 浏览 1 评论 0原文

PYQT6文档说qt.checkstate.inchecked == 0qt.checkstate.checked == 2

我写了一个小程序来测试这一点,但结果完全不同。

这是程序代码:

from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QApplication, QMainWindow, QCheckBox


class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.setWindowTitle("My App")

        self.widget = QCheckBox("CB")
        self.widget.setCheckState(Qt.CheckState.Checked)
        self.widget.stateChanged.connect(self.print_state)

        self.setCentralWidget(self.widget)

    def print_state(self, state):
        print(state)
        print(state == Qt.CheckState.Unchecked)


if __name__ == '__main__':
    app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec()

但是当我单击复选框时,显示以下内容:

0
False
2
False
0
False
2
False

为什么?

The PyQt6 documentation says that Qt.CheckState.Unchecked == 0 and Qt.CheckState.Checked == 2.

I wrote a little program to test this, but the result is completely different.

Here is the program code:

from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QApplication, QMainWindow, QCheckBox


class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.setWindowTitle("My App")

        self.widget = QCheckBox("CB")
        self.widget.setCheckState(Qt.CheckState.Checked)
        self.widget.stateChanged.connect(self.print_state)

        self.setCentralWidget(self.widget)

    def print_state(self, state):
        print(state)
        print(state == Qt.CheckState.Unchecked)


if __name__ == '__main__':
    app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec()

But when I click on the checkbox, the following is displayed:

0
False
2
False
0
False
2
False

Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太傻旳人生 2025-02-03 02:30:19

问题在于您正在比较一个INT(由statechanged Signal发送的值)和枚举(qt.checkstate.inchecked),因此无法直接比较它。解决方案是将整数转换为枚举:

def print_state(self, state):
    print(Qt.CheckState(state) == Qt.CheckState.Unchecked)

The problem is that you are comparing an int(the value sent by the stateChanged signal) and an enum(Qt.CheckState.Unchecked) so it cannot be compared directly. The solution is to convert the integer to enum:

def print_state(self, state):
    print(Qt.CheckState(state) == Qt.CheckState.Unchecked)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文