Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to show a list in a QTableWidgetItem item?



  • I have a QTableWidget with 2 columns: name and year of birth. The user will input its name, but I would like to show a list of years in year of birth column, so that he/she could select from it.

    I probably did not search correctly, as I could not find any explanation or example.

    Could anyone provide an example/link of how can I do it?

    Thanks!


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You can make a custom QStyledItemDelegate and in the createEditor method create a QComboBox containing the list of dates you want.



  • @SGaist ,

    Thanks a lot for your time!

    I used QTableWidget::setWidget instead of QTableWidget::setItem to set a QComboBox in the cell, and it worked fine:

          QComboBox* _list = new QComboBox();
          _list->addItem("A");
          _list->addItem("B");
          _list->addItem("C");
          ui->tblPks->setCellWidget(_row, 4, (QWidget*)_list);
    

    img00.png


Log in to reply