Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED] Can't create a QStandardItem of a C defined struct



  • Hi all,
    I'm a newby to Qt. I'm trying to learn and use QStandardItem together with my old code.
    The old code is in C, hence contains structs instead of classes.
    I want to present the data from the struct in a QTableView using QStandardItemModel but when I loop through and try to create each item and add to the model the compiler says "no matching function for call to 'QStandardItem::QStandardItem(mystruct&)'

    So what am I missing to create an item?



  • Hi,

    could you post the code where you get this error?
    The idea behind QStandardItemModel is to create a QStandardItem for each field of your struct and add a row to the model with QStandardItemModel::insertRow()

    For instance:

    mystruct data[ARRAY_SIZE];
    
    for (const mystruct& item: data) {
        QList<QStandardItem> row;
    
        row.append(QStandardItem(item.field1));
        ....
        row.append(QStandardItem(item.fieldn));
    
        model->addRow(row);
    }
    

    You have to manually convert your fields is Strings



  • @mcosta Hi
    OK, so there is no way to create an item from a struct, without separating the struct into field data per item!? Like in your code sample

    Below is the struct, slightly modified an reduced in size. I want to use a combobox to select index and present elements in the tableview to be edited.
    The elementnames are specified, like 50 of them in a separate ini file, so they arre filled into a delegate combobox as in QSpinBox example to select from into the cell
    The limits are equally presented and limited to input type and size

    struct data {
    int index; /* index to display /
    int group; /
    group index 1=> 3 with 4 dataelements in each group ... /
    struct elements {
    int elemno; /
    elementnumber, 1- 5 /
    char elemname[8]; /
    its name /
    float limit 1; /
    low limit /
    float limit 2; /
    upper limit */
    } elem [5];
    };



  • @andsun said:

    OK, so there is no way to create an item from a struct, without separating the struct into field data per item!?

    QStandardItemModel is for simple models; if you want to use complex models you have to subclass QAbstractItemModel



  • Hmm OK,

    Think I get it.
    What you say is I have to create my own class inherrited from QAbstractItemModel
    And in the class implementation modify to my usage

    OK, I'll give it a try and see where it leads me.

    Thanks for the reply, hope its the answer to my problem.
    Close the thread for now... somehow