yan bellavance
yan bellavance

Reputation: 4850

How can I add a user editable checkbox in QTableView using only QStandardItemModel

I have a QTableView and a QStandardItemModel. Is there have a column can contain checkboxes that are user editable without using delegates or using the abstract model classes? It is not that I can't do it, I just want to minimize the code, I would find it overkill for simple check boxes.

By using model.setData(index, Qt::Unchecked,Qt::CheckStateRole) this creates the checkbox but it is not user editable (text beside checkbox is).

I used modelTX.setData(index, FALSE) but this creates a combo box containing True and False.

I'll try setItemData.

Upvotes: 11

Views: 16258

Answers (1)

serge_gubenko
serge_gubenko

Reputation: 20492

pls, check if the following example would work for you:

QStandardItemModel* tableModel = new QStandardItemModel();
// create text item
tableModel->setItem(0, 0, new QStandardItem("text item"));
// create check box item
QStandardItem* item0 = new QStandardItem(true);
item0->setCheckable(true);
item0->setCheckState(Qt::Checked);
item0->setText("some text");
tableModel->setItem(0, 1, item0);
// set model
ui->tableView->setModel(tableModel);

hope this helps, regards

Upvotes: 25

Related Questions