33

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

Cœur
  • 37,241
  • 25
  • 195
  • 267

3 Answers3

64

There are two methods:

void QTableWidget::setCellWidget(int row, int column, QWidget* widget)

and

void QListWidget::setItemWidget(QListWidgetItem* item, QWidget* widget)

They allow to insert any widget and other controls that inherit QWidget. Checkbox/radio button/combobox do inherit from QWidget.

Pie_Jesu
  • 1,894
  • 3
  • 16
  • 30
  • 6
    I agree it's confusing this wasn't selected as the correct answer. I didn't spend a whole lot of time investigating, but I thought setting the checkable flag on a QTableWidgetItem would make a checkbox appear--it didn't. setCellWidget() worked, though. – Scott Jan 27 '13 at 15:23
  • 1
    you should check with `checkbox.checkState() == 0 or == 2 ` one unckecked and another ckecked , i excampled with python , you should convert . to -> – PersianGulf Oct 12 '13 at 13:35
  • 5
    @MohsenPahlevanzadeh: Please avoid using literal numbers when you can use aptly named constants: [Qt::CheckState](http://qt-project.org/doc/qt-4.8/qt.html#CheckState-enum). This will make your code more readable and easier to maintain (the actual value of the constants can change without you having to modify your entire codebase to reflect the change). – Luc Touraille Nov 19 '14 at 08:36
22

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.

feedc0de
  • 3,646
  • 8
  • 30
  • 55
serge_gubenko
  • 20,186
  • 2
  • 61
  • 64
  • 11
    Wow. Surprised that this comment was selected as correct answer. Because setting QListWidgetItem in check state isn't quite `adding checkbox to Table or to List`. Also, creating delegates isn't the way to insert those widgets in Table or List. Its used only for editing cells. – Pie_Jesu Mar 21 '11 at 15:46
  • may be it's because this is what OP wanted. And since his question is open ended this answer is one of the ways how he can create\set up his widget. Besides in most cases you want a control to get shown when you editing the field not viewing it. Supplying a widget to the cell via setCellWidget doesn't make this widget a part of the model, you would still have to take care of widget's content and events manually. – serge_gubenko Mar 21 '11 at 16:16
  • Can you elaborate on your "QList**Widget** and QTable**Widget**. Its impossible to reiplement delegates for them" comment? Both widgets are QAbstractItemView descendants and nothing prevents you from setting item delegates for them. The spinbox example from my post works perfectly with QTableWidget – serge_gubenko Mar 21 '11 at 17:28
  • And where exactly does it say there "Its impossible to reiplement delegates for them"? – serge_gubenko Mar 21 '11 at 18:47
  • 2
    This answer is simply incorrect. The correct answer is below. – Michael Leonard May 24 '17 at 18:36
  • 1
    Further evidence that StackOverflow should default to dynamically marking the answer currently receiving the most upvotes as "accepted." – Cecil Curry Feb 28 '18 at 08:16
3

you can add checkbox like this too

#include <QCheckBox>

void addCheckBoxAt(int row_number, int column_number,int state)
{

    // Create a widget that will contain a checkbox
     QWidget *checkBoxWidget = new QWidget();
     QCheckBox *checkBox = new QCheckBox();      // We declare and initialize the checkbox
     QHBoxLayout *layoutCheckBox = new QHBoxLayout(checkBoxWidget); // create a layer with reference to the widget
     layoutCheckBox->addWidget(checkBox);            // Set the checkbox in the layer
     layoutCheckBox->setAlignment(Qt::AlignCenter);  // Center the checkbox
     layoutCheckBox->setContentsMargins(0,0,0,0);    // Set the zero padding
     /* Check on the status of odd if an odd device,
       * exhibiting state of the checkbox in the Checked, Unchecked otherwise
       * */

      if(state == 1){
          checkBox->setChecked(true);
      } else {
          checkBox->setChecked(false);
      }
      ui->job_table_view->setCellWidget(row_number,column_number, checkBoxWidget);


     // Another way to add check box as item
    /*

   // QTableWidgetItem *checkBoxItem = new QTableWidgetItem("checkbox string ");
    QTableWidgetItem *checkBoxItem = new QTableWidgetItem();
    checkBoxItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    checkBoxItem->setCheckState(Qt::Checked);
    ui->job_table_view->setItem(row_number,column_number,checkBoxItem);

    */
}

// call it like

addCheckBoxAt(0,0,1);  // insert checkbox it 0,0 and check status true
user889030
  • 4,353
  • 3
  • 48
  • 51