如何检测QTableWidget滚动源(代码本身/用户(滚轮)/用户(滚动条))?(How to detect QTableWidget scroll source (code itself/user

编程入门 行业动态 更新时间:2024-10-21 09:46:59
如何检测QTableWidget滚动源(代码本身/用户(滚轮)/用户(滚动条))?(How to detect QTableWidget scroll source (code itself/user (wheel)/user (scrollbar))?)

我正在使用Qt 4.8编写一个程序,它显示一个填充了文件名和文件参数的表(QTableWidget)。 首先,用户将文件添加到列表中,然后单击进程。 代码本身使用简单的进度描述更新表的内容。 我希望默认情况下自动滚动表以显示最后处理的文件,并且该代码已准备就绪。

如果我想手动滚动它,只要某些内容发生变化,就会自动滚动窗口小部件,将视口移动到最后一个元素。 如果我检测到是想要更改视图的用户,我希望能够覆盖自动滚动。

在许多终端仿真器程序中都可以看到此行为。 当添加了新行时,视图会滚动,但是当用户强制终端查看某些前一行时,终端不会尝试向下滚动。

我怎么能这样做?

解:

我创建了一个对象,它过滤我的QTableWidget处理的事件和嵌入在里面的QScrollBar。 如果我发现应该关闭自动滚动的事件我只是设置一个标志并停止滚动视图如果设置了该标志。

一切都在tableController类中实现。 以下是三种关键方法的一部分。

bool tableController::eventFilter(QObject* object, QEvent* event) { switch (event->type()) { case QEvent::KeyPress: case QEvent::KeyRelease: case QEvent::Wheel: case QEvent::MouseButtonDblClick: case QEvent::MouseButtonPress: case QEvent::MouseButtonRelease: _autoScrollEnabled = false; default: break; } return QObject::eventFilter(object, event); } void tableController::changeFile(int idx) { [...] if (_autoScrollEnabled) { QTableWidgetItem* s = _table.item(_engine.getLastProcessed(), 1); _table.scrollToItem(s); } [...] } void tableController::tableController() { [...] _autoScrollEnabled = true; _table.installEventFilter(this); _table.verticalTableScrollbar()->installEventFilter(this); [...] }

谢谢你的帮助。 我希望有人会觉得它很有用:)

I'm writing a program using Qt 4.8 that displays a table (QTableWidget) filled with filenames and file's params. First an user adds files to the list and then clicks process. The code itself updates the contents of the table with simple progress description. I want the table by default to be scrolled automatically to show the last processed file and that code is ready.

If I want to scroll it by hand the widget is being scrolled automatically as soon as something changes moving the viewport to the last element. I want to be able to override the automated scroll if I detect that it was the user who wanted to change view.

This behavior can be seen in many terminal emulator programs. When there's a new line added the view is scrolled but when user forces the terminal to see some previous lines the terminal does not try to scroll down.

How could I do that?

Solution:

I created an object which filters event processed by my QTableWidget and QScrollBar embedded inside. If I spot the event that should turn off automatic scrolling I just set a flag and stop scrolling view if that flag is set.

Everything is implemented inside tableController class. Here are parts of three crucial methods.

bool tableController::eventFilter(QObject* object, QEvent* event) { switch (event->type()) { case QEvent::KeyPress: case QEvent::KeyRelease: case QEvent::Wheel: case QEvent::MouseButtonDblClick: case QEvent::MouseButtonPress: case QEvent::MouseButtonRelease: _autoScrollEnabled = false; default: break; } return QObject::eventFilter(object, event); } void tableController::changeFile(int idx) { [...] if (_autoScrollEnabled) { QTableWidgetItem* s = _table.item(_engine.getLastProcessed(), 1); _table.scrollToItem(s); } [...] } void tableController::tableController() { [...] _autoScrollEnabled = true; _table.installEventFilter(this); _table.verticalTableScrollbar()->installEventFilter(this); [...] }

Thanks for all the help. I hope somebody will find it useful :)

最满意答案

子类QTableWidget并重载其wheelEvent 。 您可以使用提供的QWheelEvent对象的参数来确定用户是向上还是向下滚动。

然后使用一个简单的布尔标志,该标志在wheelEvent覆盖中设置(或重置)。 负责调用scrollToBottom()应该考虑这个布尔标志。

您必须找到一种方法来确定何时设置或重置该标志,例如,当用户向上滚动时始终设置它并在用户向下滚动并且当前显示的区域位于底部时重置它。

Subclass QTableWidget and overload its wheelEvent. You can use the parameters of the supplied QWheelEvent object in order to determine if the user scrolled up or down.

Then use a simple boolean flag which is set (or reset) in your wheelEvent override. The method which is responsible for calling scrollToBottom() should then consider this boolean flag.

You will have to find a way to figure out when to set or reset that flag, e.g. always set it when the user scrolls up and reset it when the user scrolls down and the currently displayed area is at the bottom.

更多推荐

本文发布于:2023-08-07 00:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1457883.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用户   滚轮   滚动条   代码   detect

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!