简体   繁体   中英

QTreeWidgetItem data

I have a following data:

1.0 2.0 3.1 4.1 5.2

Where Index.SubIndex But I want to show in QTreeWidget as following:
Index 1
Index 3
Index 4
Index 2
Index 5

But I did as following:

QTreeWidgetItem *child = new QTreeWidgetItem();
child->setText("Index " + QString::number(index));

But I want to save hidden data, which I should to get when user selected any item of tree. How I can do this? Which signal/slot I can use for it? I mean I should get not "Index 1" or "Index 2" and etc, I should get 1,2,3,4,5,6 aand etc. And parse text is NOT solution.

I don't understand half of your question. But for storing custom data in a QTreeWidgetItem I would subclass it:

class my_tree_item : public QTreeWidgetItem
{
    public:        
        my_tree_item(custom_data* d, QString& str) { this->dat=d; QTreeWidgetItem(str); }
        ~my_tree_item();

        inline custom_data* get_data { return this->dat; }

    private:
        custom_data* dat;
};

If you then receive a QTreeWidgetItem from a signal you can simply retrieve the data

custom_data* c = static_cast<my_tree_item*>(some_qtreewidgetitem)->get_data();

Use the Qt::UserRole data field to store your custom data and retrieve it when a QTreeItemWidget is selected:

QTreeWidgetItem* child = new QTreeWidgetItem();
child->setText(0, "Index " + QString::number(index));
// save internal data to retrieve later on
child->setData(0, Qt::UserRole, QVariant(index));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM