Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to make my QTableView show the latest appended data

How to make my QTableView show the latest appended data

Scheduled Pinned Locked Moved Solved Qt for Python
7 Posts 2 Posters 1.4k 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.
  • B Offline
    B Offline
    brookbot
    wrote on last edited by
    #1

    How to make my QTableView to follow and display the latest data row appended from a data stream unless the user is browsing older data?

    I want my app to always scroll to the last item unless the user has used the scroll bar to browse to a previous row. But start following new data again when scrolled back to the end of the data.

    I've added this to my update handler in my TableModel (PyQt5):

    @pyqtSlot(list)
    def update_data(self, data):
       global myview
       self.beginInsertRows(QModelIndex(), self.rowcnt, self.rowcnt)
       self.data.append(data)
       self.rowcnt = self.rowcnt + 1
       self.endInsertRows()
    
       ## scroll to new row
       myview.scrollToBottom()
    

    scrollToBottom() works, but how do I determine whether the last row is currently displayed or user has scrolled to some other location? eg. switch to follow mode, vs. browse mode and back.

    Seems like this could be a built-in ScrollMode, eg. Follow & Browse

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #6

      @brookbot said in How to make my QTableView show the latest appended data:

      So a simple comparison is not going to work.

      A QScrollbar has min/max values which you can compare with the current value.

      Returns true if the given row is hidden; otherwise returns false." - what does hidden mean?

      setRowHidden is very clear about this: "If hide is true row will be hidden, otherwise it will be shown."

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      B 1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        You can check the current state of the scrollbar before inserting a new row - if it's at the bottom then use scrollToBottom afterwards.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        B 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          You can check the current state of the scrollbar before inserting a new row - if it's at the bottom then use scrollToBottom afterwards.

          B Offline
          B Offline
          brookbot
          wrote on last edited by
          #3

          @Christian-Ehrlicher Granted, I figured as much, but what is the class name and function? I see only QAbstractScrollArea and no functions for accessing state of the actual scrollbar...

          Christian EhrlicherC 1 Reply Last reply
          0
          • B brookbot

            @Christian-Ehrlicher Granted, I figured as much, but what is the class name and function? I see only QAbstractScrollArea and no functions for accessing state of the actual scrollbar...

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @brookbot said in How to make my QTableView show the latest appended data:

            I see only QAbstractScrollArea

            Then you should take a look into the documentation and search for scrollbar.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            B 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @brookbot said in How to make my QTableView show the latest appended data:

              I see only QAbstractScrollArea

              Then you should take a look into the documentation and search for scrollbar.

              B Offline
              B Offline
              brookbot
              wrote on last edited by
              #5

              @Christian-Ehrlicher Ok, so looking at https://doc.qt.io/qt-5/qabstractitemview.html I found the scrollbar hidden under "List of all members, including inherited members". There seems to be a number of ways to get the value - via the "value" attribute or the signal "sliderMoved".

              setValue() seems to have no effect, only scrollToBottom() seems to work.

              The bigger issue however is that when scrolled to the bottom, the slider value is not the same as the row count and depends on the size of or number of columns displayed in the view. So a simple comparison is not going to work. eg. I can have 5 rows of data and the scrollbar value is still 0 even though I've called scrollToBottom()

              There must be a better way to do this?

              FYI - documentation can be interpreted many ways. Just because its documented does not mean its clear to everyone. For example: "isRowHidden - Returns true if the given row is hidden; otherwise returns false." - what does hidden mean? Does it mean the row is not in the current view area, obscured by another widget, or there is some other feature that allows hiding rows in a table? Its just not clear, and I can already derive the description given from the function name. What I don't know is what is meant by Hidden.

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @brookbot said in How to make my QTableView show the latest appended data:

                So a simple comparison is not going to work.

                A QScrollbar has min/max values which you can compare with the current value.

                Returns true if the given row is hidden; otherwise returns false." - what does hidden mean?

                setRowHidden is very clear about this: "If hide is true row will be hidden, otherwise it will be shown."

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                B 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @brookbot said in How to make my QTableView show the latest appended data:

                  So a simple comparison is not going to work.

                  A QScrollbar has min/max values which you can compare with the current value.

                  Returns true if the given row is hidden; otherwise returns false." - what does hidden mean?

                  setRowHidden is very clear about this: "If hide is true row will be hidden, otherwise it will be shown."

                  B Offline
                  B Offline
                  brookbot
                  wrote on last edited by
                  #7

                  @Christian-Ehrlicher Ok, thanks - comparing .value() with .maximum() works for me. The only annoy thing is that the scroll bar keeps moving even though the rows displayed are not. A bit distracting, but I expect that will go away when there is a lot of data.

                  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