PyQt5:使用自定义信号时 int 对象的错误转换
PyQt5: Wrong conversion of int object when using custom signal
我正在尝试创建自定义 pyqtSignal
以传递一些整数,但我遇到了一些问题。让我首先给出演示问题的最小示例:
#!/usr/local/bin/python3
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
class Form(QDialog):
post_request = pyqtSignal(int)
def __init__(self, parent=None):
super(Form, self).__init__(parent)
self.button = QPushButton('Click Me')
self.textbox = QLineEdit(self)
layout = QHBoxLayout()
layout.addWidget(self.button)
layout.addWidget(self.textbox)
self.setLayout(layout)
self.button.clicked.connect(self.on_button_click)
self.post_request.connect(self.test)
self.setWindowTitle('Test')
def on_button_click(self):
try:
integer = int(self.textbox.text(), 16)
self.post_request.emit(integer)
except ValueError:
print('wrong input')
def test(self, integer):
print('I received {0:x}'.format(integer))
def run_app():
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
if __name__ == '__main__':
run_app()
这是一个简单的 window,它会打印出您在文本框中输入的任何内容(除非是非十六进制字符)。现在,这在大多数情况下都可以正常工作。但是,当我在文本框中键入一个设置了最高有效位的数字时,它会表现出一些奇怪的行为。例如,如果我在文本框中输入 0x4afecafe
然后单击按钮,它将打印:
I received 4afecafe
但输入 0xcafecafe
将导致以下输出:
I received -35013502
如果那是 C/C++ 并没有错,但是在 Python 中这会破坏我的程序,因为 -35013502 == 0xcafecafe
returns False
.
所以我的问题是:
- 为什么会这样?我想这与 Python 包装器的底层实现有关,但我不太明白。
- 我该如何解决这个问题?我希望槽接收一个 Python
int
对象持有 0xcafecafe
.
发送信号时,PyQt 将始终尝试转换为相应的 C++ 类型。但在它不能的地方,它只会发送一个指向 Python 对象的指针。因此,解决问题的最简单方法是不声明信号参数的具体类型,如下所示:
class Form(QDialog):
post_request = pyqtSignal(object)
现在 PyQt 不会尝试在 Python 和 C++ 类型之间进行转换。这是利用 Python 类型的默认行为,例如 tuple
,在 C++ 中没有等效项。
我正在尝试创建自定义 pyqtSignal
以传递一些整数,但我遇到了一些问题。让我首先给出演示问题的最小示例:
#!/usr/local/bin/python3
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
class Form(QDialog):
post_request = pyqtSignal(int)
def __init__(self, parent=None):
super(Form, self).__init__(parent)
self.button = QPushButton('Click Me')
self.textbox = QLineEdit(self)
layout = QHBoxLayout()
layout.addWidget(self.button)
layout.addWidget(self.textbox)
self.setLayout(layout)
self.button.clicked.connect(self.on_button_click)
self.post_request.connect(self.test)
self.setWindowTitle('Test')
def on_button_click(self):
try:
integer = int(self.textbox.text(), 16)
self.post_request.emit(integer)
except ValueError:
print('wrong input')
def test(self, integer):
print('I received {0:x}'.format(integer))
def run_app():
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
if __name__ == '__main__':
run_app()
这是一个简单的 window,它会打印出您在文本框中输入的任何内容(除非是非十六进制字符)。现在,这在大多数情况下都可以正常工作。但是,当我在文本框中键入一个设置了最高有效位的数字时,它会表现出一些奇怪的行为。例如,如果我在文本框中输入 0x4afecafe
然后单击按钮,它将打印:
I received 4afecafe
但输入 0xcafecafe
将导致以下输出:
I received -35013502
如果那是 C/C++ 并没有错,但是在 Python 中这会破坏我的程序,因为 -35013502 == 0xcafecafe
returns False
.
所以我的问题是:
- 为什么会这样?我想这与 Python 包装器的底层实现有关,但我不太明白。
- 我该如何解决这个问题?我希望槽接收一个 Python
int
对象持有0xcafecafe
.
发送信号时,PyQt 将始终尝试转换为相应的 C++ 类型。但在它不能的地方,它只会发送一个指向 Python 对象的指针。因此,解决问题的最简单方法是不声明信号参数的具体类型,如下所示:
class Form(QDialog):
post_request = pyqtSignal(object)
现在 PyQt 不会尝试在 Python 和 C++ 类型之间进行转换。这是利用 Python 类型的默认行为,例如 tuple
,在 C++ 中没有等效项。