如何在PyQt中的QGraphicsViews中使用自定义绘图?
我需要在 2 个 QGraphicsViews 中查看 QGraphicsScene ,条件是它们对于场景中的项目具有不同的比例因子。我发现的最接近的函数是drawItems()
,但据我所知,它必须手动调用。如何自动重绘视图? 我在程序中有这两个代码片段:
class TGraphicsView(QGraphicsView):
def __init__(self, parent = None):
print("__init__")
QGraphicsView.__init__(self, parent)
def drawItems(self, Painter, ItemCount, Items, StyleOptions):
print("drawItems")
Brush = QBrush(Qt.red, Qt.SolidPattern)
Painter.setBrush(Brush)
Painter.drawEllipse(0, 0, 100, 100)
...
Mw.gvNavigation = TGraphicsView(Mw) # Mw - main window
Mw.gvNavigation.setGeometry(0, 0, Size1, Size1)
Mw.gvNavigation.setScene(Mw.Scene)
Mw.gvNavigation.setSceneRect(0, 0, Size2, Size2)
Mw.gvNavigation.show()
__init__
有效,显示 Mw.gvNavigation
并且其中有 Mw.Scene items
,但未调用 drawItems()
。
I need to view QGraphicsScene
in 2 QGraphicsViews
with condition that they have different scale factors for items in scene. Closest function which I found is drawItems()
, but as far I can understand, it must be called manually. How to repaint views automatically?
I have these two code fragments in program:
class TGraphicsView(QGraphicsView):
def __init__(self, parent = None):
print("__init__")
QGraphicsView.__init__(self, parent)
def drawItems(self, Painter, ItemCount, Items, StyleOptions):
print("drawItems")
Brush = QBrush(Qt.red, Qt.SolidPattern)
Painter.setBrush(Brush)
Painter.drawEllipse(0, 0, 100, 100)
...
Mw.gvNavigation = TGraphicsView(Mw) # Mw - main window
Mw.gvNavigation.setGeometry(0, 0, Size1, Size1)
Mw.gvNavigation.setScene(Mw.Scene)
Mw.gvNavigation.setSceneRect(0, 0, Size2, Size2)
Mw.gvNavigation.show()
__init__
works, Mw.gvNavigation
is displayed and there are Mw.Scene items
in it, but drawItems()
isn't called.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
QGraphicsView 和 QGraphicsScene 对象上的 drawItems 方法在 Qt 4.6 中已被弃用,必须使用 IndirectPainting 标志来启用,但我不建议使用已弃用的功能。
这是关于类似问题的另一个堆栈溢出问题。答案之一展示了如何使场景中各个项目的绘制方法知道哪个视图正在绘制它们,并在不同视图中绘制时使用不同的绘制代码。
The drawItems methods on QGraphicsView and QGraphicsScene objects have been deprecated in Qt 4.6 and have to be enabled using the IndirectPainting flag, but I would't recommend using deprecated features.
Here's another stack overflow question on a similar issue. One of the answers shows how to make the paint methods on individual items in a scene aware of which view is painting them, and use different paint code when drawn in different views.