Search code examples
c++qtqt4qt-creatorqtablewidget

How can I add a checkbox/radio button to QTableWidget


How can I add a checkbox/radiobutton/combobox to a QTableWidget or a QListWidget?


Solution

  • For a checkbox using the item's setCheckState method should do what you need both for list and table widgets. See if code below would work for you:

    List widget:

    QListWidgetItem *item0 = new QListWidgetItem(tr("First"), listWidget);
    QListWidgetItem *item1 = new QListWidgetItem(tr("Second"), listWidget);
    
    item0->setCheckState(Qt::Unchecked);
    item1->setCheckState(Qt::Checked);
    

    Table widget:

    QTableWidgetItem *item2 = new QTableWidgetItem("Item2");
    item2->setCheckState(Qt::Checked);
    tableWidget->setItem(0, 0, item2);
    

    You can use delegates (QItemDelegate) for other types of editor's widgets, example is here: Spin Box Delegate Example.

    Spin Box Delegate

    I hope this helps.