qt.checkstate.Checked!= 2和qt.checkstate.checked!= 0
PYQT6文档说qt.checkstate.inchecked == 0
和qt.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于您正在比较一个INT(由statechanged Signal发送的值)和枚举(qt.checkstate.inchecked),因此无法直接比较它。解决方案是将整数转换为枚举:
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: