PyQt 子类化
从 Python 使用 Qt 小部件的常用方法似乎是对它们进行子类化。
Qt 小部件类有很多方法,因此不可避免地我最终会向子类添加一个方法,其名称与从 Qt 小部件继承的方法相同。在Python中,所有方法都是虚拟的,所以我担心的是一些Qt代码可能最终会调用我的方法而不是预期的Qt方法 - 在最坏的情况下,打破一些不容易显示的边缘情况正在测试中。
另一方面,也许所有 PyQt 方法都只是 C++ 代码的包装器,这当然不会受到我在 Python 子类化方面所做的任何事情的影响。
有谁知道是哪种情况吗?
The usual way to use Qt widgets from Python seems to be to subclass them.
Qt widget classes have a great many methods, so inevitably I'm going to end up adding a method to the subclass, with the same name as one inherited from the Qt widget. In Python, all methods are virtual, so what I'm concerned about is that some Qt code might end up calling my method instead of the expected Qt one - in the worst-case scenario, breaking some edge case that doesn't easily show up in testing.
On the other hand, maybe all the PyQt methods are just wrappers for C++ code, which would of course be unaffected by anything I do in terms of Python subclassing.
Anyone know offhand which is the case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果底层 C++ 方法是虚拟的,则每当 C++ 代码调用它们时,覆盖它们的 Python 方法都会被调用。如果它们只是常规方法,则默认情况下任何 C++ 代码都会调用原始 C++ 方法(Python 代码将调用 Python 方法,因为它看到 Python 对象并且所有方法都是“虚拟”的)。
If the underlaying C++ methods are virtual, your Python methods that override them will be called any time C++ code calls them. If they are just regular methods, any C++ code will call the original C++ methods by default (Python code will call the Python methods though, because it sees the Python object and all methods are "virtual" there).