How can I add a checkbox/radio button to QTableWidget
Asked Answered
D

3

34

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

Doolittle answered 20/3, 2011 at 15:9 Comment(0)
S
21

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.

Spaceport answered 20/3, 2011 at 22:57 Comment(6)
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.Layla
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.Spaceport
Can you elaborate on your "QListWidget and QTableWidget. 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 QTableWidgetSpaceport
And where exactly does it say there "Its impossible to reiplement delegates for them"?Spaceport
This answer is simply incorrect. The correct answer is below.Likable
Further evidence that StackOverflow should default to dynamically marking the answer currently receiving the most upvotes as "accepted."Kroll
L
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.

Layla answered 20/3, 2011 at 17:16 Comment(3)
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.Blenny
you should check with checkbox.checkState() == 0 or == 2 one unckecked and another ckecked , i excampled with python , you should convert . to ->Fidellas
@MohsenPahlevanzadeh: Please avoid using literal numbers when you can use aptly named constants: Qt::CheckState. 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).Stralka
S
21

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.

Spaceport answered 20/3, 2011 at 22:57 Comment(6)
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.Layla
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.Spaceport
Can you elaborate on your "QListWidget and QTableWidget. 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 QTableWidgetSpaceport
And where exactly does it say there "Its impossible to reiplement delegates for them"?Spaceport
This answer is simply incorrect. The correct answer is below.Likable
Further evidence that StackOverflow should default to dynamically marking the answer currently receiving the most upvotes as "accepted."Kroll
C
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
Chader answered 17/12, 2019 at 12:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.