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. Help with QML TableView
Forum Update on Monday, May 27th 2025

Help with QML TableView

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 3 Posters 2.6k 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
    maxwell31
    wrote on last edited by
    #1

    Hi,

    I am new to Qt and QML and want to make a table view, which has two columns:

    • parameter name ( a string)
    • parameter value (editable)

    The parameter value could be an integer, float, string or bool value. For the data model I tried this code:

    class mystandardmodel: public QStandardItemModel
    {
    
    public:
        mystandardmodel();
        enum Role {
             role1=Qt::UserRole,
             role2
          };
    
     explicit mystandardmodel(QObject * parent = 0): QStandardItemModel(parent){}
     //explicit mystandardmodel( int rows, int columns, QObject * parent = 0 )
     //    : QStandardItemModel(rows, columns, parent){}
    
     QHash<int, QByteArray> roleNames() const{
          QHash<int, QByteArray> roles;
          roles[role1] = "one";
          roles[role2] = "two";
          return roles;
     }
    };
    

    Is this how you would make the model or is there a better way? Also, is it possible to make a model where an item is a struct defined by me?

    Here is how I try to fill the model with an example parameter:

     mystandardmodel* mysmodel=new mystandardmodel(0);
     QStandardItem* it = new QStandardItem();
     it->setData("data1", mystandardmodel::role1);
     it->setData(true, mystandardmodel::role2);
    

    As far as I understand, the data-type is always a QVariant, so depending on what I provide as the first argument in setData it will be handled correctly?

    Here my example TableView:

        TableView {
            id: tableView2
            x: 121
            y: 317
            TableViewColumn {
                title: "Parameter Name"
                role: "one"
            }
            TableViewColumn {
                title: "Value"
                role: "two"
            }
            model: myTestModel
        }
    

    Now, I would like to make the parameter value editable. Also, I would like to have bool values represented as a checkbox. Ideally, a string parameter value could be a drop-down list of possible values. How could this be achieved?

    Thank you for your help!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AFuzzyBadger
      wrote on last edited by
      #2

      I wanted to do something similar recently. I noticed tableview is not in qt controls 2.0 and only in 1.x. I found a post suggesting tableview has performance issues and one might be better off using listview. This is worth mentioning as it may mean you will have more luck finding examples for functionally achieving what you are after by searching for multi column listview and editing listview.

      W 1 Reply Last reply
      1
      • M Offline
        M Offline
        maxwell31
        wrote on last edited by
        #3

        Thank you to mention this. I think performance will not be an issue for me, as I will have only around 20 entries.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          maxwell31
          wrote on last edited by maxwell31
          #4

          To have different delegates for different types I now used the following code:

              TreeView {
                  id: tableView2
                  x: 69
                  y: 316
                  width: 318
                  height: 150
                  TableViewColumn {
                      title: "Parameter Name"
                      role: "one"
                  }
                  TableViewColumn {
                      title: "Value"
                      role: "two"
                      //delegate: myDelegate
                  }
                  model: myTestModel
              }
          
              Component {
                  id: myDelegate
                  Loader {
                      property var roleTwo: model.two
                      sourceComponent: if(typeof(roleTwo)=='boolean') {
                                           checkBoxDelegate}
                                       else { stringDelegate}
                  }
              }
          
              Component {
                  id: checkBoxDelegate
                  CheckBox{text: roleTwo}
              }
          
              Component {
                  id: stringDelegate
                  Text {text: roleTwo
          
                  }
              }
          

          How can I make this model editable?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            maxwell31
            wrote on last edited by
            #5

            Following this it works
            https://stackoverflow.com/questions/49529314/edit-qstandarditemmodel-via-tableview-with-custom-delegate

            The solution is to simply call an updateValue function in the delegate, e.g. for the string delegate
            onEditingFinished: {
            myTestModel.updateValue(thisIndex, text, "two")
            }

            1 Reply Last reply
            0
            • A AFuzzyBadger

              I wanted to do something similar recently. I noticed tableview is not in qt controls 2.0 and only in 1.x. I found a post suggesting tableview has performance issues and one might be better off using listview. This is worth mentioning as it may mean you will have more luck finding examples for functionally achieving what you are after by searching for multi column listview and editing listview.

              W Offline
              W Offline
              wpmagic70
              wrote on last edited by
              #6

              @AFuzzyBadger Thank you so much. After searching the whole google and forum results about why qt creator didnt understand the tableview, i found your comment!
              Thanks again :)

              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