How to create a tree widget from QList
-
@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?@jsulm i tried this, it says that no viable conversion from 'const qstring' to 'const qlist tree widget item
treeWidget->insertTopLevelItems(0, gpsList.at(i));
-
@jsulm i tried this, it says that no viable conversion from 'const qstring' to 'const qlist tree widget item
treeWidget->insertTopLevelItems(0, gpsList.at(i));
@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. -
@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));
-
@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);
-
@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);
@jsulm ı understand it, but i dont understand how can i append my QList instead of items
-
@jsulm ı understand it, but i dont understand how can i append my QList instead of 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);
-
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);
-
@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 said in How to create a tree widget from QList:
is there anything seems wrong?
No.
Does it work?@jsulm yes, but i wonder is there any logical error of my code.
Thank you -
@jsulm yes, but i wonder is there any logical error of my code.
Thank you@suslucoder How i have second column, and i want to add something to my child object.
Should i create new tree widget items? -
@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 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")));
@jsulm my second column value is coming from a list again, does the code work that you suggest me in that case
-
@jsulm my second column value is coming from a list again, does the code work that you suggest me in that case
@suslucoder yes it works. thank you