1

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

2 Answers 2

0

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.

5
  • The problem is it's not only about wheel scroll but also about user pushing arrows, page up/down and also using the supplied vertical scroll bar (again, by wheel scroll or using buttons at the ends). There are so many ways of scrolling the view that I wanted to tap somewhere I can do everything at once.
    – Caladan
    Aug 23, 2012 at 7:18
  • well you can use QObject::installEventFilter() and then switch/case over QEvent::type() to handle multiple events at once.
    – Tim Meyer
    Aug 23, 2012 at 7:57
  • Ok, this makes more sense. I will try this. I guess I will have to find out what events are sent and who sends them.
    – Caladan
    Aug 23, 2012 at 12:21
  • If you install the event filter on QTableWidget, that should be enough.. you're looking specifically for WheelEvent, ScrollEvent and KeyPressEvent
    – Tim Meyer
    Aug 23, 2012 at 13:57
  • I don't know if it's only QTableWidget getting all the events or the scroll bar getting those messages too. I may need to tap in both places...
    – Caladan
    Aug 23, 2012 at 14:09
0
connect(_table->view()->verticalScrollBar(), &QAbstractSlider::actionTriggered, this, [this](int) {
    _autoScrollEnabled = false;
});
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.