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

旧街凉风 提交于 2019-12-04 06:21:11

问题


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:


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


来源:https://stackoverflow.com/questions/27017332/style-qcomboboxs-sub-control-down-arrow-when-mouse-is-hovering-over-the-qcombob

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