Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. [solved]QAbstractListModel - update data in table view [linux]
Forum Updated to NodeBB v4.3 + New Features

[solved]QAbstractListModel - update data in table view [linux]

Scheduled Pinned Locked Moved Mobile and Embedded
29 Posts 3 Posters 12.8k 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.
  • S Offline
    S Offline
    s_z_p
    wrote on last edited by
    #1

    How to update/refresh a data when my app is running. Data in ‘file.txt’ is changing, how to display this changes ?
    I don’t want to shutdown and restart my app.

    @ class StockListModel(QtCore.QAbstractListModel):

        def __init__(self, stockdata = [], parent = None):
            QtCore.QAbstractListModel.__init__(self, parent)            
            self.__stockdata = stockdata            
     
        def rowCount(self, parent):
            return len(self.__stockdata)
     
        def data(self, index, role):        
            if role == QtCore.Qt.DisplayRole:
                row = index.row()
                value = self.__stockdata[row]
     
                return value
     
    if __name__ == '__main__':
        app = QtGui.QApplication(sys.argv)
        app.setStyle("plastique")
     
        tableView = QtGui.QTableView()      
        tableView.show()
     
        a = os.popen("cat /home/user/file.txt")
        a = a.read()
        time_variable = QtCore.QString("%s"%a)       # how to refresh when app is running ?
     
        model = StockListModel([time_variable])
     
        tableView.setModel(model)
        sys.exit(app.exec_())
    

    @

    1 Reply Last reply
    0
    • T Offline
      T Offline
      topse
      wrote on last edited by
      #2

      Reset Model and add repopulate data?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        s_z_p
        wrote on last edited by
        #3

        Should reset looks like that?, if yes how can I repopulate data?
        Code below begins in the line 16.

        @def resetItems(self,model):
        self.beginResetModel()
        self.model = model
        self.endResetModel()
        @

        1 Reply Last reply
        0
        • T Offline
          T Offline
          topse
          wrote on last edited by
          #4

          Have you made yourself acquaint with Signals and Slots?

          When is your "resetItems" being called? I am not familiar with the Python Implementation of Qt -- maybe you should use "setModel"?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            s_z_p
            wrote on last edited by
            #5

            I am just a beginner in Qt.
            Perhaps instead of "model" I shoud reset "stockdata" (add to the line 6):

            @
            def resetItems(self,stockdata):
            self.beginResetModel()
            self.__stockdata = stockdata
            self.endResetModel()
            @

            I took the example from this website:
            http://www.nullege.com/codes/show/src@o@m@omg-0.3.1@omg@models@simplelistmodel.py/42/PyQt4.QtCore.QAbstractListModel.beginResetModel/python

            1 Reply Last reply
            0
            • S Offline
              S Offline
              s_z_p
              wrote on last edited by
              #6

              There is something like " self.dataChanged.emit(index, index, ())" but I haven't found how to use reset and signal together to refresh my view yet.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                s_z_p
                wrote on last edited by
                #7

                somebody? any idea how?
                Is it possible to refresh QAbstractListModel getting data from file at all?

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

                  Hi,

                  You could use a QFileSystemWatcher so you get the information that the file has been modified and re-read it.

                  Hope it helps

                  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
                  • S Offline
                    S Offline
                    s_z_p
                    wrote on last edited by
                    #9

                    I use QFileSystemWatcher like that:
                    @
                    file_check = QtCore.QFileSystemWatcher(['/home/user/file.txt'])
                    file_check.fileChanged.connect(resetItems)@

                    "resetItems" looks like that:

                    @def resetItems(self,stockdata):
                    self.beginResetModel()
                    self.stockdata = stockdata
                    self.endResetModel()@

                    it looks fine but when I run script I got the error:

                    TypeError: resetItems() takes exactly 2 arguments (1 given)

                    Could it be mistake in QFileSystemWatcher syntax?

                    thx for reply!

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      s_z_p
                      wrote on last edited by
                      #10

                      Or problem is related how I reset model:

                      @ def resetItems(self):
                      self.beginResetModel()
                      self.__stockdata = stockdata
                      self.endResetModel()@

                      Error:
                      AttributeError: 'QString' object has no attribute 'beginResetModel'

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

                        It looks like you are mixing several things here. Why did you change your class base type ?

                        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
                        • S Offline
                          S Offline
                          s_z_p
                          wrote on last edited by
                          #12

                          @SGaist what do you mean by changing my class base type?, are you talking about 'stockdata' vs '__stockdata' ?
                          What I understand, my app is running, 'QWatcher' checks if the source file hasn't been changed if yes it runs 'resetItems'.

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            s_z_p
                            wrote on last edited by
                            #13

                            I checked and QWatcher works fine. Problem is related to function resetItems but I haven't found solution.. any idea?

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

                              @AttributeError: ‘QString’ object has no attribute ‘beginResetModel’@

                              means that you are trying to call beginResetModel on a QString class, which is wrong.

                              stockdata and __stockdata don't seem to represent the same thing, you should use more meaningful names for your variables to make your code easier to understand

                              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
                              • S Offline
                                S Offline
                                s_z_p
                                wrote on last edited by
                                #15

                                how then resetItems has to look like? in this particular case. thx

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

                                  It should be part of your subclass of QAbstractItemModel

                                  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
                                  • S Offline
                                    S Offline
                                    s_z_p
                                    wrote on last edited by
                                    #17

                                    what you have written means that this is wrong ?

                                    @class StockListModel(QtCore.QAbstractListModel):
                                    def init(self, stockdata = [], parent = None):
                                    QtCore.QAbstractListModel.init(self, parent)
                                    self.stockdata = stockdata

                                        def getItems(self):
                                           return stockdata
                                    

                                    #resetItems:
                                    def resetItems(self,stockdata):
                                    self.beginResetModel()
                                    self.stockdata = stockdata
                                    self.endResetModel()

                                        def rowCount(self, parent):
                                            return len(self.stockdata)
                                            
                                        def data(self, index, role):        
                                            if role == QtCore.Qt.DisplayRole:
                                                row = index.row()
                                                value = self.stockdata[row]
                                                return value
                                                
                                        file_check = QtCore.QFileSystemWatcher(['/home/user/Desktop/plik.txt'])
                                        file_check.fileChanged.connect(resetItems)
                                    

                                    if name == 'main':
                                    app = QtGui.QApplication(sys.argv)
                                    app.setStyle("plastique")

                                        tableView = QtGui.QTableView()      
                                        tableView.show()
                                     
                                        a = os.popen("cat /home/user/Desktop/plik.txt")
                                        a = a.read()
                                        time_variable = QtCore.QString("%s"%a)
                                     
                                        model = StockListModel([time_variable])
                                     
                                        tableView.setModel(model)
                                        sys.exit(app.exec_())@
                                    

                                    and instead of QtCore.QAbstractListModel i should use QtCore.QAbstractItemModel ? or add aditional subclass?

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

                                      Shouldn't stockdata be a list ? If so, your connection doesn't make sense

                                      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
                                      • S Offline
                                        S Offline
                                        s_z_p
                                        wrote on last edited by
                                        #19

                                        Yes it is a list but I didn't paste the whole code. There is only one cell in this code. What do you mean "connection doesn't make sense", which part?

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

                                          fileChanged gives you the path of the file that changed, not it's content, so basically you are replacing your list by a string.

                                          Also, why are you using the model/view approach if you only have one entry ?

                                          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