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

南楼画角 提交于 2019-12-02 10:31:08

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!