David Doria
David Doria

Reputation: 10273

Checkboxes in QAbstractListModel not behaving properly

I tried to subclass QAbstractListModel to store an items that each have a string and a bool. In my view the checks are dashed rather than solid, and I am able to check boxes that were previously unchecked, but I cannot uncheck boxes that are checked.

http://programmingexamples.net/wiki/Qt/ModelView/AbstractListModelCheckable

Is there something else I have to do to get these to work like normal checkboxes?

Upvotes: 1

Views: 961

Answers (1)

Frank Osterfeld
Frank Osterfeld

Reputation: 25165

It looks to me like your bool <-> Qt::CheckState conversions go wrong and you end up with Qt::PartiallyChecked (value 1) where you want Qt::Checked.

From your data() implementation::

if(role == Qt::CheckStateRole)
{
    return this->Items[index.row()].Displayed;
}

This looks wrong. You're returning a bool where a Qt::CheckState is expected. Try:

if(role == Qt::CheckStateRole)
{
    return this->Items[index.row()].Displayed ? Qt::Checked : Qt::Unchecked;
}

Also adapt your setData() implementation accordingly:

this->Items[index.row()].Displayed = static_cast<Qt::CheckState>(value.toUInt()) == Qt::Checked;

Alternative: Make Displayed a Qt::CheckState.

Upvotes: 3

Related Questions