python - 这段在 Pyqt 中创建 QPolygon 的代码正在停止我的应用程序!帮助?

标签 python windows qt pyqt pyqt4

大家好,

以下代码:

self.painter = QtGui.QPainter(self)
self.painter.setRenderHint(QPainter.Antialiasing)
self.painter.translate(482,395)
self.painter.scale(300,300)
self.painter.save()
needle = Qt.QPolygon([QPoint(30, 0), QPoint(-30, 0), QPoint(0, 200)])
self.painter.setBrush(Qt.cyan)
self.painter.setPen(Qt.black)
self.painter.drawPolygon(needle)
self.painter.restore()

导致我的 Pyqt 应用程序崩溃。有谁知道为什么?它是我的 ui_form.py 文件的一部分,由 pyuic4 自动吐出。删除/注释掉它可以解决问题。是的,这是我自己写的,而不是编译器做的。

非常感谢!

最佳答案

将传递给 QPolygon 构造函数的列表保存在局部变量中。我猜这些元素会在调用返回后立即被垃圾回收,因此当您绘制多边形时,它们就不再存在了。

points = [QPoint(30, 0), QPoint(-30, 0), QPoint(0, 200)]
needle = Qt.QPolygon(points)

关于python - 这段在 Pyqt 中创建 QPolygon 的代码正在停止我的应用程序!帮助?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3828489/

相关文章:

Python - ascii 编解码器无法解码位置 0 : ordinal not in range(128) 中的字节 0xe3

python请求POST 400错误

c++ - 使用 gdb 附加到尚未启动的进程

windows - "An unhandled non-continuable exception was thrown during process load"

c++ - 为什么 GetClientRect 包括窗口边框和标题栏?

qt 无法打开输入文件 'c:\Qt\qt\lib\qtmaind.lib'

python - 在 Python 中将 multi_match 与 Elasticsearch 批量扫描结合使用

python - 为什么我的代码在 .py 文件中运行时可以工作,但在 Python 解释器中返回 SyntaxError ?

c++ - 从文本文件/文本流错误中读取 QStrings

c++ - QDateTime Isodate with timezone ?