Results 1 to 7 of 7

Thread: ListModel with CheckStateRole but don't show checkboxes in QComboBox

  1. #1
    Join Date
    Aug 2010
    Posts
    99
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default ListModel with CheckStateRole but don't show checkboxes in QComboBox

    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?
    Last edited by xtal256; 9th March 2012 at 11:03.
    [Window Detective] - Windows UI spy utility
    [War Thunder]

  2. #2
    Join Date
    Aug 2010
    Posts
    99
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: ListModel with CheckStateRole but don't show checkboxes in QComboBox

    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?

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: ListModel with CheckStateRole but don't show checkboxes in QComboBox

    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.

  4. #4
    Join Date
    Aug 2010
    Posts
    99
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: ListModel with CheckStateRole but don't show checkboxes in QComboBox

    No, i don't want to go that far. I will just go with using a separate QStringListModel.
    But thanks for your help.
    [Window Detective] - Windows UI spy utility
    [War Thunder]

  5. #5
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: ListModel with CheckStateRole but don't show checkboxes in QComboBox

    On my Linux box the QCleanlooksStyle does not show a checkbox in a QComboBox. If you can live with that style . . .

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: ListModel with CheckStateRole but don't show checkboxes in QComboBox

    Quote Originally Posted by xtal256 View Post
    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:
    Qt Code:
    1. class CheckStateFilterProxy: public QSortFilterProxyModel
    2. {
    3. Q_OBJECT
    4. public:
    5. CheckStateFilterProxy(QObject * parent = 0):
    6. { }
    7.  
    8. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
    9. {
    10. if (role == Qt::CheckStateRole)
    11. return QVariant();
    12. return QSortFilterProxyModel::data(index, role);
    13. }
    14. };
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Aug 2010
    Posts
    99
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: ListModel with CheckStateRole but don't show checkboxes in QComboBox

    Quote Originally Posted by ChrisW67 View Post
    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:
    Qt Code:
    1. QList<MyStruct>::const_iterator i;
    2. for (i = list.begin(); i != list.end(); i++) {
    3. names.append(i->name);
    4. }
    5. stringListModel.setStringList(names);
    To copy to clipboard, switch view to plain text mode 

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

Similar Threads

  1. ListModel.remove() crashes program
    By truefusion in forum Qt Quick
    Replies: 5
    Last Post: 5th February 2012, 15:27
  2. Show QComboBox list while editing
    By radix07 in forum Qt Programming
    Replies: 0
    Last Post: 14th October 2011, 16:52
  3. to show Checkboxes in listview
    By Ketan Shah in forum Newbie
    Replies: 3
    Last Post: 23rd May 2011, 09:01
  4. checkboxes
    By abrou in forum Newbie
    Replies: 2
    Last Post: 1st February 2008, 18:52
  5. A simplest ListModel... no example :(
    By tomek in forum Newbie
    Replies: 5
    Last Post: 7th January 2006, 00:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.