【问题标题】:Style QComboBox's sub-control down-arrow when mouse is hovering over the QComboBox via QSS当鼠标通过 QSS 悬停在 QComboBox 上时,样式 QComboBox 的子控件向下箭头
【发布时间】:2015-01-17 00:00:01
【问题描述】:

我知道如何在鼠标悬停时设置QComboBox 的样式:

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

我还知道通过以下方式设置QComboBox 的子控件向下箭头的样式:

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

但是当鼠标通过QSS 悬停在QComboBox 上时,我不知道如何设置QComboBox 的子控件down-arrow。有人有想法吗?

【问题讨论】:

    标签: qt qcombobox qtstylesheets


    【解决方案1】:

    我不知道QSS 是否足够强大(我认为没有),但使用eventfilter 你可以很容易地做到这一点:

    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);
    }
    

    要使用eventFilter,您还应该:

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

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

    【讨论】:

    • 感谢您的解决方案。
    猜你喜欢
    • 2020-02-14
    • 2012-07-14
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    • 2018-01-11
    • 1970-01-01
    相关资源
    最近更新 更多