PDA

View Full Version : ListModel with CheckStateRole but don't show checkboxes in QComboBox



xtal256
9th March 2012, 11:54
I have a subclass of QAbstractListModel containing items consisting of a name (QString) and a boolean. The model returns the name as Qt::DisplayRole, and the boolean as Qt::CheckStateRole. I then use this model in a list view.

But i would also like to use the same model for combo boxes. This is in the same UI, using the same data but in two different views.

The problem is that although the list view displays the check boxes for each item, the combo box does as well. Is there any way i can set it to not show these? Or will i have to use a separate model for the combo boxes?

xtal256
10th March 2012, 23:43
Anyone?

I looked in the documentation for a way to make QComboBox only show the "display" role, but i couldn't find anything.

I guess i could subclass QComboBox to not show the check boxes, but i don't want to go that far. If that's the case, i will just create a QStringListModel for the combo boxes, and copy the strings into it.

I also thought about using two columns in the model, one for just the check state and one for the string. But QListView can't display multiple columns, can it?

ChrisW67
11th March 2012, 04:30
You could put a QAbstractProxyModel or QSortFilterProxyModel derivative in between the original model and the combo box. Have the filter data() function return QVariant() for the check role and whatever the underlying model offers for other roles.

xtal256
11th March 2012, 22:42
No, i don't want to go that far. I will just go with using a separate QStringListModel.
But thanks for your help.

norobro
11th March 2012, 22:52
On my Linux box the QCleanlooksStyle does not show a checkbox in a QComboBox. If you can live with that style . . .

ChrisW67
12th March 2012, 06:05
No, i don't want to go that far.
You don't want to trivially address the problem and would rather duplicate data? Curious, but each to their own.


Here is what you are avoiding:


class CheckStateFilterProxy: public QSortFilterProxyModel
{
Q_OBJECT
public:
CheckStateFilterProxy(QObject * parent = 0):
QSortFilterProxyModel(parent)
{ }

QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
{
if (role == Qt::CheckStateRole)
return QVariant();
return QSortFilterProxyModel::data(index, role);
}
};

xtal256
12th March 2012, 11:06
You don't want to trivially address the problem and would rather duplicate data? Curious, but each to their own.
Yes, but i am more concerned with the amount of effort required, not whether a few extra kilobytes of memory is used.

By comparison, this is what i ended up going with:


QStringList names;
QList<MyStruct>::const_iterator i;
for (i = list.begin(); i != list.end(); i++) {
names.append(i->name);
}
stringListModel.setStringList(names);

I see that your way isn't too difficult either, but my way will do, so i'll leave it at that.