开发者

Creating tree models in qt and data mapping

开发者 https://www.devze.com 2023-03-08 16:34 出处:网络
I have looked through the qt docs at the tree model examples. I am struggling at how tomap my data structure to the model.

I have looked through the qt docs at the tree model examples. I am struggling at how to map my data structure to the model.

I have an array of mystructs which I would like to display in a tree, could someone offer any help on how to do this. The structure is shown below

struct mystruct{
    int id;
    float val;
    struct settings{
            QString setting_1;
            QStrin开发者_如何学运维g setting_2;
            bool on;
    };
};

I am trying to fit this structure into the qt simpletreemodel example.

Thanks


You could do something like that:

Implement the tree as a QTreeWidget

class YourTreeWidget : public QTreeWidget
{
public:
    ...
    //reimplement useful stuff of the QTreeWidget 
    //like onItemClicked(...)
};

Then implement items binded to a mystruct instance

class mystruct;
class YourTreeWidgetItem : public QTreeWidgetItem
{
public:
    YourTreeWidgetItem(mystruct*);
    mystruct* getData() { return data;}
private:
    mystruct* data;
}

Later on create one YourTreeWidget, and add one item for each of the mystruct instance your have in your array. Don't forget to format your QTreeWidget correctly according to the data you put in your items.

setHeaderLabels( QStringList() << "Column 1" << "Column 2");
...

Again it a simple solution, but I think your can do complexe stuff with that.

NOTE: when I said

Do you have to interact a lot with struct

I meant do you need custom selection, custom edition, custom display ...

0

精彩评论

暂无评论...
验证码 换一张
取 消