Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Request] TreeView C++ model to qml example
QtWS25 Last Chance

[Request] TreeView C++ model to qml example

Scheduled Pinned Locked Moved QML and Qt Quick
treeviewtreeviewexampleqml
4 Posts 2 Posters 6.3k 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.
  • P Offline
    P Offline
    Pheelbert
    wrote on 18 Jul 2015, 16:47 last edited by Pheelbert
    #1

    I found this example for creating a tree model: http://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html

    The issue is that using the model in the qml doesn't display anything, and creating the QTreeView instead DOES show it correctly..

    main.cpp (without main def):

    QGuiApplication app(argc, argv);
    QQmlEngine engine;
    
    QString data = ...
    MyTreeModel myTreeModel = MyTreeModel(data);
    engine.rootContext()->setContextProperty("MyTreeModel", &myTreeModel);
    
    QQmlComponent component(&engine, QUrl("qrc:/main.qml"));
    component.create();
    return app.exec();
    

    main.qml:

    ApplicationWindow {
        title: "TestWindow"
        width: 640
        height: 480
        visible: true
    
        TreeView {
            id: treeView
            anchors.fill: parent
            model: MyTreeModel
        }
    }
    

    If instead of executing the qml I do this, it shows correctly (same model instantiation beforehand):

    [...]
    QApplication app(argc, argv);
    QTreeView tree;
    tree.setModel(&myTreeModel);
    tree.show();
    
    P 1 Reply Last reply 18 Jul 2015, 17:22
    0
    • P Pheelbert
      18 Jul 2015, 16:47

      I found this example for creating a tree model: http://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html

      The issue is that using the model in the qml doesn't display anything, and creating the QTreeView instead DOES show it correctly..

      main.cpp (without main def):

      QGuiApplication app(argc, argv);
      QQmlEngine engine;
      
      QString data = ...
      MyTreeModel myTreeModel = MyTreeModel(data);
      engine.rootContext()->setContextProperty("MyTreeModel", &myTreeModel);
      
      QQmlComponent component(&engine, QUrl("qrc:/main.qml"));
      component.create();
      return app.exec();
      

      main.qml:

      ApplicationWindow {
          title: "TestWindow"
          width: 640
          height: 480
          visible: true
      
          TreeView {
              id: treeView
              anchors.fill: parent
              model: MyTreeModel
          }
      }
      

      If instead of executing the qml I do this, it shows correctly (same model instantiation beforehand):

      [...]
      QApplication app(argc, argv);
      QTreeView tree;
      tree.setModel(&myTreeModel);
      tree.show();
      
      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 18 Jul 2015, 17:22 last edited by p3c0
      #2

      Hi @Pheelbert,
      I think you may have missed re-implementing roleNames function in the model. QML views rely on these. Without them the model doesn't understand what to return. I had explained a similar one here. So basically you have to add the following in C++ model:

      • re-implement roleNames
      • define the role names
      QHash<int, QByteArray> MyModel::roleNames() const
      {
          QHash<int, QByteArray> roles;
          roles[TitleRole] = "Title";  //these should match exactly with that in QML
          roles[SummaryRole] = "Summary"; //these should match exactly with that in QML
          return roles;
      }
      
      enum TreeRoles {
          TitleRole = Qt::UserRole + 10,
          SummaryRole = Qt::UserRole + 11
      };
      

      Apart from that I hope you have initialized the model and set it as a context Property to access it from QML.
      For eg:
      in main.cpp or any where as per you need

      TreeModel model;
      
      QQmlApplicationEngine engine;
      engine.rootContext()->setContextProperty("myModel",&model);
      engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); //access myModel in QML
      

      157

      P 1 Reply Last reply 18 Jul 2015, 17:41
      2
      • P p3c0
        18 Jul 2015, 17:22

        Hi @Pheelbert,
        I think you may have missed re-implementing roleNames function in the model. QML views rely on these. Without them the model doesn't understand what to return. I had explained a similar one here. So basically you have to add the following in C++ model:

        • re-implement roleNames
        • define the role names
        QHash<int, QByteArray> MyModel::roleNames() const
        {
            QHash<int, QByteArray> roles;
            roles[TitleRole] = "Title";  //these should match exactly with that in QML
            roles[SummaryRole] = "Summary"; //these should match exactly with that in QML
            return roles;
        }
        
        enum TreeRoles {
            TitleRole = Qt::UserRole + 10,
            SummaryRole = Qt::UserRole + 11
        };
        

        Apart from that I hope you have initialized the model and set it as a context Property to access it from QML.
        For eg:
        in main.cpp or any where as per you need

        TreeModel model;
        
        QQmlApplicationEngine engine;
        engine.rootContext()->setContextProperty("myModel",&model);
        engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); //access myModel in QML
        
        P Offline
        P Offline
        Pheelbert
        wrote on 18 Jul 2015, 17:41 last edited by Pheelbert
        #3

        @p3c0 Thank you so much, I also had to change to this too afterwards (removed the check if role != Qt.DisplayRole ):

        QVariant MyTreeModel::data(const QModelIndex& index, int role) const {
            if (!index.isValid())
                return QVariant();
        
            MyTreeItem* item = static_cast<MyTreeItem*>(index.internalPointer());
        
            switch (role) {
            case TitleRole:
                return item->data(0);
            case SummaryRole:
                return item->data(1);
            default:
                return QVariant();
            }
        }
        

        Also, there's a "root item" that I would much rather not have; not a big deal I'll find a way work around that!

        P 1 Reply Last reply 18 Jul 2015, 17:58
        1
        • P Pheelbert
          18 Jul 2015, 17:41

          @p3c0 Thank you so much, I also had to change to this too afterwards (removed the check if role != Qt.DisplayRole ):

          QVariant MyTreeModel::data(const QModelIndex& index, int role) const {
              if (!index.isValid())
                  return QVariant();
          
              MyTreeItem* item = static_cast<MyTreeItem*>(index.internalPointer());
          
              switch (role) {
              case TitleRole:
                  return item->data(0);
              case SummaryRole:
                  return item->data(1);
              default:
                  return QVariant();
              }
          }
          

          Also, there's a "root item" that I would much rather not have; not a big deal I'll find a way work around that!

          P Offline
          P Offline
          p3c0
          Moderators
          wrote on 18 Jul 2015, 17:58 last edited by
          #4

          @Pheelbert Yes missed the mentioning of data() here.
          You're Welcome :-) Happy Coding.

          157

          1 Reply Last reply
          0

          2/4

          18 Jul 2015, 17:22

          • Login

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