Results 1 to 5 of 5

Thread: Access QComboBox inside QTableWidget

  1. #1
    Join Date
    Feb 2017
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Access QComboBox inside QTableWidget

    Hi,

    I am currently using the QTableWidget, and some of the fields need to have a dropdown menu, so I inserted some QComboBoxes.
    Qt Code:
    1. QComboBox* myComboBox = new QComboBox();
    2. ui->tableWidget->setCellWidget(0,i,myComboBox);
    To copy to clipboard, switch view to plain text mode 

    Now I need to be able to retrieve the QComboBox Index whenever someone changes a dropdown box.


    For the normal cells inside my table, I am using:
    Qt Code:
    1. on_tableWidget_cellChanged(int row, int column)
    2. {
    3. QTableWidgetItem *myItem = ui->tableWidget->item(row,column);
    4. }
    To copy to clipboard, switch view to plain text mode 

    But this does not tell me when one of the QComboBoxes is manipulated by the user.
    Does anyone know how I can setup a function which is called whenever the user changes one of the QComboBoxes?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Access QComboBox inside QTableWidget

    Connect to currentIndexChanged() signal of QComboBox.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Feb 2017
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Access QComboBox inside QTableWidget

    thank you for your quick reply.

    I am still a bit confused as to how to connect this, especially since the number of qComboboxes in my table can change.
    I am assuming I will need to use this at some point:

    Qt Code:
    1. connect(myComboBox,SIGNAL(currentIndexChanged(QString)) ,info,SLOT(setText(QString)) );
    To copy to clipboard, switch view to plain text mode 


    Also, for the currentItemChanged, what do i need as function input?
    Qt Code:
    1. void gepetto2::on_tableWidget_currentItemChanged(QTableWidgetItem *current, QTableWidgetItem *previous)
    2. {
    3. current->currentIndex();
    4. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Access QComboBox inside QTableWidget

    Well, it may not look straight forward, but you could store the item's row and col index in each combo box, and retrieve them back in the connected slot.

    Here is an working example. Just show() this widget

    Qt Code:
    1. class TableWidget : public QTableWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. enum Id
    6. {
    7. Row = Qt::UserRole + 1,
    8. Col
    9. };
    10.  
    11. explicit TableWidget(QWidget * parent = 0)
    12. : QTableWidget(parent)
    13. {
    14. setRowCount(5);
    15. setColumnCount(2);
    16.  
    17. for(int row = 0; row < rowCount(); ++row)
    18. {
    19. for(int col = 0; col < columnCount(); ++col)
    20. {
    21. setItem(row, col, new QTableWidgetItem());
    22.  
    23. if(col == 0)
    24. {
    25. QComboBox * comboBox = new QComboBox();
    26. comboBox->insertItems(0, QString("1,2,3,4,5,6").split(","));
    27. comboBox->setItemData(0, row, Row);
    28. comboBox->setItemData(0, col, Col);
    29. connect(comboBox, SIGNAL(currentIndexChanged(QString)), SLOT(onComboChanged(QString)));
    30.  
    31. setCellWidget(row, 0, comboBox);
    32. }
    33. }
    34. }
    35.  
    36. show();
    37. }
    38.  
    39. private slots:
    40. void onComboChanged(const QString & text)
    41. {
    42. QComboBox * comboBox = dynamic_cast<QComboBox *>(sender());
    43.  
    44. if(comboBox)
    45. {
    46. int row = comboBox->itemData(0, Row).toInt();
    47. int col = comboBox->itemData(0, Col).toInt();
    48.  
    49. item(row, col + 1)->setText(text);
    50. }
    51. }
    52. };
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    Feb 2017
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Access QComboBox inside QTableWidget

    Thank you for your post. I ended up finding a different workaround. Since i only ended up needing the column number, I pass it as setObjectName to the qCombobox, which is somewhat less complicated. ( i know, it's more of a workaround then a solution...


    I declare the box as follows before i pass it to the table
    Qt Code:
    1. QComboBox* myComboBox = new QComboBox(); // making a new dropdown box
    2. myComboBox->setObjectName(QString::number(i)); // pass column number as object name
    3. connect(myComboBox, SIGNAL(currentIndexChanged(QString)), SLOT(onComboChanged(QString)));
    4. ui->tableWidget->setCellWidget(0,i,myComboBox); // put box into table
    To copy to clipboard, switch view to plain text mode 

    Then I call the onComboChanged() (based on what you suggested previously)

    Qt Code:
    1. void gepetto2::onComboChanged(const QString & text)
    2. {
    3. qDebug() << "onComboChanged " << text;
    4. QComboBox * comboBox = dynamic_cast<QComboBox *>(sender()); // load the qcombobox item
    5.  
    6.  
    7. if(comboBox)
    8. {
    9. int column = comboBox->objectName().toInt(); // retrieve column number
    10. qDebug() << "Dropdown: cols " << column << " : " << comboBox->currentText() << " index " << comboBox->currentIndex();
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Access to QDateEdit value and QCombobox
    By rahma_sassi in forum Qt-based Software
    Replies: 1
    Last Post: 23rd December 2016, 12:56
  2. Selection Qtablewidget inside qtablewidget
    By chistof in forum Qt Programming
    Replies: 0
    Last Post: 4th July 2016, 17:24
  3. access selected value of QcomboBox has problem
    By isan in forum Qt Programming
    Replies: 3
    Last Post: 1st May 2016, 22:37
  4. Replies: 2
    Last Post: 13th August 2012, 10:23
  5. QComboBox inside QTableWidget
    By campana in forum Qt Programming
    Replies: 7
    Last Post: 20th March 2006, 18: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.