Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

qt

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.

like image 213
yan bellavance Avatar asked Dec 04 '09 20:12

yan bellavance


1 Answers

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

like image 146
serge_gubenko Avatar answered Nov 20 '22 14:11

serge_gubenko