在 Pyside 中,为什么发出一个整数 > > 0x7FFFFFFF 导致“溢出错误”信号处理后?

发布于 2025-01-09 19:04:51 字数 989 浏览 0 评论 0原文

我正在尝试使用具有 0 - 2^32-1 范围内的大整数的信号/槽。我发现了一些奇怪的事情——一旦我发出> 7FFFFFFF 边界,我在插槽运行后抛出 OverflowError 异常。如果我或 QT 在另一种语言(如 C 或 C++)中显式使用带符号的 32 位整数,我可能会预料到这种溢出 - 因为我们都知道 0x80000000 在 2 补码表示法中回绕为 -2^31。但在 python 中,没有换行的话它只是 2^32。我在编写代码时的假设是,这是 python 并且内置 int 可以变得非常大(也许是任意的?)并且我不需要明确地将某些东西定义为 32 或 64 位或有符号/无符号。这一切都会奏效。

下面的代码演示了我所看到的内容(Python 2.7.2(64 位)、Pyside 1.1.0、Windows 7)

from PySide.QtCore import *

@Slot(int)
def say(i):
    print "Say %i" % i

class Communicate(QObject):
    speak = Signal(int)

someone = Communicate()
someone.speak.connect(say)
someone.speak.emit(0x7FFFFFFF) #works fine
someone.speak.emit(0x80000000) #OverflowError after slot "say" runs
say(0x80000000)                #works fine

确切的输出是:

Say 2147483647
Say -2147483648
OverflowError
Say 2147483648
  1. Why does Qt似乎将整数类型的信号/槽视为处理有符号的 32 位整数而不是 python 内置整数?
  2. 如果这是 Qt 的限制,我该怎么做才能将 int 标记为无符号或确保 QT 可以处理整数 > 0x7FFFFFFF?

I'm attempting to use signals/slots with large integers ranging from 0 - 2^32-1. I've discovered something a little weird -- once I emit > 7FFFFFFF boundary, I get OverflowError exceptions thrown after the slot is run. I might expect this kind of overflow if I or QT were explicitly using a signed 32 bit integer in another language like C or C++--as we all know 0x80000000 wraps back to -2^31 in 2s complement notation. In python though, its just 2^32 without wrapping. My assumption when writing the code though was that this is python and that the built-in int can grow very large (maybe arbitrarilly so?) and that I don't explicitly need to define something as 32 or 64 bit or signed/unsigned. It would all just work.

The code below demonstrates what I'm seeing (Python 2.7.2 (64 bit), Pyside 1.1.0, Windows 7)

from PySide.QtCore import *

@Slot(int)
def say(i):
    print "Say %i" % i

class Communicate(QObject):
    speak = Signal(int)

someone = Communicate()
someone.speak.connect(say)
someone.speak.emit(0x7FFFFFFF) #works fine
someone.speak.emit(0x80000000) #OverflowError after slot "say" runs
say(0x80000000)                #works fine

The exact output is:

Say 2147483647
Say -2147483648
OverflowError
Say 2147483648
  1. Why does Qt seem to treat the signals/slots of type integer as if its dealing with signed 32 bit integers and not python built-in ints?
  2. If this is a restriction of Qt, what can I do to mark the int as unsigned or make sure QT can deal with integers > 0x7FFFFFFF?

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

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

发布评论

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

评论(1

云巢 2025-01-16 19:04:51

我主要是 PyQt 用户,但我相信行为是相似的。信号定义中的 int 映射为 4 字节整数(Qt 理解 int)。

一种可能的解决方案是强制信号发出 Python 对象。这是可行的:

class Communicate(QObject):
    speak = Signal(object)

但请注意,如果您将此信号连接到需要 Qt 版本的 int 的插槽(例如 QtGui.QSpinBox.setMaximum),您会看到相同的行为。除此之外,纯粹在 Python 端使用这个信号应该没问题。

I'm mostly a PyQt user, but I believe the behavior is similar. int in signal definition is mapped to 4-byte integer (as Qt understands an int).

One possible solution is to force the signal to emit a Python object. This works:

class Communicate(QObject):
    speak = Signal(object)

But be aware that, if you connect this signal to a slot that expects a Qt's version of int (for example QtGui.QSpinBox.setMaximum) you'll see the same behavior. Other than that, using this signal purely on the Python side should be fine.

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