Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to scrollTo(index) if view was hidden
QtWS25 Last Chance

How to scrollTo(index) if view was hidden

Scheduled Pinned Locked Moved Solved General and Desktop
scrolltoqlistviewsetvisible
10 Posts 3 Posters 4.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rory_1
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      As a possible workaround, use a single shot QTimer to trigger the scrollTo. Start the Timer after you shown your grid view.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      R 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        As a possible workaround, use a single shot QTimer to trigger the scrollTo. Start the Timer after you shown your grid view.

        R Offline
        R Offline
        Rory_1
        wrote on last edited by
        #3

        @SGaist That worked. Thanks!!! I sure appreciated your efforts on this forum.

        R 1 Reply Last reply
        0
        • R Rory_1

          @SGaist That worked. Thanks!!! I sure appreciated your efforts on this forum.

          R Offline
          R Offline
          Rory_1
          wrote on last edited by
          #4

          BTW, it takes quite a delay in my case: 400ms. Seems like a weak link. I'll research, but is there a signal emitted when the scrollbars are ready to go?

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            R 2 Replies Last reply
            0
            • SGaistS SGaist

              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.

              R Offline
              R Offline
              Rory_1
              wrote on last edited by
              #6

              @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()));
              
              1 Reply Last reply
              0
              • SGaistS SGaist

                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.

                R Offline
                R Offline
                Rory_1
                wrote on last edited by
                #7

                @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.

                JonBJ R 2 Replies Last reply
                0
                • R Rory_1

                  @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.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @Rory_1
                  If you need to avoid the multiple QPaintEvent->scrollTo calls, and only do so after the last one, in your first paint event handler set off a timer which will do the scrollTo after a bit (and recognise that the timer is already running/update it on subsequent paint events).

                  1 Reply Last reply
                  0
                  • R Rory_1

                    @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.

                    R Offline
                    R Offline
                    Rory_1
                    wrote on last edited by
                    #9

                    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.

                    R 1 Reply Last reply
                    0
                    • R Rory_1

                      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.

                      R Offline
                      R Offline
                      Rory_1
                      wrote on last edited by
                      #10

                      I've found a satisfactory solution. I found out the horizontalScrollBar->maximum changes with each successive paint event. I can calculate what the maximum should be and then determine which is the last paint event.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved