如何将QAction设置为在QToolBar中具有透明背景

How to set QAction to have transparent background inside QToolBar

创建 QToolBar 后,我用图标向其添加 QAction,但是当我设置 QToolBar StyleSheet 时,QAction 将具有白色背景而不是透明背景,如下图所示

代码:

QToolBar *toolBarFile = addToolBar("File");
toolBarFile->setMinimumWidth(70);
this->addToolBar(Qt::LeftToolBarArea, toolBarFile);
toolBarFile->setMovable(false);
toolBarFile->setStyleSheet("*{border: none;}"
                           "QToolBar{background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0, stop: 0 #f2f2f2, stop: 0.8 #f2f2f2, stop: 1 #999999);}");

QAction* ActionUniversal = new QAction(QIcon(":/DownArrow.png"), "&Open", this);
toolBarFile->addAction(ActionUniversal);

ActionUniversal = new QAction(QIcon(":/DownArrow.png"), "&Open", this);
toolBarFile->addAction(ActionUniversal);

ActionUniversal = new QAction(QIcon(":/DownArrow.png"), "&Open", this);
toolBarFile->addAction(ActionUniversal);

ActionUniversal = new QAction(QIcon(":/DownArrow.png"), "&Open", this);
toolBarFile->addAction(ActionUniversal);

显示图像背景透明度的另一个示例:

我的代码发生了什么变化:

toolBarFile->setStyleSheet("border: none;"
                               "background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0, stop: 0 #f2f2f2, stop: 0.8 #f2f2f2, stop: 1 #999999);");

试试这个:

toolBarFile->setStyleSheet("QToolButton{border: none; background-color: transparent;} "
                           "QToolBar{background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0, stop: 0 #f2f2f2, stop: 0.8 #f2f2f2, stop: 1 #999999);}");