Style QComboBox's sub-control down-arrow when mouse is hovering over the QComboBox via QSS

1.2k views Asked by At

I know how to style QComboBox when mouse is hovering by doing:

pComboBox->setStyleSheet(pComboBox->styleSheet()+QString("  QComboBox:hover{css style here}"))

And I also know to style QComboBox's sub-control down-arrow's style via:

pComboBox->setStyleSheet(pComboBox->styleSheet()+QString("  QComboBox::down-arrow{css style here}"))

But I don't know how to style QComboBox's sub-control down-arrow when the mouse is hovering over the QComboBox via QSS. Does anybody have an idea?

1

There are 1 answers

0
Jablonski On

I don't know is QSS powerful enough to do this(I think no), but with eventfilter you can do this very easy:

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{

    if (obj == ui->comboBox && event->type() == QEvent::Enter)
    {
        //user enters combobox, so we apply stylesheet
        ui->comboBox->setStyleSheet("QComboBox::down-arrow{background-color: red}");
    }
    else
        if(event->type() == QEvent::Leave)//user leaves combobox, so we set default settings
            ui->comboBox->setStyleSheet("");

    return QObject::eventFilter(obj, event);
}

To use eventFilter you should also:

protected:
    bool eventFilter(QObject *obj, QEvent *event);//in header

and

qApp->installEventFilter(this);//in constructor