Results 1 to 3 of 3

Thread: Getting the value of a QComboBox inside a QTreeWidget

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

    Default Getting the value of a QComboBox inside a QTreeWidget

    I created a QTreeWidget and assigned a column as a combobox using the code in a function AddRoot shown below:

    Qt Code:
    1. combo_box = new QComboBox();
    2. combo_box->addItem("0");
    3. combo_box->addItem("0.5");
    4. combo_box->addItem("1", QVariant(1));
    5. combo_box->addItem("2", 2);
    6. combo_box->addItem("3", 3);
    7. combo_box->addItem("4");
    8. combo_box->addItem("5");
    9. combo_box->addItem("6");
    10. combo_box->addItem("7");
    11. combo_box->addItem("8");
    12. combo_box->addItem("9");
    13. combo_box->addItem("10");
    14. combo_box->addItem("11");
    15. combo_box->addItem("12");
    16. combo_box->addItem("13");
    17. combo_box->addItem("14");
    18. combo_box->addItem("15");
    19. combo_box->addItem("16");
    20.  
    21. connect(combo_box, SIGNAL(currentIndexChanged(int)), this, SLOT(planQuantity()));
    22.  
    23. 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.

    Qt Code:
    1. void PlanDialog::planQuantity()
    2. {
    3. // Calculate the total plan quantity
    4. float quantity = 0.0;
    5. for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
    6. {
    7. QTreeWidgetItem *item = ui->treeWidget->topLevelItem(i);
    8. quantity += item->text(7).toFloat();
    9. }
    10.  
    11. ui->planQuantityLineEdit->setText(QString("%1").arg(quantity));
    12. }
    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

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

    Default Re: Getting the value of a QComboBox inside a QTreeWidget

    Any ideas on how I can access my combobox value inserted in the qtreewidget?

    Pericles

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

    Default Re: Getting the value of a QComboBox inside a QTreeWidget

    I changed the widget to a QDoubleSpinBox which makes it easier to handle the values since I only want decimals at 0.5 steps.

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

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

    Qt Code:
    1. for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
    2. {
    3. QTreeWidgetItem *item = ui->treeWidget->topLevelItem(i);
    4. qDebug() << item->data(7, Qt::DisplayRole).toFloat();
    5. quantity += item->text(7).toFloat();
    6. }
    To copy to clipboard, switch view to plain text mode 

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

    Thanks,

    Pericles

Similar Threads

  1. QCombobox with QTreeView/QTreeWidget
    By swiety in forum Qt Programming
    Replies: 2
    Last Post: 30th August 2011, 19:27
  2. breakpoint inside QComboBox subclass not working
    By yodasoda in forum Qt Programming
    Replies: 6
    Last Post: 4th June 2010, 17:02
  3. Focus frame inside QComboBox list
    By alpinista in forum Qt Programming
    Replies: 2
    Last Post: 13th November 2009, 14:30
  4. QComboBox in QTreeWidget
    By yes756 in forum Qt Programming
    Replies: 2
    Last Post: 10th September 2009, 11:22
  5. QComboBox inside QTableWidget
    By campana in forum Qt Programming
    Replies: 7
    Last Post: 20th March 2006, 17:22

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.