PDA

View Full Version : Getting the value of a QComboBox inside a QTreeWidget



pcheng
9th August 2012, 13:16
I created a QTreeWidget and assigned a column as a combobox using the code in a function AddRoot shown below:



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);


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++)
{
QTreeWidgetItem *item = ui->treeWidget->topLevelItem(i);
quantity += item->text(7).toFloat();
}

ui->planQuantityLineEdit->setText(QString("%1").arg(quantity));
}


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

pcheng
10th August 2012, 09:26
Any ideas on how I can access my combobox value inserted in the qtreewidget?

Pericles

pcheng
13th August 2012, 09:23
I changed the widget to a QDoubleSpinBox which makes it easier to handle the values since I only want decimals at 0.5 steps.


QDoubleSpinBox *quantitySpinBox = new QDoubleSpinBox();
quantitySpinBox->setDecimals(1);
quantitySpinBox->setSingleStep(0.5);
ui->treeWidget->setItemWidget(itm, 7, quantitySpinBox);

connect(quantitySpinBox, SIGNAL(valueChanged(double)), this, SLOT(planQuantity()));

But again when I try to access the value with the following code it does not return anything.


for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
{
QTreeWidgetItem *item = ui->treeWidget->topLevelItem(i);
qDebug() << item->data(7, Qt::DisplayRole).toFloat();
quantity += item->text(7).toFloat();
}

Any ideas on how to access a value in a widget within the QTreeWidgetItem?

Thanks,

Pericles