ComboBox of CheckBoxes?

前端 未结 4 527
离开以前
离开以前 2020-12-03 03:28

I am trying to make the items in a ComboBox checkable. I tried this:

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

where

4条回答
  •  伪装坚强ぢ
    2020-12-03 03:46

    I have a little addition.

    If one compiles the skyhisi's code then the combobox on Mac OS X doesn't look as combobox with native checkboxes. You can see it on the screenshot.

    enter image description here

    Tested with qt-4.8.5 and 5.1.1.

    It seems like Qt draws these controls by itself. Our team has found the following workaround by pure accident. You can subclass QStyledItemDelegate and reimplement paint() this way:

    void SubclassOfQStyledItemDelegate::paint(QPainter * painter_, const QStyleOptionViewItem & option_, const QModelIndex & index_) const
    {
        QStyleOptionViewItem & refToNonConstOption = const_cast(option_);
        refToNonConstOption.showDecorationSelected = false;
        //refToNonConstOption.state &= ~QStyle::State_HasFocus & ~QStyle::State_MouseOver;
    
        QStyledItemDelegate::paint(painter_, refToNonConstOption, index_);
    }
    

    You can then set this delegate to the combo box by adding the following lines to skyhisi's code:

    SubclassOfQStyledItemDelegate *delegate = new SubclassOfQStyledItemDelegate(this);
    combo->setItemDelegate(delegate);
    

    The comboBox installed with this delegate looks the following way: enter image description here

    On Windows there may be a different issue: text of the checkBoxes has sticked background or dotted border around an item:

    enter image description here

    To change this appearance one can add the following line to the overridden paint just before the line QStyledItemDelegate::paint(painter_, refToNonConstOption, index_) (in the code sample this line was commented):

    refToNonConstOption.state &= ~QStyle::State_HasFocus & ~QStyle::State_MouseOver;
    

    Result:

    enter image description here

提交回复
热议问题