[用较小的示例更新]
我们刚刚升级到 PyQt 5.7,我们的应用程序中还有最后一个问题需要解决。这是我根据应用程序代码创建的一个独立示例。运行它并查看椭圆如何绘制到 View 边界之外。这在 5.5.1 中没有发生。平台是 Windows 7 64 位(在虚拟机中运行)。它看起来像这样:
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QGraphicsView, QGraphicsScene, QHBoxLayout, QWidget, QLabel
from PyQt5.QtWidgets import QGraphicsProxyWidget, QGraphicsObject, QGraphicsEllipseItem
from PyQt5.QtGui import QBrush
class MyGraphicsItem(QGraphicsObject):
def __init__(self):
QGraphicsObject.__init__(self)
# next line could be any type of graphics item:
rect_item = QGraphicsEllipseItem(0, 0, 100, 100, self)
# effect easier to see if paint black:
rect_item.setBrush(QBrush(Qt.SolidPattern))
label_item = QGraphicsProxyWidget(self)
# *** Next line must be there for effect to be visible, but could be any other type of widget
label_item.setWidget(QLabel('a'*30))
def paint(self, painter, option, widget=None):
return
def boundingRect(self):
return self.childrenBoundingRect()
def show_problem():
app = QApplication([])
widget = QWidget()
layout = QHBoxLayout()
widget.setLayout(layout)
view = QGraphicsView()
scene = QGraphicsScene()
view.setScene(scene)
scene.addItem(MyGraphicsItem()) # *** effect only there if more than 1 item
scene.addItem(MyGraphicsItem())
layout.addWidget(view)
widget.setGeometry(100, 100, 50, 50)
widget.show()
app.exec()
show_problem()
最佳答案
所以拉塞尔确认 this is a regression in 5.7 (即,PyQt 5.6 中不存在问题)。
我将 Python 示例的 C++ 版本发布到了 Qt 论坛。那里的一位好心人修复了编译错误并运行它,并看到了该错误:所以这确实是一个 Qt 错误。
有一个bug report at bugreports.qt.io这几乎肯定是同一个问题。它尚未解决,但幸运的是,它链接到帖子 where there is a workaround :将代理小部件项目的不透明度设置为略低于 1。在我的帖子的示例应用程序中,我添加了
label_item.setOpacity(0.999999)
设置 label_item 的小部件后。在我们真正的应用程序(110k PyQt 代码)中,我设置了所有 QGraphicsProxyWidget 实例的不透明度,它似乎有效。
这个解决方法似乎没有任何其他副作用,时间会证明是否存在更深层次的问题。但在 Qt 中修复 bug 之前,这至少是一个临时解决方案。我已经提交了bug report .
关于qt - PyQt 5.7 中 GraphicsView 中的奇怪绘画错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39376050/