QComboBox drop-down list - set selected item style

后端 未结 3 1310
走了就别回头了
走了就别回头了 2021-01-12 00:49

Is it possible to set selected item style (Qt style sheet) of the QComboBox drop-down list?

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-12 01:14

    @Sergey Vlasov: I don't know if there is a better solution to your problem but , but I managed to solve it with the following:

    class MyDelegate : public QStyledItemDelegate
    {
    protected:
        void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
        {
            if (option.state & QStyle::State_HasFocus){
                QStyleOptionViewItem my_option = option;
                my_option.state = my_option.state ^ QStyle::State_HasFocus;
                QStyledItemDelegate::paint(painter, my_option, index);
                return;
            }
            QStyledItemDelegate::paint(painter, option, index);
        }
    };
    

    And then using your delegate in your combobox:

    QStyledItemDelegate* itemDelegate = new MyDelegate();
        combobox->setItemDelegate(itemDelegate);
    

    this eliminates nasty frame around selected item

提交回复
热议问题