Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Different background behaviour for widgets in the same layout
Forum Updated to NodeBB v4.3 + New Features

Different background behaviour for widgets in the same layout

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 928 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pjpetitprez
    wrote on 8 Mar 2017, 14:51 last edited by
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 8 Mar 2017, 22:34 last edited by
      #2

      Hi,

      Can you show a minimal code sample that sets things up for your widget ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pjpetitprez
        wrote on 9 Mar 2017, 08:09 last edited by pjpetitprez 3 Sept 2017, 08:11
        #3

        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.).

        J 1 Reply Last reply 9 Mar 2017, 08:21
        0
        • P pjpetitprez
          9 Mar 2017, 08:09

          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.).

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 9 Mar 2017, 08:21 last edited by
          #4

          @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;}"
          };
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          1
          • P Offline
            P Offline
            pjpetitprez
            wrote on 9 Mar 2017, 13:56 last edited by
            #5

            Thanks for the tip, I have never used stylesheets before, I need to test that.

            1 Reply Last reply
            0

            1/5

            8 Mar 2017, 14:51

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved