我正在尝试将我的脚本从PyQt5移植到PyQt6。多亏了this answer,我已经知道了如何移植大多数东西,但是,我遇到了一个问题
我发现PyQt6使用QtWidgets.QMessageBox.StandardButtons.Yes
而不是PyQt5的QtWidgets.QMessageBox.Yes
但是,当检查用户在QMessageBox打开后是否按下“是”时,将QtWidgets.QMessageBox.Yes
替换为QtWidgets.QMessageBox.StandardButtons.Yes
不起作用(请检查下面的示例)
示例:
PyQt5:
reply = QtWidgets.QMessageBox()
reply.setText("Some random text.")
reply.setStandardButtons(QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
x = reply.exec_()
if x == QtWidgets.QMessageBox.Yes:
print("Hello!")
在这里打印“你好!”正常工作(16384 == 16384)
PyQt6:
reply = QtWidgets.QMessageBox()
reply.setText("Some random text.")
reply.setStandardButtons(QtWidgets.QMessageBox.StandardButtons.Yes |
QtWidgets.QMessageBox.StandardButtons.No)
x = reply.exec()
if x == QtWidgets.QMessageBox.StandardButtons.Yes:
print("Hello!")
“你好!”这里根本没有打印(16384 != StandardButtons.yes)
我知道我可以做到:
x = reply.exec()
if x == 16384:
print("Hello!")
因为,在按下“Yes”之后,QMessageBox等于16384(see this),但是我想不使用这种方法,而是使用类似PyQt5示例的方法
StandardButtons不是我可以为QMessageBox选择的属性/方法。不确定这是否在过去4个月内更新过,但对我来说,代码使用的是标准按钮,而不是标准按钮
QtWidgets.QMessageBox.StandardButtons
在PyQt6中使用^{QDialog.exec()
返回一个int
。遗憾的是,这些无法直接比较,但您仍然可以使用:注意,惯用的
x == int(Yes)
也不起作用PyQt5使用了一个包装的自定义} 。一个
StandardButtons
类(键入Yes | No
来查看),而不是另一个答案所声称的^{IntEnum
本来是一个合乎逻辑的选择,但是因为它特别允许int比较这有点奇怪。根据QMessageBox.exec的文件:
您使用的是标准按钮,因此应该返回一个
QMessageBox.StandardButtons
枚举还值得一提的是,在PyQt5中比较整数和枚举不是问题,因为枚举是用
enum.IntEnum
实现的。现在,它们是用enum.Enum
实现的。从Riverbank Computing website开始:然而,由于某种原因,
QMessageBox.exec
返回一个整数(我刚刚用PyQt6==6.0.0
尝试过它)目前,您可以通过故意从返回的整数构造枚举对象来解决此问题:
而且,由于您正在比较枚举,我建议使用
is
而不是==
相关问题 更多 >
编程相关推荐