Important: Please read the Qt Code of Conduct -

QTableView: show checkbox, but keep it disabled

  • Hi all,

    I'm working with my implementation of QAbstractTableModel, which to state upfront, I have no issues with the ability to add checkbox columns to my QTableView.

    My question, rather- are you able to show the checkboxes such that an individual can see them, see their state, but not interact? Think - a signed in user doesn't have the permissions to change the value, but has the ability to read it.

    I was hoping that under Qt::ItemFlags there would be an ItemIsDisabled like ItemIsEnabled, but I don't see anything in 5.9.4.

    Any tips or tricks would be greatly appreciated!

    Thank you

  • @HunterMetcalfe

    Are you using delegates?
    Can't you just chkBx->setEnabled(false) these checkboxes?

  • @Pl45m4

    I'm not using delegates for my checkboxes I'm curious to know more in particular.
    I'm setting the flag 'ItemIsCheckable' on a certain column in the reimplemented QAbstractTableModel::flags function. I handle the value of the checkbox through a flag within the structure populating the other columns, which is populated when QAbstractTableModel::data and QAbstractTableModel::setData are called. I believe my solution is going to be to change my reimplemented QAbstractTableModel::setData function to add a flag to essentially ignore user input, hence retaining the original state.

  • Hey, I can't find any flag named 'ItemIsCheckable', only ItemIsUserCheckable, which can control the interacting from users, exactly as you asked.
    It might not look like disabled, but users cannot check or uncheck without this flag.
    P.S. disabled == not enabled, so: without ItemIsEnabled == item disabled.

  • @HunterMetcalfe

    Your way is already the easiest one. ItemIsUserCheckable turns your item into a checkbox. So use a combination of ItemIsEnabled and ItemIsUserCheckable, as also suggested by @Bonnie . If you take or don't set the enabled-flag, your item still has a checkBox, but you can not interact with it.

  • @Pl45m4
    Sorry to say this but that's not I meant.

    As I tested, ItemIsUserCheckable flag doesn't turn the item into a checkbox, it just controls whether user can check / uncheck the checkbox if there is one.
    Whether there will be a checkbox is determined by the value of data(Qt::CheckStateRole) is valid or not.

    So if the item has ItemIsEnabled flag and no ItemIsUserCheckable flag, and data(Qt::CheckStateRole) returns a valid value, then it will have a checkbox. The item is enabled, but the user cannot change the check state by clicking the checkbox.

    If the item has no ItemIsEnabled flag, then no matter it has ItemIsUserCheckable flag or not, the user cannot change the check state, because it is disabled.

  • @Bonnie said in QTableView: show checkbox, but keep it disabled:

    As I tested, ItemIsUserCheckable flag doesn't turn the item into a checkbox, it just controls whether user can check / uncheck the checkbox if there is one.

    Haven't tested myself, but AFAIK there is this flag which automatically adds a checkBox to your item. Thought, this was ItemIsUserCheckable, but could be wrong...

    Edit: It works for QListWidget items... Though it will work with QTableWidget / QTableView as well, but apparently the flag is handled differently in both models / views

  • @Pl45m4

    Haven't tested myself,

    No, @Bonnie is correct on this one (QTableView at least) :) The data(Qt::CheckStateRole) determines whether there is a checkbox, the ItemIsUserCheckable determines whether the user can change its state (only useful if it has a checkbox).

  • @JonB

    I could swear, I once just set the flag without changing any role and it changed the item (added a checkBox). But good to know :)

Log in to reply