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. DelegateChooser not working with TableView and QStandardItemModel
Forum Updated to NodeBB v4.3 + New Features

DelegateChooser not working with TableView and QStandardItemModel

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 3 Posters 1.2k 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.
  • B Offline
    B Offline
    Bartelchen
    wrote on last edited by
    #1

    i have a QStandardItemModel which I fill by receiving a JSON. The JSON looks sth like this:

    {
       "vehicle_list": [
           "subs": [ "CAM_1", "CAM_2",  ... ],
           "name": "vehicle_0"
        ]
    }
    

    I put this into a QStandardItemModel. Each row reprents one Vehicle, and the column entrys are the Name, and after that the cameras:

    ROW(0): vehicle_0, cam_1, cam_2, ...
    ROW(1): vehicle_1, cam_1, cam_2, ...
    

    This works as intended. So I want to display the data in my model with an TableView in QML. And i need to display the vehicle name as a text, and the cameras as buttons. My QML file looks like this:

    TableView {
        id: vehicleView
        model: vehicleModel
        anchors.fill: parent
        delegate: myChooser
    
        DelegateChooser {
            id: myChooser
            role: "r_vehicle_type"
    
            DelegateChoice {
                id: choice1
                roleValue: "camera"
                delegate: Rectangle {
                    width: 100
                    height: 30
                    border.color: "black"
                    border.width: 1
    
                    Component.onCompleted: {
                        console.log("Camera: col=", choice1.column, ", row=", choice1.row)
                    }
    
                    Button {
                        text: r_camera_name
                    }
                }
            }
    
            DelegateChoice {
                roleValue: "vehicle"
                delegate: Rectangle {
                    width: 100
                    height: 30
                    border.color: "black"
                    border.width: 1
    
                    Text {
                        text: r_vehicle_name
                    }
                }
            }
        }
    }
    

    If I run my app like this, the console puts out the following:

    TableView: failed loading index: 0
    TableView: failed loading index: 1
    TableView: failed loading index: 2
    TableView: failed loading index: 3
    TableView: failed loading index: 4
    TableView: failed loading index: 5
    TableView: failed loading index: 6
    

    If I dont use the DelegateChooser but instead something like:

    delegate: Rectangle {
    width: 100
    height: 20
    Text {
     text: r_vehicle_name
     }
    }
    

    (with r_vehicle_name is the role defined in my model, where i put the vehicle name) works fine.

    Anyone got a helping hand?

    Thanks in advance,
    Bartelchen

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bartelchen
      wrote on last edited by Bartelchen
      #2

      I managed to (partially) solve the issue . I was missing a default DelegateChoice, therefor the indexes could not be loaded ... which leads me to my next problem:

      The default option is always chosen by my DelgateChooser for the first column. The type for the first column should always be "vehicle" and I set it in my model like the following:

      for(int i = 0; i < m_model->rowCount(); i++) {
              for(int j = 0; j < m_model->columnCount(); j++) {
                  qDebug() << i << j;
                  if(j==0) {
                      m_model->setData(m_model->index(i, j), jValueNameList.at(i).toString(), Vehicle::VEHICLE_NAME);
                      m_model->setData(m_model->index(i, j), "vehicle", Vehicle::TYPE);
                  } else {
                      m_model->setData(m_model->index(i, j), jArraySubsList.at(i).at(j-1), Vehicle::CAMERA_NAME);
                      m_model->setData(m_model->index(i, j), "camera", Vehicle::TYPE);
                  }
      }
      

      The DelegateChooser with the default option looks like this:

      DelegateChooser {
      id: myChooser
      role: "r_vehicle_type"

          DelegateChoice {
              roleValue: "vehicle"
              Rectangle {
                  width: 100
                  height: 30
                  border.color: "blue"
                  border.width: 1
                  Text {
                      text:  model.r_vehicle_name
                  }
              }
          }
          DelegateChoice {
              roleValue: "camera"
              Rectangle {
                  width: 100
                  height: 30
                  border.color: "green"
                  border.width: 1
                  Text {
                      text: model.r_vehicle_cam
                  }
              }
          }
          DelegateChoice {
              Rectangle {
                  width: 100
                  height: 30
                  border.color: "red"
                  border.width: 1
                  Text {
                      text: "default"
                  }
              }
          }
      

      }
      ```

      Every entry with "camera" as type is displayed correctly. Any ideas what I am missing now? I know that I could just use the "default" choice for the vehicle type, but I want to know why my type "vehicle" is not processed correctly.

      Greetings

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Bartelchen
        wrote on last edited by
        #3

        Another update on this topic ... and I am even more confused:

        I just started testing the reception of multiple vehicle in my JSON message so my model consists of multiple rows.

        The index (0, 0 -- first row, first entry) still gets displayed incorrectly, being displayed by the "default" delegateChoice.
        For every row following, the index (X, 0) is getting displayed correctly by the delegateChoice with roleValue "vehicle".

        I debugged the model data after inserting into the first column. I get the following output:

        First Column, type:  QVariant(QString, "vehicle")
        First Column, type:  QVariant(QString, "vehicle")
        First Column, type:  QVariant(QString, "vehicle")
        
        

        Why is just the first index not displayed correctly?

        P 1 Reply Last reply
        0
        • B Bartelchen

          Another update on this topic ... and I am even more confused:

          I just started testing the reception of multiple vehicle in my JSON message so my model consists of multiple rows.

          The index (0, 0 -- first row, first entry) still gets displayed incorrectly, being displayed by the "default" delegateChoice.
          For every row following, the index (X, 0) is getting displayed correctly by the delegateChoice with roleValue "vehicle".

          I debugged the model data after inserting into the first column. I get the following output:

          First Column, type:  QVariant(QString, "vehicle")
          First Column, type:  QVariant(QString, "vehicle")
          First Column, type:  QVariant(QString, "vehicle")
          
          

          Why is just the first index not displayed correctly?

          P Offline
          P Offline
          Pavel Kovalenko
          wrote on last edited by Pavel Kovalenko
          #4

          @Bartelchen
          I had same problem.
          I noticed with qdebug that when I move ScrollIndicator - DelegateChooser roleValue was updated from model[0,0] and after that everything is ok. So my solution was to add this to TableView:

          Component.onCompleted: {
              model.invalidate();
          }
          
          1 Reply Last reply
          0
          • strahlexS Offline
            strahlexS Offline
            strahlex
            wrote on last edited by
            #5

            I'm seeing the same thing here as well. Has anyone reported it as bug?

            Feel free to check out my website machinekoder.com
            and my pet projects Intellicute and QtQuickVcp

            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