c++ 如何设置QComboBox菜单背景透明?

lxkprmvk  于 2022-12-30  发布在  其他
关注(0)|答案(1)|浏览(420)

我试图创建一个带有圆形边框的组合框,包括弹出菜单,但是当我设置了一个边框半径时,它不适用于背景:

在寻找消除这种背景的方法时,我发现了这个答案:Rounded QComboBox without square box behind
重现:

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);

    QComboBox* comboBox = new QComboBox(this);
    
    comboBox->setStyleSheet(R"(
        QComboBox  {
            border-radius: 8px;
            margin: 10px;
            background-color: gray;
            min-height: 32px;
        }
    
        QComboBox QAbstractItemView {
            border-radius: 8px;
            margin: 10px;
            background-color: gray;
        }
    )");
    
    comboBox->move(100, 100);
    comboBox->addItem("1");
    comboBox->addItem("2");
    
    comboBox->view()->window()->setWindowFlags( Qt::Popup | Qt::FramelessWindowHint |Qt::NoDropShadowWindowHint);
    comboBox->view()->window()->setAttribute(Qt::WA_TranslucentBackground);

}

它确实会移除背景,但会让黑色背景在短时间内可见:

我怎样才能去掉这个黑色的背景?
以下是我已经尝试过但没有成功的所有方法:

if (comboBox->view()->parentWidget())
    {
        auto p = comboBox->view()->parentWidget();
        p->setWindowOpacity(0);
        p->setWindowFlags( Qt::Popup | Qt::FramelessWindowHint |Qt::NoDropShadowWindowHint);
        p->setAttribute(Qt::WA_TranslucentBackground);
        p->setHidden(true);
        p->setAutoFillBackground( false );
    }

    comboBox->setAutoFillBackground( false );
    comboBox->view()->setAutoFillBackground( false );
    comboBox->view()->viewport()->setAutoFillBackground(false);
    comboBox->view()->window()->setAutoFillBackground(false);

    //comboBox->view()->setAttribute(Qt::WA_TranslucentBackground);
    //comboBox->view()->viewport()->setAttribute(Qt::WA_TranslucentBackground);
    //comboBox->view()->window()->setAttribute(Qt::WA_TranslucentBackground);

    comboBox->view()->setWindowOpacity(0);
    comboBox->view()->viewport()->setWindowOpacity(0);
    comboBox->view()->window()->setWindowOpacity(0);

    //comboBox->view()->setHidden(true);
    //comboBox->view()->viewport()->setHidden(true);
    //comboBox->view()->window()->setHidden(true);

    QPalette palette = comboBox->view()->palette();
    palette.setColor(QPalette::Window, Qt::transparent);
    comboBox->view()->setPalette(palette);
fdbelqdn

fdbelqdn1#

试试这个,我以前解决过这个问题。

