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

查看:60
本文介绍了如何检测 QTableWidget 滚动源(代码本身/用户(滚轮)/用户(滚动条))?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我怎么能这样做?

解决方案:

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

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.

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

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 :)

推荐答案

Subclass QTableWidget 并重载其 wheelEvent.您可以使用所提供的 QWheelEvent 对象的参数来确定是否用户向上或向下滚动.

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.

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

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.

这篇关于如何检测 QTableWidget 滚动源(代码本身/用户(滚轮)/用户(滚动条))?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