Nominate our 2022 Qt Champions!

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

  • Lifetime Qt Champion

    well there is

    or using a delegate

  • 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

    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