How to scrollTo(index) if view was hidden
-
In my image viewer app I have a QStandardItemModel (dm) with a QSortFilterProxyModel (sf). I have two instances of a QListView of the model: (thumbView) and (gridView). The thumbView resides in a QDockWidget (thumbDock) while the gridView is located in a QStackedLayout in the central widget. The central widget QStackedLayout also has several other tabs, so the gridView is often not visible.
Central widget:
centralLabel = new QLabel; centralLayout->addWidget(centralLabel); // first time open program tips centralLayout->addWidget(imageView); centralLayout->addWidget(compareImages); centralLayout->addWidget(tableView); centralLayout->addWidget(gridView); centralLayout->setCurrentIndex(0); centralWidget->setLayout(centralLayout); setCentralWidget(centralWidget);
The views share the same selection model:
// set a common selection model for all views selectionModel = new QItemSelectionModel(dm->sf); thumbView->setSelectionModel(selectionModel); gridView->setSelectionModel(selectionModel);
When both the thumbView and gridView are visible all changes in selection are mirrored in both views. However, if scrolling is required, and the gridView is not visible, then when I switch to the gridView it does not scroll to the currentIndex. Here is the code to display gridView:
void MW::gridDisplay() { // show gridView in central widget centralLayout->setCurrentIndex(GridTab); // hide thumbView when gridView thumbDock->setVisible(false); // no affect on scrolling gridView->setVisible(true); // no difference to scroll gridView->setFocus(); // no difference to scroll qDebug() << "gridView->currentIndex()" << gridView->currentIndex() << "thumbView->currentIndex()" << thumbView->currentIndex(); // scrollTo not working if gridView was hidden gridView->scrollTo(gridView->currentIndex(), gridView->ScrollHint::PositionAtCenter); }
Example qDebug() output:
gridView->currentIndex() QModelIndex(5,0,0x7fee1398cc70,SortFilter(0x7fee13989c50))
thumbView->currentIndex() QModelIndex(5,0,0x7fee1398cc70,SortFilter(0x7fee13989c50))So the views are in sync but the scrollTo is not happening if gridView was not visible.
I have tried:
gridView->viewport()->update(); gridView->update(); gridView->updateGeometry();
Any suggestions would be much appreciated.
-
Hi,
As a possible workaround, use a single shot QTimer to trigger the scrollTo. Start the Timer after you shown your grid view.
-
It's indeed a bit long. One possible way would be to add an event filter and check to the show event of the object of interest.
-
@SGaist said in How to scrollTo(index) if view was hidden:
show event
Thanks - I'll give that a try - it looks more promising than this.
What I was starting to work on was setting a flag just before the singleShot, and this connection:
connect(gridView->verticalScrollBar(), SIGNAL(rangeChanged(int,int)), gridView, SLOT(scrollRangeChanged()));
-
@SGaist Reporting back. The following events are triggered when the gridView is activated (MW = MainWindow):
- MW events QChildEvent(ChildAdded, QAbstractAnimation(0x7fc2bdd30330)) QDockWidget(0x7fc2b7646eb0, name = "thumbDock")
- ThumbView events QResizeEvent(12, 487, non-spontaneous) QScrollBar(0x7fc2bc51df00, name = "VerticalScrollBar")
- ThumbView events QShowEvent(Show, 0x7ffeea14f828) QScrollBar(0x7fc2bc51df00, name = "VerticalScrollBar")
- MW events QHideEvent(Hide, 0x7ffeea14fb00) QDockWidget(0x7fc2b7646eb0, name = "thumbDock")
- ThumbView events QMoveEvent(0,0, non-spontaneous) QScrollBar(0x7fc2bc51df00, name = "VerticalScrollBar")
- ThumbView events QResizeEvent(12, 612, non-spontaneous) QScrollBar(0x7fc2bc51df00, name = "VerticalScrollBar")
- MW events QEvent(HideToParent, 0x7ffeea14fb80) QDockWidget(0x7fc2b7646eb0, name = "thumbDock")
- MW events QChildEvent(ChildRemoved, QObject(0x7fc2bdd30330)) QDockWidget(0x7fc2b7646eb0, name = "thumbDock")
- ThumbView events QEvent(UpdateLater, 0x7fc2be412d40) QScrollBar(0x7fc2bc51df00, name = "VerticalScrollBar")
- ThumbView events QPaintEvent(QRegion(0,0 12x612)) QScrollBar(0x7fc2bc51df00, name = "VerticalScrollBar")
- ThumbView events QPaintEvent(QRegion(0,0 12x612)) QScrollBar(0x7fc2bc51df00, name = "VerticalScrollBar")
- ThumbView events QEvent(UpdateLater, 0x7fc2bdc56d90) QScrollBar(0x7fc2bc51df00, name = "VerticalScrollBar")
- ThumbView events QPaintEvent(QRegion(0,0 12x612)) QScrollBar(0x7fc2bc51df00, name = "VerticalScrollBar")
By filtering on the paint event I was able to get it to work. Unfortunately the paint event is called three times, and it works on the third one, so the scrollTo also gets called three times. Not a big deal and much better than guessing at the delay for the singleShot timer.
-
@Rory_1
If you need to avoid the multipleQPaintEvent
->scrollTo
calls, and only do so after the last one, in your first paint event handler set off a timer which will do thescrollTo
after a bit (and recognise that the timer is already running/update it on subsequent paint events). -
So, I've marked this as unsolved again because the current solution is a real kludge. I have a QListView subclass that shows a data model with up to 3000+ image icons. If I scroll to the end in a visible view and then switch to another view that was hidden (in the QStackWidget or a hidden QDockWidget) it can take 30-40 scrollbar paint events before the view is rebuilt. The view shows each paint event update so it scrolls until the final paint event in quite a jerky manner.
Any ideas or suggestions greatly appreciated.