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. Accessing QAbstractItemModel fields in QML
Forum Updated to NodeBB v4.3 + New Features

Accessing QAbstractItemModel fields in QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 952 Views 2 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.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by ODБOï
    #1

    hi,
    I am trying to access model (QAbstractItemModel ) fields inside the itemDelegate of a QML TreeView (QtQuick.Controls 1.4)

    I'm able to show the field i need using TableViewColumn and implementing roleNames() method

    TableViewColumn {
               title: "isSub"
               role: "sub"
               width: 100
           }
    
    QHash<int, QByteArray> OpcUaModel::roleNames() const
    {
            QHash<int, QByteArray> roles;
            roles[NodeSubActive] = "sub";
            return roles;
    }
    

    this works as expected, it shows the value of my sub variable.

    My problem is that I don't know how to access to that variable from somwhere else like :

    itemDelegate: Text {
               text: styleData.value
               color: myUaModel.sub   ? "blue" : "red"
              // or   color: styleData.sub  ? "blue" : "red"
           }
    

    Can someone tell how this should be done please ?

    Thank you

    raven-worxR 1 Reply Last reply
    0
    • ODБOïO ODБOï

      hi,
      I am trying to access model (QAbstractItemModel ) fields inside the itemDelegate of a QML TreeView (QtQuick.Controls 1.4)

      I'm able to show the field i need using TableViewColumn and implementing roleNames() method

      TableViewColumn {
                 title: "isSub"
                 role: "sub"
                 width: 100
             }
      
      QHash<int, QByteArray> OpcUaModel::roleNames() const
      {
              QHash<int, QByteArray> roles;
              roles[NodeSubActive] = "sub";
              return roles;
      }
      

      this works as expected, it shows the value of my sub variable.

      My problem is that I don't know how to access to that variable from somwhere else like :

      itemDelegate: Text {
                 text: styleData.value
                 color: myUaModel.sub   ? "blue" : "red"
                // or   color: styleData.sub  ? "blue" : "red"
             }
      

      Can someone tell how this should be done please ?

      Thank you

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @LeLev
      a variable is attached to each item delegate instance according to the item/row displayed
      so simply use the roleName in the delegate context

      Edit: ah, i misread. you are using controls1

      as a workaround you can add a custom invokable method which gives you the role value for "styleData.row". This custom invokable method, simply calls data() with the itemrole value taken from roleNames()

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      ODБOïO 2 Replies Last reply
      1
      • raven-worxR raven-worx

        @LeLev
        a variable is attached to each item delegate instance according to the item/row displayed
        so simply use the roleName in the delegate context

        Edit: ah, i misread. you are using controls1

        as a workaround you can add a custom invokable method which gives you the role value for "styleData.row". This custom invokable method, simply calls data() with the itemrole value taken from roleNames()

        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by
        #3

        hi, thank you very much for your answer
        @raven-worx said in Accessing QAbstractItemModel fields in QML:

        as a workaround you can add a custom invokable method which gives you the role value for "styleData.row". This custom invokable method, simply calls data() with the itemrole value taken from roleNames()

        I was trying to do that but i was wondering if things will get refreshed correctly, I will try and let you know.

        1 Reply Last reply
        0
        • raven-worxR raven-worx

          @LeLev
          a variable is attached to each item delegate instance according to the item/row displayed
          so simply use the roleName in the delegate context

          Edit: ah, i misread. you are using controls1

          as a workaround you can add a custom invokable method which gives you the role value for "styleData.row". This custom invokable method, simply calls data() with the itemrole value taken from roleNames()

          ODБOïO Offline
          ODБOïO Offline
          ODБOï
          wrote on last edited by ODБOï
          #4

          @raven-worx hi
          I am trying to write the q_invokable method you suggested to me but i have lots of troubles, this is what i try

          itemDelegate: Text {
               text: styleData.value 
               color: myUaModel.getSubActiveByIndex(styleData.row,styleData.column) ? "green" : "blue"
          }
          
            Q_INVOKABLE bool getSubActiveByIndex(int r, int c){ 
                  qDebug() << "r : " << r << ";" << "c : " << c;
                  QModelIndex idx = QAbstractItemModel::createIndex(r,c);
                  qDebug() << "ModelIndex : " << idx;
                  if(idx.isValid()){
                      auto item = static_cast<TreeItem *>(idx.internalPointer());
                      qDebug()  << item; // always outputs QObject(0x0)
                  }else{
                      qDebug()<< "Bad qModelIndex";
                  }
          

          i get this output when i show the tree view in QML, this are the 3 children of my root node, but it looks like i already have an issue here because qDebug() << item; shows QObject(0x0)

          r :  0 ; c :  0
          ModelIndex :  QModelIndex(0,0,0x0,OpcUaModel(0x127b460))
          QObject(0x0)
          r :  0 ; c :  1
          ModelIndex :  QModelIndex(0,1,0x0,OpcUaModel(0x127b460))
          QObject(0x0)
          r :  0 ; c :  2
          ModelIndex :  QModelIndex(0,2,0x0,OpcUaModel(0x127b460))
          QObject(0x0)
          

          Then if i access items attributes or data() method, obviously my app crashes, do you see what I did wrong ?

          To me it looks like i'm doing something wrong about QModelIndex creation, because if i pass the QModelIndex directly from qml to c++ like this :

          TreeView{
              onDoubleClicked: myUaModel.subscribe(index)
          }
          //c++
          Q_INVOKABLE void subscribe(QModelIndex ind ){
                 auto item = static_cast<TreeItem *>(ind.internalPointer());
          // ...
          }
          

          this way i can construct and interact with the TreeItems.
          The probleme is that index is not avalable in the itemDelegate, thats why i pass styleData.row and styleData.column

          color : myUaModel.getSubActiveByIndex(styleData.row,styleData.column) ? "green" : "red"
          

          and try construct the QModelIndex in cpp

          Q_INVOKABLE bool getSubActiveByIndex(int r, int c){
            QModelIndex idx = QAbstractItemModel::createIndex(r,c); 
          

          TreeItem and the model class come from this example : https://doc.qt.io/QtOPCUA/qtopcua-opcuaviewer-example.html (i'm just trying to change the view from QtreeView to QML TreeView. My first post about this )

          ODБOïO 1 Reply Last reply
          0
          • ODБOïO ODБOï

            @raven-worx hi
            I am trying to write the q_invokable method you suggested to me but i have lots of troubles, this is what i try

            itemDelegate: Text {
                 text: styleData.value 
                 color: myUaModel.getSubActiveByIndex(styleData.row,styleData.column) ? "green" : "blue"
            }
            
              Q_INVOKABLE bool getSubActiveByIndex(int r, int c){ 
                    qDebug() << "r : " << r << ";" << "c : " << c;
                    QModelIndex idx = QAbstractItemModel::createIndex(r,c);
                    qDebug() << "ModelIndex : " << idx;
                    if(idx.isValid()){
                        auto item = static_cast<TreeItem *>(idx.internalPointer());
                        qDebug()  << item; // always outputs QObject(0x0)
                    }else{
                        qDebug()<< "Bad qModelIndex";
                    }
            

            i get this output when i show the tree view in QML, this are the 3 children of my root node, but it looks like i already have an issue here because qDebug() << item; shows QObject(0x0)

            r :  0 ; c :  0
            ModelIndex :  QModelIndex(0,0,0x0,OpcUaModel(0x127b460))
            QObject(0x0)
            r :  0 ; c :  1
            ModelIndex :  QModelIndex(0,1,0x0,OpcUaModel(0x127b460))
            QObject(0x0)
            r :  0 ; c :  2
            ModelIndex :  QModelIndex(0,2,0x0,OpcUaModel(0x127b460))
            QObject(0x0)
            

            Then if i access items attributes or data() method, obviously my app crashes, do you see what I did wrong ?

            To me it looks like i'm doing something wrong about QModelIndex creation, because if i pass the QModelIndex directly from qml to c++ like this :

            TreeView{
                onDoubleClicked: myUaModel.subscribe(index)
            }
            //c++
            Q_INVOKABLE void subscribe(QModelIndex ind ){
                   auto item = static_cast<TreeItem *>(ind.internalPointer());
            // ...
            }
            

            this way i can construct and interact with the TreeItems.
            The probleme is that index is not avalable in the itemDelegate, thats why i pass styleData.row and styleData.column

            color : myUaModel.getSubActiveByIndex(styleData.row,styleData.column) ? "green" : "red"
            

            and try construct the QModelIndex in cpp

            Q_INVOKABLE bool getSubActiveByIndex(int r, int c){
              QModelIndex idx = QAbstractItemModel::createIndex(r,c); 
            

            TreeItem and the model class come from this example : https://doc.qt.io/QtOPCUA/qtopcua-opcuaviewer-example.html (i'm just trying to change the view from QtreeView to QML TreeView. My first post about this )

            ODБOïO Offline
            ODБOïO Offline
            ODБOï
            wrote on last edited by
            #5

            @LeLev said in Accessing QAbstractItemModel fields in QML:

            To me it looks like i'm doing something wrong about QModelIndex creation, because if i pass the QModelIndex directly from qml to c++ like this :

            indeed this was the problem, instead of passing styleData.row and styleData.column to my Q_Invokable method i can pass
            styleData.index

            itemDelegate: Text {
             color:  myUaModel.getSubActiveByIndex(styleData.index) ? "green" : "blue"
            }
            
            Q_INVOKABLE bool getSubActiveByIndex(QModelIndex ind){
                    bool isSub = false;
                    if(!ind.isValid()) return isSub;
            
                    auto item = static_cast<TreeItem *>(ind.internalPointer());
                    isSub = item->data(7).toBool();
                    return isSub;
                }
            
            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