numberCruncher
numberCruncher

Reputation: 645

Getting type of QStandardItem in Qml

I have a QStandardItemModel which I display using a TableView in qml. It uses QVariant to store the data. I want to have custom delegates depending on which type the stored data has, e.g. like this

    Component {
        id: myDelegate
        Loader {
            property var roleTwo: model.two
            sourceComponent: if( CODE_FOR_MY_ITEM_HAS_BOOL_TYPE) {
                                 checkBoxDelegate}
                             else { stringDelegate}
        }
    }

However, I don't know how to check the type of an item in my model. How can this be achieved?

Upon request, I provide more context to this question:

As posted here Custom Model for TableView or QStandardItemModel I want to have a TableView with two columns, a parameter name and a parameter value. The goal is to have a list of editable parameters which control the behaviour of an algorithm. For this, I use a QStandardItemModel defined like this:

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;
 }
};

This model right now is displayed like this:

TableView {
    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

    }
}

So is this the way you would do it? Also, I am happy for hints on who to make the model editable.

Upvotes: 0

Views: 558

Answers (1)

dtech
dtech

Reputation: 49319

If typeof() works for your use-case, then you can roll with it.

What I personally like to do is have an integer member to denote the particular type. This has an array of advantages, here are some:

  • you can use the same underlying data type to drive different gui types, for example, a string might be a name or an info, one might be editable, the other read only
  • it works in cases where you might have fundamentally different model items with fundamentally different sets of data
  • you can use it as an index, and have an array of Components, and in the loader simply use sourceComponent: guitypes[type]

Upvotes: 2

Related Questions