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. QTreeView empty

QTreeView empty

Scheduled Pinned Locked Moved Solved General and Desktop
qtreeviewqabstractitemmo
6 Posts 3 Posters 77 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.
  • M Offline
    M Offline
    MisterOrange
    wrote last edited by
    #1

    Tried to implement a custom model from QAbstractItemModel but whenever I try to display the data in a QTreeView it's just empty.
    QAbstractItemModelTester doesn't seems to detect anything and manually getting data like this model->data(model->index(row, column), 0).toString() works without any problems as well.

    Model implementation: https://pastebin.com/w9TWaTCb
    TreeItem (Components) implementation: https://pastebin.com/vqjYThzJ

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

      And how/where do you use them?

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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MisterOrange
        wrote last edited by
        #3

        This code is being invoked after a button press:

        JsonProcessor processor("C:/Users/admin/source/qt-repos/Jser/Inputs/1.json");
        
        JsonModel *model = processor.getModel();
        
        QAbstractItemModelTester *tester = new QAbstractItemModelTester(model, QAbstractItemModelTester::FailureReportingMode::Fatal);
        
        ui->treeView->setModel(model);
        

        The JsonProcessor class creates the TreeView items based on a json file
        JsonProcessor implementation: https://pastebin.com/fThF73PF

        Christian EhrlicherC 1 Reply Last reply
        0
        • C Offline
          C Offline
          CassD
          wrote last edited by CassD
          #4

          Hi

          as far as I remember, QAIMT only performs type validation checks. this means, for a particular role, it verifies that the data returned for that role is convertible to the datatype that the default delegates expects.
          In other words, it just contents itself verifying that data for display role can be converted to QString, that background role can be converted to QBrush, etc etc ... As far as I remember, an empty QVariant passes the test.
          But it's been a while I haven't used Qt's predefined roles so I might be wrong.

          What you should try is display to console the first cell for root item, and repeat the process for a few cells, first in root item, then get first cell of first child :

          QModelIndex firstCell = model->index(0, 0, QModelIndex()) ;
          qDebug() << data (firstCell, Qt::DisplayRole) ;
          

          So of course Qt::DisplayRole is not needed as it's already the default value.
          The empty QModelIndex isn't either for accessing values in root item, but you will need of course to specify a parent once you try to access a cell within a child node.

          This for sure will tell you if you are actually getting the value that you are expecting.

          I have never worked with trees having multiple columns, I haven't digged much into it, but this seems to me a little suspicious :

          int JsonModel::rowCount(const QModelIndex &parent) const {
              // Only column 0 has children
              if (parent.column() > 0)
                  return 0;
          

          at multiple times, you get the parent of the currentItem, to get the child of the parent next. That seems to me a little weird also.

          1 Reply Last reply
          0
          • M MisterOrange

            This code is being invoked after a button press:

            JsonProcessor processor("C:/Users/admin/source/qt-repos/Jser/Inputs/1.json");
            
            JsonModel *model = processor.getModel();
            
            QAbstractItemModelTester *tester = new QAbstractItemModelTester(model, QAbstractItemModelTester::FailureReportingMode::Fatal);
            
            ui->treeView->setModel(model);
            

            The JsonProcessor class creates the TreeView items based on a json file
            JsonProcessor implementation: https://pastebin.com/fThF73PF

            Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote last edited by
            #5

            @MisterOrange said in QTreeView empty:

            JsonProcessor processor("C:/Users/admin/source/qt-repos/Jser/Inputs/1.json");

            JsonModel *model = processor.getModel();

            Basic c++ - how long does the JsonProcessor instance (and therefore the model) live?

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

            1 Reply Last reply
            2
            • M Offline
              M Offline
              MisterOrange
              wrote last edited by
              #6

              @Christian-Ehrlicher yep that was it. Can't believe I missed that for so long

              thank you both for the help

              1 Reply Last reply
              0
              • M MisterOrange has marked this topic as solved

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved