PDA

View Full Version : QDoubleSpinBox inside a QTreeWidget



pcheng
4th September 2012, 09:01
I have a QTreeWidget in which I made an addRoot function and an addChild function.

In the addRoot I want one column of the widget to be a doublespinbox and so I call it with the following code:



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

connect(quantitySpinBox, SIGNAL(editingFinished()), this, SLOT(planQuantity()));


The spinbox appears correctly and works fine in the widget.

The problem arises when I try to access the values in the widget.

I use the following debugging code to see what the values are in the spinboxes.



qDebug() << item->data(7, Qt::DisplayRole).toFloat();


The result is all 0. Even though I change values throughout the cells it is always 0.

Is there something that I am doing wrong? How can I access the value set inside the spinbox?

Any help will be greatly appreciated because I am stuck here not going anywhere without those values.

Thanks,

Pericles

Zlatomir
4th September 2012, 10:14
Do you use setData (http://http://doc.qt.nokia.com/4.7-snapshot/qtreewidgetitem.html#setData) to put something (the spinbox value) in that "data"? If not you need to take the pointer to the widget of that item (itemWidget (http://doc.qt.nokia.com/4.7-snapshot/qtreewidget.html#itemWidget)) then dynamic cast it to QDoubleSpinBox and use the value() member function.

pcheng
4th September 2012, 10:37
I am not using the setData on that widget. I change the value on the UI by clicking the up and down arrows.

I assume I need to access the QDoubleSpinBox within the widget as you stated. So I use the following:



QTreeWidgetItem *item = ui->treeWidget->topLevelItem(i);


This gives me the item with all the columns. But how do I associate column 7 with a QDoubleSpingBox? Is there a way to assign a specific column to a pointer?

I am now trying to create a new QDoubleSpinBox and then assign column 7 to that so that I can read its value but I can't seem to find a way to do that from the options that item is giving me.

Pericles

Zlatomir
4th September 2012, 10:59
Don't create a new QDoubleSpinBox.
See the link in my previous post: QTreeWidget::itemWidget takes a QTreeWidgetItem* and a column and returns the pointer to the QWidget that was set with setItemWidget for that item, in your case this is supposed to be a QDoubleSpinBox* but you get a QWidget* so you need to cast it (use dynamic_cast and don't forget to check for null - for the case that QTreeWidgetItem contains no widget or a different type of widget)

pcheng
4th September 2012, 11:29
I tried to follow your instructions and placed this code in the program:



QTreeWidget *myWidget = new QTreeWidget();
float myValue = dynamic_cast<QDoubleSpinBox*>(myWidget->itemWidget(item, 7))->value();
qDebug() << myValue;


But I get a segmentation fault when I try to use it on the dynamic_cast line.

I also saw some people use qobject_cast so I tried that too but with no result.

Any ideas what I am doing wrong.

I am sorry but my typecasting is rusty. Very rusty.

Thanks,

Pericles

EDIT. It seems that dynamic_cast<QDoubleSpinBox*>(myWidget->itemWidget(item, 7)) returns NULL as you said. Any ideas on why it does that?

Zlatomir
4th September 2012, 12:17
Because you create another treewidget instead of using the one that you already have and you added QSpinbox to it. So don't do: QTreeWidget *myWidget = new QTreeWidget(); (i can guess) you want to use: ui->treeWidget

pcheng
5th September 2012, 09:04
Thanks Zlatomir,

That did the trick.

Pericles