在 Windows 上,我正在尝试创建具有透明 DOCKWIDGETS 的 Qt 应用程序,其中 dock widget 的背景在 float 时是透明的。所以我们可以通过dock widget来查看。 目前它看起来是黑色的,如下所示。
QDockWidget * dock3 = new QDockWidget(tr("DOCK3 TranslucentBackground"),
textEdit,Qt::FramelessWindowHint);
dock3->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
//dock3->setWindowFlags(dock2->windowFlags()|Qt::FramelessWindowHint);
dock3->setAttribute(Qt::WA_TranslucentBackground);
//dock3->setAttribute(Qt::WA_NoSystemBackground);
{
QWidget* WindowRect = new QWidget(dock3);
QWidget* titleRect = new QLabel ("Title",WindowRect);
titleRect->setFixedSize(QSize(30,60));
titleRect->setStyleSheet("background:rgb(0,0,255);");
QWidget* ContentRect = new QLabel("Content",WindowRect);
ContentRect->setFixedSize(QSize(60,30));
ContentRect->setStyleSheet("background:rgb(0,255,0);");
QVBoxLayout* layout = new QVBoxLayout(WindowRect);
layout->addWidget(titleRect);
layout->addWidget(ContentRect);
dock3->setWidget(WindowRect);
}
最佳答案
一种方法是使用 QDockWidget
的 setWindowOpacity(qreal)
。
但请记住,这会将不透明度应用于 QDockWidget
的所有子项。
供引用:https://doc.qt.io/qt-5/qwidget.html#windowOpacity-prop
另一种方法是使用样式表:
setStyleSheet("background-color: transparent;");
.不幸的是,在您设置基本小部件的属性 WA_TranslucentBackground
之前,这对顶级小部件不起作用。
供引用:
https://doc.qt.io/qt-5/stylesheet.html
https://doc.qt.io/qt-5/qwidget.html#styleSheet-prop
关于qt - 如何制作透明的QT Dock Widget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62040757/