Solved QTableView checkboxes
-
Hello,
I'm using QTableView to display data from db.
There is a way to add checkboxes in front of each row, so when I check some checkboxes and I click Delete button to delete those specific rows?
I'm having a hard time finding how to add those checkboxes.Thank you
-
well there is
setCellWidget();or using a delegate
http://www.bogotobogo.com/Qt/Qt5_QTableView_QItemDelegate_ModelView_MVC.php -
I was thinking something like inserting another column on beginning, rather than replacing/modifying existing ones. But this works too in this case. Thanks
P.S.: I'm kind of new to Qt and I find trivial things like this to be a bit overcomplicated, comparing to web development. -
@gatz
This is controlled by the model.
You need to override the following methods:- flags() - return the flags including Qt::ItemIsUserCheckable (only for indieces where you want to show the checkbox)
- setData() - check for the item role Qt::CheckStateRole and convert the passed variant to an int and compare it to Qt::Checked and save the data in your model
- data() - return QVariant::fromValue<int>( checked ? Qt::Checked : Qt::Unchecked );