void setSelectComboBoxStyle(QComboBox* comboBox)
{
    comboBox->setStyleSheet("QComboBox {"
                                "border:2px solid rgb(20,150,225);"
                                "border-radius: 10px;"
                                "padding-left: 5px;"
                                "background-color: white;"
                                "font: bold;"
                                "font-size: 16px;"
                                "color: rgb(107,107,107);}"
                            "QComboBox::drop-down{"
                                "border:none;"
                                "subcontrol-position: top right;"
                                "subcontrol-origin: padding;"
                                "width:40px;}"
                            "QScrollBar:vertical {"
                                "border: 1px transparent rgb(213, 218, 223);"
                                "background:rgb(213, 218, 223);"
                                "border-radius: 2px;"
                                "width:6px;"
                                "margin: 2px 0px 2px 1px;}"
                            "QScrollBar::handle:vertical {"
                                "border-radius: 2px;"
                                "min-height: 0px;"
                                "background-color: rgb(131, 131, 131);}"
                            "QScrollBar::add-line:vertical {"
                                "height: 0px;"
                                "subcontrol-position: bottom;"
                                "subcontrol-origin: margin;}"
                            "QScrollBar::sub-line:vertical {"
                                "height: 0px;"
                                "subcontrol-position: top;"
                                "subcontrol-origin: margin;}");

    QListView *view = new QListView(comboBox);
    view->setStyleSheet("QListView{top: 2px;"
                            "border:2px solid rgb(20,150,225);"
                            "border-radius: 8px;"
                            "background-color: white;"
                            "show-decoration-selected: 1;"
                            "margin-left: 0px;"
                            "margin-top: 6px;"
                            "padding-left: 5px;"
                            "padding-right: 6px;"
                            "padding-top: 6px;"
                            "padding-bottom: 2px;"
                            "font: bold;"
                            "font-size: 16px;}"
                        "QListView::item{"
                            "height: 30px;"
                            "background-color: white;"
                            "color: rgb(107,107,107);}"
                        "QListView::item:hover{"
                            "background-color: rgb(20, 150, 225);"
                            "color: white;}");
    view->setCursor(Qt::PointingHandCursor);
    comboBox->setView(view);
    comboBox->setMaxVisibleItems(5);
    comboBox->setFixedSize(500,45);
    comboBox->view()->window()->setWindowFlags( Qt::Popup | Qt::FramelessWindowHint |Qt::NoDropShadowWindowHint);
}

qt 6.2.3版本:

void setSelectComboBoxStyle(QComboBox* comboBox)
{
 comboBox->setStyleSheet("QComboBox {"
                                    "combobox-popup: 0;"
                                    "background: transparent;"
                                    "border:2px solid rgb(20,150,225);"
                                    "border-radius: 10px;"
                                    "padding-left: 5px;"
                                    "background-color: white;"
                                    "font: bold;"
                                    "font-size: 16px;"
                                    "color: rgb(107,107,107);}"
                                "QComboBox::drop-down{"
                                    "border:none;"
                                    "subcontrol-position: top right;"
                                    "subcontrol-origin: padding;"
                                    "width:40px;}"
                                "QScrollBar:vertical {"
                                    "border: 1px transparent rgb(213, 218, 223);"
                                    "background:rgb(213, 218, 223);"
                                    "border-radius: 2px;"
                                    "width:6px;"
                                    "margin: 2px 0px 2px 1px;}"
                                "QScrollBar::handle:vertical {"
                                    "border-radius: 2px;"
                                    "min-height: 0px;"
                                    "background-color: rgb(131, 131, 131);}"
                                "QScrollBar::add-line:vertical {"
                                    "height: 0px;"
                                    "subcontrol-position: bottom;"
                                    "subcontrol-origin: margin;}"
                                "QScrollBar::sub-line:vertical {"
                                    "height: 0px;"
                                    "subcontrol-position: top;"
                                    "subcontrol-origin: margin;}");

    QListView *view = new QListView(comboBox);
    view->setStyleSheet("QListView{top: 2px;"
                                "border:2px solid rgb(20,150,225);"
                                "border-radius: 8px;"
                                "background-color: white;"
                                "show-decoration-selected: 1;"
                                "padding-left: 5px;"
                                "padding-right: 6px;"
                                "padding-top: 6px;"
                                "padding-bottom: 2px;"
                                "font: bold;"
                                "font-size: 16px;}"
                            "QListView::item{"
                                "height: 30px;"
                                "background-color: white;"
                                "color: rgb(107,107,107);}"
                            "QListView::item:hover{"
                                "background-color: rgb(20, 150, 225);"
                                "color: white;}");
    view->setCursor(Qt::PointingHandCursor);
    comboBox->setView(view);
    comboBox->setMaxVisibleItems(5);
    comboBox->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    comboBox->setFixedSize(500,45);
    comboBox->view()->window()->setWindowFlags( Qt::Popup | Qt::FramelessWindowHint |Qt::NoDropShadowWindowHint);
}

相关问题