Results 1 to 7 of 7

Thread: QDoubleSpinBox inside a QTreeWidget

  1. #1
    Join Date
    Jun 2012
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default QDoubleSpinBox inside a QTreeWidget

    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:

    Qt Code:
    1. // Testing spinboxes
    2. QDoubleSpinBox *quantitySpinBox = new QDoubleSpinBox();
    3. quantitySpinBox->setDecimals(1);
    4. quantitySpinBox->setSingleStep(0.5);
    5. ui->treeWidget->setItemWidget(itm, 7, quantitySpinBox);
    6.  
    7. connect(quantitySpinBox, SIGNAL(editingFinished()), this, SLOT(planQuantity()));
    To copy to clipboard, switch view to plain text mode 

    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.

    Qt Code:
    1. qDebug() << item->data(7, Qt::DisplayRole).toFloat();
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QDoubleSpinBox inside a QTreeWidget

    Do you use 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) then dynamic cast it to QDoubleSpinBox and use the value() member function.

  3. #3
    Join Date
    Jun 2012
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDoubleSpinBox inside a QTreeWidget

    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:

    Qt Code:
    1. QTreeWidgetItem *item = ui->treeWidget->topLevelItem(i);
    To copy to clipboard, switch view to plain text mode 

    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

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QDoubleSpinBox inside a QTreeWidget

    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)

  5. #5
    Join Date
    Jun 2012
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDoubleSpinBox inside a QTreeWidget

    I tried to follow your instructions and placed this code in the program:

    Qt Code:
    1. QTreeWidget *myWidget = new QTreeWidget();
    2. float myValue = dynamic_cast<QDoubleSpinBox*>(myWidget->itemWidget(item, 7))->value();
    3. qDebug() << myValue;
    To copy to clipboard, switch view to plain text mode 

    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?
    Last edited by pcheng; 4th September 2012 at 11:41.

  6. #6
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QDoubleSpinBox inside a QTreeWidget

    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

  7. The following user says thank you to Zlatomir for this useful post:

    pcheng (5th September 2012)

  8. #7
    Join Date
    Jun 2012
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDoubleSpinBox inside a QTreeWidget

    Thanks Zlatomir,

    That did the trick.

    Pericles

Similar Threads

  1. Replies: 2
    Last Post: 13th August 2012, 09:23
  2. React on KeyEvent inside QTreeWidget
    By Raadush in forum Qt Programming
    Replies: 2
    Last Post: 10th April 2012, 06:58
  3. QTreeWidget row updates inside QDialog
    By innerhippy in forum Qt Programming
    Replies: 4
    Last Post: 19th December 2008, 18:32
  4. Drag and Drop item inside QTreeWidget
    By nina1983 in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2008, 11:43
  5. Resizing a QTreeWidget inside a layout
    By bruccutler in forum Qt Programming
    Replies: 11
    Last Post: 27th March 2007, 15:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.