QGraphicsView 具有自动项目放置功能
我想使用 QGraphicsView 编写一个资源浏览器。它与使用 QGraphicsView 和 QGraphicsItems 的示例略有不同,因为我只想要一个滚动条,并且当视口大小发生变化时,我希望项目自动移动。例如,当视口宽度足够大以显示 4 个资源时,它们应该像这样显示:
aaaa
aaaa
aa
但是当视口缩小并且只能连续包含 3 个资源时,它应该像这样显示它们
aaa
aaa
aaa
a
:我自己移动这些资源并让图形视图管理它们。这有可能吗?
我曾经写过这样的事情,但是使用 QWidget 和 PaintEvent,自己绘制所有资产并跟踪可以连续显示多少个资产。使用 QGraphicsView 可以更简单吗?
I would like to write an asset browser using QGraphicsView. It's a little different from examples using QGraphicsView and QGraphicsItems, because I want only one scrollbar and I want items to move automatically, when the viewport size changes. For example, when viewport width is large enough to display 4 asssets, they should be displayed like this:
aaaa
aaaa
aa
but when viewport is shrinked and can only contain 3 in a row, it should display them like this:
aaa
aaa
aaa
a
I wouldn't like to have to move those asset's by myself and let the graphics view manage them all. Is it somehow possible?
I have written once such a thing, but using QWidget and paintEvent, drawing all assets myself and keeping track of how many assets can be displayed in a row. Can it be done simpler with QGraphicsView?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
QGraphicsView 支持布局。你所要做的就是实现你自己的布局管理器,继承自QGraphicsLayout。
对于您需要的布局,请查看 Qt 的 Flow Layout 示例。转换该示例将为您提供一个 QGraphicsFlowLayout。将您的 QGraphicsItems 添加到此布局并将 QGraphicsView 的布局设置为该布局,这样就可以了。
QGraphicsView supports layouts. What you have to do is implement your own layout manager, inheriting from QGraphicsLayout.
For the layout you require, take a look at the Flow Layout example of Qt. Converting that example will give you a QGraphicsFlowLayout. Add your QGraphicsItems to this layout and set your QGraphicsView's layout to that layout, and that would do the trick.
在我看来,您想要一个列表,而不是图形视图。可以设置一个列表来显示您想要的环绕的内容。请参阅拼图示例,注意列表左边的拼图。对于所展示的案例来说,设置看起来非常简单。
当然,如果您确实希望在图形视图中使用它,我想您可以向视图添加一个列表并在那里使用它。
It sounds to me you want a list, not a graphics view. A list can be set to display things wrapping around like you desire. See the puzzle example, paying attention to the list of puzzle pieces on the left. It looks pretty simple to set up for the case presented.
Of course, if you really want it in a graphics view, I suppose you could add a list to the view and use it there.
我会使用自定义布局来做到这一点。尝试创建继承自 QGraphicsLayout 的自定义布局类并管理其放置项目的方式。
I would use a custom layout to do this. Try to create your custom Layout class that inherits from QGraphicsLayout and manage the way it is placing items.