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. update QTableView from QAbstractTableModel with dataChanged()???

update QTableView from QAbstractTableModel with dataChanged()???

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 15.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.
  • D Offline
    D Offline
    ddelta
    wrote on last edited by p3c0
    #1

    I have a QTableView and i have set as its model a class inherited from QAbstractTableModel. I have implemented the proper rowCount(),columnCount(),data() virtual methods that are need for a proper TableModel.

    So my question is this. When the data in the model changes how can I tell the QTableView to update itself?

    I looking through documentation is found that i should emit the signal dataChanged()... but i dont see what slot on the table view i should connect this to. I dont even see a slot on tableview that has the same parameters..

    here is the method that i emit data changed in

    void signalTapSignalAggregator::updateFinished(){
         QModelIndex topLeft = index(0, 0);
         QModelIndex bottomRight = index(numberOfRow, listOfSignals.size());
         beginResetModel();
         //emit  layoutAboutToBeChanged();
         emit layoutChanged();
         emit dataChanged(topLeft,bottomRight);
    }
    

    now after i emit layoutChanged() it seems that the tableview update the number of columns that are needed but there is no data in them.

    i have set a break point on data() method of the model in hopes that I will see the QTableView attempt to read the new data but it never fires after i call dataChanged()

    So how do i connect the dataChanged() signal to the QTableView????

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

      Hi,

      You don't have to, it's internally done.

      Why are you calling beginResetModel ? You are not reseting it and if you call any of the beginXXX method you also have to call the matching endXXX method.

      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
      • D Offline
        D Offline
        ddelta
        wrote on last edited by
        #3

        i dont know why i was calling dataChanged() to be honest... i was flailing around trying to get the table to update and i left this in there...

        Ok so they are connected internally.. that is kinda what i suspected, but its good to know for sure.

        So after the the dataChanged() signal is emit what is the TableView supposed to do in response? I was expecting the TableView to try to use the data() method in the model to read that updated data, bit its not calling this method after I emit dataChanged()...

        thanks for you reply.

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

          Did you check that bottomRight is a valid index ?

          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
          1
          • D Offline
            D Offline
            ddelta
            wrote on last edited by
            #5

            i didn't explicitly do this, but i did try setting the bottom right to (1,1) thinking that this would be a valid index but still no change...

            I was just looking at the address book example

            https://doc.qt.io/qt-5/qtwidgets-itemviews-addressbook-example.html

            and i noticed that they implement they setRows,setData functions and this is where the dataChanged() signal is emitted....

            in my example I am not doing this...

            my reasoning was that i dont need the model to be "editable" from the view... rather the data in the model is initialed once the user imports a file...

            so my question is this... do i need to implement the setData and setRow methods... een though i dont want the model to be editable from the view... rather i want it to change based on some external source???

            i think the ansewr is yes and i need to go do that....

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

              setData is used when you edit your model through a view so no, you don't need to implement it if your view is going to be read-only.

              You have to emit dataChanged when you modify the content of your model be it through setData or other means.

              If you change the geometry of the model you have to call the beginXXX/endXXX before emitting dataChanged.

              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
              2
              • D Offline
                D Offline
                ddelta
                wrote on last edited by
                #7

                hmm. that is good to know.

                i am chaning the geoemetries.... that is probably what i am doing wrong... what function are the XXX refering to? the beginInsertRows()?? beginInsertColumns()???

                i think i was naively assuming that as long as i implemented rowCount() and columnCount() i would be ok

                but even if i did that shouldn't the view attempt to ready new data from the model through the data() function after i emit data dataChanged()???

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

                  rowCount and columnCount are only getters. You have to notify the view that something is changing, that's the use of beginInsertXXX/endInsertXXX. You notify the view that e.g. the row count is about to change, then you do the actual changes and at the end you notify the view that it's OK for it to refresh itself with the new content.

                  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