Unsolved Different background behaviour for widgets in the same layout
-
Hello,
I am wondering if it is possible to get different background behaviours between widgets that are in the same layout. I would like for example to make some of them have a transparent background (so that we can see through), and some other sibling widgets should keep an opaque background.
For now I manage to get a fully transparent background by applying this attribute to the parent widget:parentWidget->setAttribute(Qt::WA_TranslucentBackground);
This works well and all the widgets placed in its layout follow the behaviour. I was thinking it was possible to override the parent attribute by setting the attribute to false on the child widget, but it doesn't work.
It is possible to achieve this?
My solution for the moment is to separate the widgets which have a different behaviour into different parent layouts, but I'd like to use only one layout if possible.Thanks
-
Hi,
Can you show a minimal code sample that sets things up for your widget ?
-
Here is my code :
// Creating the main "parent" widget QVBoxLayout* vBoxLayout = new QVBoxLayout(); vBoxLayout->setContentsMargins(0, 0, 0, 0); QWidget* mainWidget = new QWidget; mainWidget ->setLayout(vBoxLayout); // Creating and adding child widgets in its layout QWidget * childWidget1 = new QWidget; mainWidget->layout()->addWidget(childWidget1); QWidget * childWidget2 = new QWidget; mainWidget->layout()->addWidget(childWidget2); QWidget * childWidget3 = new QWidget; mainWidget->layout()->addWidget(childWidget3);
And now if I do that :
mainWidget->setWindowFlags(Qt::FramelessWindowHint); mainWidget->setAttribute(Qt::WA_TranslucentBackground);
I get the main widget and all child widgets with a transparent background.
So my question was : if I want childWidget1 and childWidget2 to have transparent background, but let childWidget3 have an opaque background, is it possible without removing childWidget3 from the parent layout ?
I tried this :
childWidget3->setAttribute(Qt::WA_TranslucentBackground, false);
but this doesn't work, I assume the attribute from the parent widget cannot be overriden in a child widget. So my current solution is to manage childWidget3 separately from childWidget1 and 2, but it would simplify things if I could let it in the mainWidget's layout (no need for manual positioning etc.).
-
@pjpetitprez said in Different background behaviour for widgets in the same layout:
Would something like this help?
// Creating the main "parent" widget QVBoxLayout* vBoxLayout = new QVBoxLayout(); vBoxLayout->setContentsMargins(0, 0, 0, 0); QWidget* mainWidget = new QWidget; mainWidget ->setLayout(vBoxLayout); mainWidget->setObjectName("parent"); // Creating and adding child widgets in its layout QWidget * childWidget1 = new QWidget; childWidget1->setObjectName("Child1"); mainWidget->layout()->addWidget(childWidget1); QWidget * childWidget2 = new QWidget; childWidget2->setObjectName("Child2"); mainWidget->layout()->addWidget(childWidget2); QWidget * childWidget3 = new QWidget; childWidget3->setObjectName("Child3"); mainWidget->layout()->addWidget(childWidget3); mainWidget->setStyleSheet{ "QWidget#parent{background-color:transparent;}" "QWidget#Child1{background-color:white;}" "QWidget#Child2{background-color:blue;}" "QWidget#Child3{background-color:red;}" };
-
Thanks for the tip, I have never used stylesheets before, I need to test that.