Qt 自定义小部件如何通知 ScrollArea 父级有关视图更改的信息
我正在编写一个图像查看器作为自定义 Qt 小部件(请参阅: https://github.com/dov/Qviv< /a>),我现在陷入了如何让我的小部件通知父级 QScrollArea 视口中的更改,从而告诉它移动滚动条的问题。例如,如果图像查看器因按键而改变缩放系数,则滚动条需要改变其页面大小。
一种方法是让小部件显式检查父级是否是 QScrollArea,然后显式调用其方法以通知它任何更改。
当然,我还需要将 ScrollArea 的更改连接到图像的内部视图,但这是另一个问题。我需要削减无限递归,其中小部件报告对滚动条的更改,滚动条报告对小部件的更改等。
编辑星期三 20:15 (GMT/UTC) 试图向 Vjo 和我自己澄清我需要什么。
我想要实现的相当于一个 Gtk 小部件,它被分配了一对连接到水平和垂直滚动条的 GtkAdjustment。在我的小部件 GtkImageViewer(QvivImageViewer 所基于的)中,每当我由于某些内部事件(例如按键)而更改视图时,我都会更新 GtkAdjustment。滚动条与此类更改相关并相应更新。 GtkImageViewer 还侦听 GtkAdjustment 更改,因此如果用户滚动滚动条,GtkImageViewer 会使用此信息进行更新并可以更改其视图。我的问题是 Qt 中是否有类似 GtkAdjustment 的东西可以连接到进行更改和更新,在这种情况下更新将传播到所有侦听器?
因此,我不希望 ScrollArea 成为 QvivImageViewer 的一部分,但如果用户将 QvivImageViewer 放置在 ScrollArea 中,我希望与其进行双向通信,以便滚动条反映小部件的内部状态。
I'm writing an image viewer as a custom Qt widget (see: https://github.com/dov/Qviv) and I now got stuck on the question of how to make my widget notify a parent QScrollArea of changes in the view port, and thus to tell it to move the scrollbars. E.g. if the image viewer changes the zoom factor as the result of a keypress then the scrollbars need to change their page size.
One way of doing it would be to have the widget explicitly check if the parent is a QScrollArea and then make an explicit call to its methods to notify it on any changes.
Of course I also need to connect the changes of the ScrollArea to the internal view of the image, but that is a different question. And I need to cut the infinite recursion where the widget reports changes to the scrollbar that report changes to the widget etc.
Edit 20:15 Wednesday (GMT/UTC) trying to clarify to Vjo and myself what I need.
What I am trying to achieve is the equivalent of a Gtk widget that has been assigned a pair of GtkAdjustment's that are connected to a horizontal and vertical scrollbar. In my widget GtkImageViewer, that QvivImageViewer is based on, whenever I change the view due to some internal event (e.g. a keypress) I update the GtkAdjustment's. The scrollbars are connected to such changes and are update accordingly. GtkImageViewer also listens to the GtkAdjustment changes, and thus if the user scrolls the scrollbars, the GtkImageViewer is updated with this information and can change its view. My question is whether there is anything similar to GtkAdjustment in Qt that you can connect to for changes, and update in which case the update will be propagated to all the listeners?
Thus I don't expect the ScrollArea to be part of QvivImageViewer, but if the user has placed QvivImageViewer within a ScrollArea, I want bidirectional communication with it so that the scrollbars reflect the internal state of the widget.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的方法是将
QResizeEvent
事件从您的 widget 对象发送到 QScrollArea 对象。The simplest is to send the
QResizeEvent
event from your widget object to the QScrollArea object.我最终下载了 Qt 源代码并研究了 QTextEdit 是如何做到的。我发现 QTextEdit 继承了 QAbstractScrollArea 本身,因此滚动区域和滚动条是小部件的一部分。这与 Gtk 不同,Gtk 通过其用于指示滚动条和小部件之间变化的 GtkAdjustment 使用更高级别的抽象。 Qt 模型更简单,这就是我在我的小部件中实现它的方式。
I finally downloaded the Qt sources and investigated how
QTextEdit
does it. What I found is thatQTextEdit
inherits theQAbstractScrollArea
on its own, and thus the scroll area and the scrollbars are part of the widget. This is different from Gtk, which uses a higher level of abstraction, through itsGtkAdjustment
's that are used to signal changes between the scrollbars and the widget. The Qt model is simpler and this is the way that I will implement it in my widget.已经有一段时间了,但我遇到了同样的问题。
如果您愿意,您可以继承
QAbstractScrollArea
,但QScrollArea
也可以工作。您的自定义内部小部件(即您正在滚动的小部件)在其大小发生变化时应该执行以下操作:
我错过了
adjustSize()
调用,并且没有它QScrollArea
将忽略内部小部件的尺寸更改。It's been a while, but I ran across this same issue.
You can inherit
QAbstractScrollArea
if you'd like, butQScrollArea
will work as well.Your custom inner widget (i.e. the one that you are scrolling), should do the following when its size changes:
I was missing the
adjustSize()
call, and without it theQScrollArea
will ignore size changes of the internal widget.