How to create a tree widget from QList
-
I have a table widget and im adding my datas into table like
gpsList << inner_data[2].toString(); gpsbrmList << inner_data[3].toString(); for (int i=0; i<gpsList.size(); i++ ) { ui->tableWidget->setItem(i, 0, new QTableWidgetItem(gpsList.at(i))); ui->tableWidget->setItem(i, 1, new QTableWidgetItem(gpsbrmList.at(i))); }
How can i create a tree widget using the same list, which is gpsList?
-
@suslucoder said in How to create a tree widget from QList:
How can i create a tree widget using the same list, which is gpsList?
Using https://doc.qt.io/qt-5/qtreewidget.html (there is even an example using a list!) I guess? Or what is your exact question?
-
@suslucoder said in How to create a tree widget from QList:
I've tried it but it doesnt work
You should already know that saying "I've tried it but it doesnt work" without providing any details is not helpful!
Can you please show what you tried and also tell what the result was? Or should we guess? -
@suslucoder If you would have read documentation (https://doc.qt.io/qt-5/qtreewidget.html#insertTopLevelItems) you would know that insertTopLevelItems expects a list and not single value...
Also, you're supposed to add QTreeWidgetItem to the tree and not QString. -
@jsulm said in How to create a tree widget from QList:
Also, you're supposed to add QTreeWidgetItem to the tree and not QString.
ok. I did it like this way, but it added the first row always, or should i say column i guess. I want to see my datas like in table widget
``` QTreeWidgetItem *gps = new QTreeWidgetItem(ui->treeWidget2); gps->setText(0, tr("GPS")); QTreeWidgetItem *gpsChild = new QTreeWidgetItem(gps); gpsChild->setText(0, gpsList.at(0)); gpsChild->setText(1, gpsList.at(1));
-
@suslucoder said in How to create a tree widget from QList:
but it added the first row always, or should i say column i guess
Please use correct wording, else it is hard to understand.
Do you mean you only got one row with all elements of gpsList as columns?
This is exactly what your code is doing.
I ask you again to take a look at the link I posted, especially this part:QTreeWidget *treeWidget = new QTreeWidget(); treeWidget->setColumnCount(1); QList<QTreeWidgetItem *> items; for (int i = 0; i < 10; ++i) items.append(new QTreeWidgetItem(static_cast<QTreeWidget *>(nullptr), QStringList(QString("item: %1").arg(i)))); treeWidget->insertTopLevelItems(0, items);
-
treeWidget->setColumnCount(1); QList<QTreeWidgetItem *> items; for (int i = 0; i < gpsList.size(); ++i) items.append(new QTreeWidgetItem(static_cast<QTreeWidget *>(nullptr), QStringList() << gpsList.at(i))); treeWidget->insertTopLevelItems(0, items);
-
@jsulm I did it like that, is there anything seems wrong?
``` QTreeWidgetItem *imu = new QTreeWidgetItem(ui->treeWidget2); imu->setText(0, tr("IMU")); QList<QTreeWidgetItem *> imu_items; for (int i = 0; i<imuList.size(); ++i) { imu_items.append(new QTreeWidgetItem(static_cast<QTreeWidget *>(nullptr), QStringList(imuList.at(i)))); } imu->addChildren(imu_items);
-
@suslucoder said in How to create a tree widget from QList:
is there anything seems wrong?
No.
Does it work? -
@suslucoder How i have second column, and i want to add something to my child object.
Should i create new tree widget items? -
@suslucoder Change treeWidget->setColumnCount(1); to treeWidget->setColumnCount(2); And then add your second column in each item:
imu_items.append(new QTreeWidgetItem(static_cast<QTreeWidget *>(nullptr), QStringList(imuList.at(i) << "SECOND_COLUMN_VALUE")));
-
@suslucoder yes it works. thank you