Skip to main content
 首页 » 编程设计

python之如何使选定的 qgraphicsitem 的边界矩形自动显示

2025年05月04日16傻小

我在 SO 中找到了@serge_gubenko 的这个例子。 Moving a QGraphicsItem around a central point in PyQt4

然后我做了一些修改以结束: Why is my QGraphicsItem not selectable?

如果我运行该示例 (Moving a QGraphicsItem around a central point in PyQt4) 并单击图形项,它会自动显示一个虚线框,表明它已被选中。我准备了图片来展示效果,但由于我的声誉不高,我还不允许上传这些图片 ;)

对我来说,虚线框的“已选择指示”似乎是自动出现的。 在我修改后的示例 ( Why is my QGraphicsItem not selectable? ) 中,这并没有发生,我不明白为什么?

请您参考如下方法:

您使用 QtGui.QGraphicsItem,因此您定义了 boundingRect 和 paint 方法,其中您使用了 painter drawEllipse 方法。在您找到的第一个示例中,该类直接使用 QtGui.QGraphicsEllipseItem 并且它做了所有的不同,因为那些方法已经定义了。顺便说一句,我没有找到为什么 boundingRect 没有在你的案例中绘制。