【发布时间】:2017-10-27 10:52:53
【问题描述】:
我正在尝试创建一个自定义 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()
这是一个简单的窗口,可以打印出您在文本框中输入的任何内容(除非它是非十六进制字符)。现在,这在大多数情况下都可以正常工作。但是,当我在文本框中输入设置了最高有效位的数字时,它会表现出一些奇怪的行为。例如,如果我在文本框中输入0x4afecafe,然后单击按钮,它将打印:
我收到了 4afecafe
但输入0xcafecafe 将导致以下输出:
我收到了-35013502
如果那是 C/C++ 这并没有错,但在 Python 中这会破坏我的程序,因为 -35013502 == 0xcafecafe 返回 False。
所以我的问题是:
- 为什么会这样?我想这与 Python 包装器的底层实现有关,但我不太明白。
- 如何解决这个问题?我希望插槽接收一个 Python
int对象,该对象持有0xcafecafe。
【问题讨论】:
标签: python c++ pyqt5 signals-slots signed