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


  • Qt Champions 2016

    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.


  • Moderators

    @gatz
    This is controlled by the model.
    You need to override the following methods:

    1. flags() - return the flags including Qt::ItemIsUserCheckable (only for indieces where you want to show the checkbox)
    2. 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
    3. data() - return QVariant::fromValue<int>( checked ? Qt::Checked : Qt::Unchecked );

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.