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 13.1k 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.
  • 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
            • S Offline
              S Offline
              s_z_p
              wrote on last edited by
              #21

              Model view approach is needed for let's say full version of the script.

              I think I get your point about string. Let me check that.

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

                The signal will just tell you that the file has changed, it will not reload the content for you. You'll have to parse the file again and update stockdata with what you just read from it

                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
                  #23

                  One more thing.
                  file_check = QtCore.QFileSystemWatcher(['/home/user/Desktop/plik.txt'])
                  file_check only checks if file was changed if was:

                  file_check.fileChanged.connect(resetItems)
                  it sends signal to 'resetItems' and 'resetItems' should reload data, true?
                  and I have to find a way how accept the string parameter emitted by fileChanged..

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

                    Is it better ? but I have got error like that:
                    AttributeError: 'StockListModel' object has no attribute 'beginResetModel'

                    @def init(self, stockdata = [], parent = None):
                    QtCore.QAbstractListModel.init(self, parent)
                    self.stockdata = stockdata
                    self.file_check = QtCore.QFileSystemWatcher(['/home/user/Desktop/file.txt'])
                    self.file_check.fileChanged.connect(self.resetItems)

                        def getItems(self):
                           return stockdata
                    
                        @QtCore.pyqtSlot(str)
                        def resetItems(self, path):
                           self.beginResetModel()
                           ....
                    

                    @

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

                      You have to the data reloading yourself. Like in your main function.

                      As for beginResetModel I don't know why it's saying that

                      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
                        #26

                        I cannot find a solution for this error.
                        @SGaist, writing "reloading data" do you mean file with data?

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

                          Yes, that's what I mean. On you program startup you are reading that file and put it's content in the stockdata variable. When the QFileSystemWatcher triggers, you must do that parsing again.

                          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
                            #28

                            Finally solved. 'beginResetModel', 'end...' and 'self.stockdata' in 'resetItems' are not needed. It can be done without it. Thank you for support.

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

                              You're welcome !

                              Since you have it working now, please update the thread title prepending [solved] so that other forum users may know a solution has been found :)

                              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