Results 1 to 8 of 8

Thread: Multi-selection Combo Box

  1. #1
    Join Date
    Oct 2007
    Location
    Quebec, Canada
    Posts
    40
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Multi-selection Combo Box

    Hi, I'm trying to implement a combo box that would allow more than one selection(for flag purpose). I want the combo box to display check boxes in it instead of plain strings.

    I already got that part covered usung a delegate on the combo box's list view for painting.

    Now I need to be able to check the check boxes from within the list view. I tried implementing createEditor in my item delegate but it doesn't get called.

    One last question, I would like to display the combined flags in the combo box's edit like Flag1 | Flag2 | Flag4. Is it possible to do it instead of the last selected item ?

    Here's my code for the delegate in case you're interested :

    Qt Code:
    1. void AcuiMultiSelectionComboBoxItemDelegate::paint(
    2. QPainter* painter,
    3. const QStyleOptionViewItem& option,
    4. const QModelIndex& index ) const
    5. {
    6. QStyleOptionButton buttonStyle;
    7. buttonStyle.text = index.model()->data(index, Qt::DisplayRole).toString();
    8. buttonStyle.rect = option.rect;
    9.  
    10. qApp->style()->drawControl(
    11. QStyle::CE_CheckBox,
    12. &buttonStyle,
    13. painter );
    14. }
    15.  
    16. QWidget* AcuiMultiSelectionComboBoxItemDelegate::createEditor(
    17. QWidget* parent,
    18. const QStyleOptionViewItem& option,
    19. const QModelIndex& index ) const
    20. {
    21. QCheckBox* checkBox = new QCheckBox(
    22. index.model()->data(index, Qt::DisplayRole).toString(),
    23. parent );
    24.  
    25. return checkBox;
    26. }
    To copy to clipboard, switch view to plain text mode 

    Feel free to give any comments, recommendation.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Multi-selection Combo Box

    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    EricF (5th November 2007)

  4. #3
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multi-selection Combo Box

    You can use Qxt for your checkable combo boxes
    http://www.libqxt.org/static/docs/cl...kComboBox.html

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multi-selection Combo Box

    If you want to avoid the above mentioned dependency, you can set a model on the combobox and make sure the model enables items to be checkable (like Designer does for setting properties that are sets of flags).

  6. The following user says thank you to wysota for this useful post:

    EricF (5th November 2007)

  7. #5
    Join Date
    Oct 2007
    Location
    Quebec, Canada
    Posts
    40
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multi-selection Combo Box

    Thanks for all the suggestions, I didn't know libqxt. And thanks for a work around.

  8. #6
    Join Date
    Oct 2007
    Location
    Quebec, Canada
    Posts
    40
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multi-selection Combo Box

    I tried the work around and discovered that I didn't have to draw the combo box myself, setting the flag and handling the Qt::CheckStateRole in data() method does the job for me and even better because the check box I get is nicer than the one I was drawing. I would have like to see it in the documentation though.

    However, it does not seem to handle the checking itself. Clicking on the check box selects the string (Normal behavior) but does not check the check box. Why ?

    Is there some extensive documentation on model/view framework to read so I can get some insight to use it ?

    Edit: I'll probably end up using QxtCheckComboBox but for the sake of learning I try to do it by myself.

  9. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multi-selection Combo Box

    Mouse events of the popup are intercepted by the combobox. You need to remove the event filter from the combobox or apply your own to make sure the view gets the event.

  10. #8
    Join Date
    Sep 2007
    Location
    Szczecin, Poland
    Posts
    153
    Thanks
    7
    Thanked 11 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multi-selection Combo Box

    hello,
    there's another way - it's lots of work, but gives also lots of possibilities:
    inheriting combobox that provides own tree/list view/widget

    There's some work to do with showing view as popup, hiding it when it loses focus etc. but You have FULL control over a popup including i.e. hierarchical views(like in file dialogs), more than 1 column shown in dropdown item & other non standard goodies you can imagine... but as i said, there's number of things that are for free in combobox or QxtCheckComboBox and have to be programmed in inherited classes.

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.