Qt World Summit: Register Today!

QTreeView: "Auto-Checkboxes" and "Checkbox only"?

  • Hello,

    Other than overriding the flags() and data(Qt::CheckState) members of my QAbstractItemModel based model, is there a way to induce QTreeView to render checkboxes (and tristate boxes) for certain columns of my model? If so, how? Can it be based on datatype returned by data(Qt::DisplayRole)?

    Secondly, when QTreeView renders a checkbox, it also seems to leave an area for text. It's even editable(!). How can just render the checkbox?

    Thank you

  • Hi,

    to enable the check boxes, you need the flags (Qt::ItemIsUserCheckable), If you set Qt::ItemIsEditable, it will be editable by an editfield.

  • Gerolf,

    Yeah, I've got the check boxes by setting those flags on my data model.

    To clarify: I was wondering if there was a different way, since the choice of how to render a given data type would seem the responsibly of the view, not the model.

    Something like:
    treeView->setColumnRenderRule(colNum1, QTextRenderRule);
    treeView->setColumnRenderRule(colNum2, QCheckboxRenderRule);

    or even:

    treeView->setCellRenderRule(QBool, QCheckboxRenderRule, optionalColumnNumber);

  • You could use a delegate, but that seems overkill. What might be a compromise, is to use a proxy model to do this. There is even one ready made available on the wiki in the snippets section...

  • Thanks, Andre. I'll take a look.

Log in to reply