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. Scroll QTableView as soon as model changes
Forum Updated to NodeBB v4.3 + New Features

Scroll QTableView as soon as model changes

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 1.3k Views 1 Watching
  • 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.
  • H Offline
    H Offline
    Herman Nordberg
    wrote on last edited by Herman Nordberg
    #1

    I have a QTableView subclass that displays a QStandardItemModel subclass, I want to scroll to a certain row as soon as the model updates.

    class OrderBook : public QStandardItemModel
    ...
    
    void OrderBook::updateModel() {
    	std::pair<double, double> minMax = getMinMaxPrice();
    	if (minMax.first == -1 && minMax.second == -1) {
    		setRowCount(0);
    		return;
    	}
    	int numRows = calculateNumRows(minMax.first, minMax.second);
    	setRowCount(numRows);
    	resetCells();
    	setPrices(minMax.second);
    	setOtherOrders(minMax.second);
    	setOwnOrders(minMax.second);
    
    	emit midPointChanged(calculateMidPoint());
    }
    
    class OrderBookView : public QTableView
    ...
    
    void OrderBookView::onMidPointChanged(double midPoint) {
    	//repaint();
    	size_t row = findClosestRow(midPoint);
    	QModelIndex index = model()->index(row, 0);
    	scrollTo(index, QAbstractItemView::ScrollHint::PositionAtCenter);
    }
    

    The slots and signals are connected and the slot is called, the model also has the correct row count, but after rows were added it does not scroll the first time. It will scroll the second time the model is updated, probably because the rows are already displayed at that point, only their values change.
    7b99880676109f26635c773b2524a272.png

    As you can see the ModelIndex is also valid. If I call repaint before scrolling, it works, but is it safe to do so? Are there better ways to do it?

    JonBJ 1 Reply Last reply
    0
    • H Herman Nordberg

      I have a QTableView subclass that displays a QStandardItemModel subclass, I want to scroll to a certain row as soon as the model updates.

      class OrderBook : public QStandardItemModel
      ...
      
      void OrderBook::updateModel() {
      	std::pair<double, double> minMax = getMinMaxPrice();
      	if (minMax.first == -1 && minMax.second == -1) {
      		setRowCount(0);
      		return;
      	}
      	int numRows = calculateNumRows(minMax.first, minMax.second);
      	setRowCount(numRows);
      	resetCells();
      	setPrices(minMax.second);
      	setOtherOrders(minMax.second);
      	setOwnOrders(minMax.second);
      
      	emit midPointChanged(calculateMidPoint());
      }
      
      class OrderBookView : public QTableView
      ...
      
      void OrderBookView::onMidPointChanged(double midPoint) {
      	//repaint();
      	size_t row = findClosestRow(midPoint);
      	QModelIndex index = model()->index(row, 0);
      	scrollTo(index, QAbstractItemView::ScrollHint::PositionAtCenter);
      }
      

      The slots and signals are connected and the slot is called, the model also has the correct row count, but after rows were added it does not scroll the first time. It will scroll the second time the model is updated, probably because the rows are already displayed at that point, only their values change.
      7b99880676109f26635c773b2524a272.png

      As you can see the ModelIndex is also valid. If I call repaint before scrolling, it works, but is it safe to do so? Are there better ways to do it?

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @Herman-Nordberg
      This is purely a guess, may make no difference, but worth a try?

      You seem to be using setRowCount() to increase/decrease the number of rows. (Following talks about removing rows; applies just as much to inserting rows.) Normally we call removeRows(), and through that beginRemoveRows() and importantly the rowsAboutToBeRemoved() signal. I believe you will find views use that:

      Note: This function emits the rowsAboutToBeRemoved() signal which connected views (or proxies) must handle before the data is removed. Otherwise, the views may end up in an invalid state.

      Does it make any difference to your view behaviour if you (at least temporarily) go via removeRows(), even if just to test?

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Herman Nordberg
        wrote on last edited by Herman Nordberg
        #3

        I have just tried it, but the behaviour is still the same.

        After adding first row
        1dacd7bdee96c8520b84392cf6dfa86e.png
        After adding more rows, the rows are diplayed, but it doesn't scroll
        da8d5ee1b42cda5fe2d6732058f3bdd4.png
        After adding another order the row cout stays the same, and it scrolls
        bfe296ce72f2fd9681a84cb03fcd59f0.png

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

          Hi,

          Did you check that the index returned by findClosestRow is indeed valid ?

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

          H 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            Did you check that the index returned by findClosestRow is indeed valid ?

            H Offline
            H Offline
            Herman Nordberg
            wrote on last edited by
            #5

            @SGaist said in Scroll QTableView as soon as model changes:

            Hi,

            Did you check that the index returned by findClosestRow is indeed valid ?

            I have added a check now, yes it's valid.

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

              And is it the index you are expecting ?

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

              1 Reply Last reply
              0
              • H Offline
                H Offline
                Herman Nordberg
                wrote on last edited by
                #7

                Yes, the model at that point has 100 rows and the midpoint is row 50, which is also the row in the ModelIndex.

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

                  For the sake of testing, can you use a queued connection ?

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

                  1 Reply Last reply
                  0
                  • H Offline
                    H Offline
                    Herman Nordberg
                    wrote on last edited by
                    #9

                    The same thing happens with a queued connection.

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

                      Which version of Qt are you running ?

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

                      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