QtComboBox style, QListViewItem - setting text

Info: Qt 4.8, Qt Designer

I'm struggling to set the addition of text elements in a drop down list. My understanding is all this is a QComboBox widget, and the dropdown is a QListView. But I can not decide which css selector to use to change the style of text elements in the drop-down list.

I want the background color of the hover element to expand all the way, instead of leaving the 5px box. The text itself is well built, only the white space on the left calls me.

It would also be great if I could remove the dashed border around the selected item.

enter image description here

I guess I'm just not using the right selector. I tried QListView :: item and QListViewItem. Here is my CSS applied to combobox.

QComboBox{
border:                 none;
background-color:   rgb(87, 96, 134);
color:                      rgb(255, 255, 255);
font-weight:            bold;
padding:                    5px 

}

QComboBox::drop-down{
    border:                 none;
    background-color:   rgb(87, 96, 134);
    color:                      rgb(255, 255, 255);
    font-weight:            bold;
    padding:                    0px;
}

QComboBox::down-arrow{
    image:                      url(:/icons/combobox_down_arrow.png);
    padding-right:          5px;
}

QListView{
    border:                 none;
    color:                      rgb(87, 96, 134);
    background-color:   rgb(255, 255, 255);
    font-weight:            bold;
    selection-background-color: rgb(47, 175, 178);
    show-decoration-selected: 1;
    margin-left:                -10px;
    padding-left    :           15px;
}

QListView::item:hover{

    background-color:   rgb(47, 175, 178);
    border:                 none;
}

Thoughts?

+4
1

, CSS, outline:

QComboBox QAbstractItemView {
    outline: none;
}

QListView . QAbstractItemView , QListView. , , .

, QT - CSS:


, , . QComboBox QAbstractItemDelegate QItemDelegate. QStyledItemDelegate ( QStyledItemDelegate vs. QItemDelegate). CSS (QListView::item) QComboBox.

:

, QStyledItemDelegate
Vista ,

, QStyledItemDelegate QComboBox :

QComboBox *combobox = new QComboBox;
combobox->setItemDelegate(new QStyledItemDelegate(combobox));

selection-background-color . ::item:

QComboBox QAbstractItemView::item {
    border: none;
    padding-left: 5px;
}

QComboBox QAbstractItemView::item:selected {
    background: rgb(47, 175, 178);
    padding-left: 5px;
}

, , , . .

:hover ::item:selected, .


, QComboBox, . QComboBox . , QComboBox . .

QStyledItemDelegate Qt QComboBoxDelegate. . Qt. QComboBoxDelegate Qt5 Qt4. Qt5 Qt4, Qt4. QItemDelegate QStyledItemDelegate:

class ComboBoxDelegateStyled : public QStyledItemDelegate
{ 
    Q_OBJECT
public:
    ComboBoxDelegateStyled(QObject *parent, QComboBox *cmb) :
        QStyledItemDelegate(parent), mCombo(cmb) {}

    static bool isSeparator(const QModelIndex &index) {
        return index.data(Qt::AccessibleDescriptionRole).toString() == QLatin1String("separator");
    }

protected:
    void paint(QPainter *painter,
               const QStyleOptionViewItem &option,
               const QModelIndex &index) const {
        if (isSeparator(index)) {
            QRect rect = option.rect;
            if (const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3*>(&option))
                if (const QAbstractItemView *view = qobject_cast<const QAbstractItemView*>(v3->widget))
                    rect.setWidth(view->viewport()->width());
            QStyleOption opt;
            opt.rect = rect;
            mCombo->style()->drawPrimitive(QStyle::PE_IndicatorToolBarSeparator, &opt, painter, mCombo);
        } else {
            QStyledItemDelegate::paint(painter, option, index);
        }
    }

    QSize sizeHint(const QStyleOptionViewItem &option,
                   const QModelIndex &index) const {
        if (isSeparator(index)) {
            int pm = mCombo->style()->pixelMetric(QStyle::PM_DefaultFrameWidth, 0, mCombo);
            return QSize(pm, pm);
        }
        return QStyledItemDelegate::sizeHint(option, index);
    }
private:
    QComboBox *mCombo;
};

QComboBox ComboBoxDelegateStyled:

class ComboBoxStyled : public QComboBox
{
public:
    explicit ComboBoxStyled(QWidget *parent = 0) : QComboBox(parent) {
        setItemDelegate(new ComboBoxDelegateStyled(this, this));
    }
};

QComboBox ComboBoxStyled. combo box, CSS ::item.

QComboBox separator: QCombobox


PyQt

PyQt. outline CSS ::item:

styledComboBox = QtGui.QComboBox()
delegate = QtGui.QStyledItemDelegate()
styledComboBox.setItemDelegate(delegate)

combo box . .

+1

Source: https://habr.com/ru/post/1613656/


All Articles