How to create a tree widget from QList
-
@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