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

后端 未结 1 764
半阙折子戏
半阙折子戏 2021-01-26 03:32

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

pComboBox->setStyleSheet(pComboBox->styleSheet()+QString(\"  QComboBox:hover{cs         


        
相关标签:
1条回答
  • 2021-01-26 03:55

    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
    
    0 讨论(0)
提交回复
热议问题