I created a QTreeWidget and assigned a column as a combobox using the code in a function AddRoot shown below:
combo_box->addItem("0");
combo_box->addItem("0.5");
combo_box->addItem("2", 2);
combo_box->addItem("3", 3);
combo_box->addItem("4");
combo_box->addItem("5");
combo_box->addItem("6");
combo_box->addItem("7");
combo_box->addItem("8");
combo_box->addItem("9");
combo_box->addItem("10");
combo_box->addItem("11");
combo_box->addItem("12");
combo_box->addItem("13");
combo_box->addItem("14");
combo_box->addItem("15");
combo_box->addItem("16");
connect(combo_box, SIGNAL(currentIndexChanged(int)), this, SLOT(planQuantity()));
ui->treeWidget->setItemWidget(itm, 7, combo_box);
combo_box = new QComboBox();
combo_box->addItem("0");
combo_box->addItem("0.5");
combo_box->addItem("1", QVariant(1));
combo_box->addItem("2", 2);
combo_box->addItem("3", 3);
combo_box->addItem("4");
combo_box->addItem("5");
combo_box->addItem("6");
combo_box->addItem("7");
combo_box->addItem("8");
combo_box->addItem("9");
combo_box->addItem("10");
combo_box->addItem("11");
combo_box->addItem("12");
combo_box->addItem("13");
combo_box->addItem("14");
combo_box->addItem("15");
combo_box->addItem("16");
connect(combo_box, SIGNAL(currentIndexChanged(int)), this, SLOT(planQuantity()));
ui->treeWidget->setItemWidget(itm, 7, combo_box);
To copy to clipboard, switch view to plain text mode
The combobox shows up nicely and I can see the values I set. The signal also works since I get to planQuantity everytime I change a value.
The problem is how to get the value from the QTreeWidget. I have the following code in planQuantity to add up all the values of the comboboxes in the column.
void PlanDialog::planQuantity()
{
// Calculate the total plan quantity
float quantity = 0.0;
for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
{
quantity += item->text(7).toFloat();
}
ui
->planQuantityLineEdit
->setText
(QString("%1").
arg(quantity
));
}
void PlanDialog::planQuantity()
{
// Calculate the total plan quantity
float quantity = 0.0;
for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
{
QTreeWidgetItem *item = ui->treeWidget->topLevelItem(i);
quantity += item->text(7).toFloat();
}
ui->planQuantityLineEdit->setText(QString("%1").arg(quantity));
}
To copy to clipboard, switch view to plain text mode
But the result is always 0. Nothing is returned from the item->text(7).toFloat(). 7 is the column where the combobox resides.
Could you please help me identify the problem?
Thanks,
Pericles
Bookmarks