我试图创建一个带有圆形边框的组合框,包括弹出菜单,但是当我设置了一个边框半径时,它不适用于背景:
在寻找消除这种背景的方法时,我发现了这个答案: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);
1条答案
按热度按时间fdbelqdn1#
试试这个,我以前解决过这个问题。
qt 6.2.3版本: