我应该如何将 QGraphicsScene 与布局和小部件一起使用
我正在 Qt4 中创建一些图形数据显示小部件,我很想使用 QGraphicsScene 来为它创建 QGraphicsItem 来为数据项等创建。
但是,我想添加一些控件层(例如滚动条、缩放+其他按钮 - 我想使其采用与 Google 地图类似的风格,也就是说,数据将显示在整个小部件上,按钮将显示在顶部其中)到小部件。 所以我认为将它们添加到场景中可能是可行的(也许作为将在数据上显示的 QGraphicsGroupItem
的子项)。 但我希望他们能够移动并移动。 当我调整整个小部件的大小时,我会调整大小,因此我应该使用 QGraphicsLayout 来管理它们。 但此时我发现事情相当复杂。
问题是,当使用QGraphicsLayout
时,存在以下约束:
- 布局只能管理
QGraphicsWidget
, QGraphicsLayout
只能用于管理QGraphicsWidget
的子级,
这意味着我必须将控件创建为 QGraphicsWidget
,向数据小部件添加顶级 QGraphicsWidget
,然后我自己管理这个顶级小部件的大小。
所以我想问:
经典方法(即对所有控件使用普通的旧小部件,并仅使用
QGraphicsScene
来显示数据)不是更合理吗?在这种情况下使用
QGraphicsScene
有什么优势吗(性能或简单性...)?我应该如何使用
QGraphicsScene
来发挥它的优势?
I'm creating some graphic data displaying widget in Qt4 and I was tempted to use the QGraphicsScene
for it, create QGraphicsItem
s for the data items etc.
However, I wanted to add some layer of controls (eg. scrollbars, zoom+other buttons - I want to make it in a similar style as eg. Google Maps, that is, the data would be displayed all over the widget, and the buttons would be shown atop of them) to the widget. So I thought it might be feasible to add them to the scene (perhaps as a child of a QGraphicsGroupItem
that would be shown over the data). But I want them to move & resize when I resize the whole widget, so I should use a QGraphicsLayout
for managing them. But at this point, I discovered things are pretty complicated.
The problem is that when using QGraphicsLayout
, the following constraints hold:
- Only a
QGraphicsWidget
can be managed by a layout QGraphicsLayout
can only be used to manage children of aQGraphicsWidget
Which means that I would have to create my controls as QGraphicsWidget
s, add a top level QGraphicsWidget
to the data widget, and manage the size of this top level widget myself.
So I want to ask:
Wouldn't a classic approach (ie. use plain old widgets for all controls, and use
QGraphicsScene
only for displaying the data) be more reasonable?Is there any advantage in using
QGraphicsScene
in this case (performance or simplicity...)?How should I use
QGraphicsScene
to exploit its strengths?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 Qt 4.4 开始,您可以使用
在
:QGraphicsScene
中嵌入经典小部件QGraphicsProxyWidgetSince Qt 4.4 you can embed classic widgets in a
QGraphicsScene
by usingQGraphicsProxyWidget
:如果您认为 QGraphicsScene(或您拥有的任何其他小部件)适合您的大部分显示,请使用它。 我们过去对类似的事情所做的就是制作一个从
QWidget
继承(以某种方式)的自定义小部件,并将控制小部件放在该小部件顶部的布局中。 这意味着整个小部件正在绘制您想要绘制的任何内容,并且控制小部件位于其之上,随着整个小部件的大小调整而调整大小。或者,有几次我们的布局有点太复杂,布局小部件无法轻松处理。 我们没有创建自定义布局,而是只是在没有布局的情况下定位它们,并在调整大小事件的代码中移动它们。 它也同样有效。
If you think that
QGraphicsScene
(or whatever other widget you have) is appropriate for most of your display, use that. What we have done in the past for somewhat similar things is to make a custom widget that inherits (one way or another) fromQWidget
, and put the control widgets in a layout on top of that widget. This means that the whole widget is drawing whatever it is you want drawn, and the control widgets are on top of that, resizing as the whole widget is resized.Alternatively, a couple of times we've had layouts that were just a bit too complicated for the layout widgets to easily handle. Rather than create a custom layout, we just positioned them with no layout, and moved them in code on the resize event. It works just as well.