How can I add a checkbox/radio button to QTableWidget

后端 未结 3 1963
逝去的感伤
逝去的感伤 2020-12-25 11:44

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

3条回答
  •  眼角桃花
    2020-12-25 12:28

    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.

提交回复
热议问题