Qt 自定义小部件如何通知 ScrollArea 父级有关视图更改的信息

发布于 2024-11-08 07:15:04 字数 876 浏览 0 评论 0原文

我正在编写一个图像查看器作为自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尛丟丟 2024-11-15 07:15:04

最简单的方法是将 QResizeEvent 事件从您的 widget 对象发送到 QScrollArea 对象。

The simplest is to send the QResizeEvent event from your widget object to the QScrollArea object.

一抹微笑 2024-11-15 07:15:04

我最终下载了 Qt 源代码并研究了 QTextEdit 是如何做到的。我发现 QTextEdit 继承了 QAbstractScrollArea 本身,因此滚动区域和滚动条是小部件的一部分。这与 Gtk 不同,Gtk 通过其用于指示滚动条和小部件之间变化的 GtkAdjustment 使用更高级别的抽象。 Qt 模型更简单,这就是我在我的小部件中实现它的方式。

I finally downloaded the Qt sources and investigated how QTextEdit does it. What I found is that QTextEdit inherits the QAbstractScrollArea 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 its GtkAdjustment'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.

绝對不後悔。 2024-11-15 07:15:04

已经有一段时间了,但我遇到了同样的问题。

如果您愿意,您可以继承QAbstractScrollArea,但QScrollArea也可以工作。

您的自定义内部小部件(即您正在滚动的小部件)在其大小发生变化时应该执行以下操作:

void MyCustomControl::resize_me() {
  // recompute internal data such that sizeHint() returns the new size
  ...
  updateGeometry();
  adjustSize();
}

QSize MyCustomControl::sizeHint() {
  return ... ; // Return my internally computed size.
}

我错过了 adjustSize() 调用,并且没有它 QScrollArea 将忽略内部小部件的尺寸更改。

It's been a while, but I ran across this same issue.

You can inherit QAbstractScrollArea if you'd like, but QScrollArea will work as well.

Your custom inner widget (i.e. the one that you are scrolling), should do the following when its size changes:

void MyCustomControl::resize_me() {
  // recompute internal data such that sizeHint() returns the new size
  ...
  updateGeometry();
  adjustSize();
}

QSize MyCustomControl::sizeHint() {
  return ... ; // Return my internally computed size.
}

I was missing the adjustSize() call, and without it the QScrollArea will ignore size changes of the internal widget.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文