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. How to create a childwidget with other color as parent
Forum Updated to NodeBB v4.3 + New Features

How to create a childwidget with other color as parent

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.2k 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.
  • L Offline
    L Offline
    Lukas02
    wrote on last edited by
    #1

    I want to make a black mainwindow with a yello label in the window, (not in designer-mode).
    I tried this:

        mainWindow.setFixedSize(1100, 900);
        mainWindow.setPalette(QPalette(Qt::black));
    
        QLabel* brett = new QLabel(&mainWindow);
        brett->setFixedSize(800, 800);
        brett->move(10, 10);
    
        brett->setPalette(QPalette(QPalette::Base, Qt::yellow));
    
        brett->show();
        mainWindow.show();
    

    the mainwindow gets black, correctly, but the label is also black. (because its a child of the mainwindow

    I tried this:

    mainWindow.setFixedSize(1100, 900);
        mainWindow.setPalette(QPalette(Qt::black));
    
        QLabel* brett = new QLabel();
        brett->setFixedSize(800, 800);
        brett->move(10, 10);
    
        brett->setPalette(QPalette(QPalette::Base, Qt::yellow));
    
        brett->show();
        mainWindow.show();
    

    now the label is yello, but its not a child of the mainwindow more, and so its showed in a new window.

    How to change the Color of one Label, without changing the color of the parent?

    raven-worxR Pl45m4P 2 Replies Last reply
    0
    • L Lukas02

      I want to make a black mainwindow with a yello label in the window, (not in designer-mode).
      I tried this:

          mainWindow.setFixedSize(1100, 900);
          mainWindow.setPalette(QPalette(Qt::black));
      
          QLabel* brett = new QLabel(&mainWindow);
          brett->setFixedSize(800, 800);
          brett->move(10, 10);
      
          brett->setPalette(QPalette(QPalette::Base, Qt::yellow));
      
          brett->show();
          mainWindow.show();
      

      the mainwindow gets black, correctly, but the label is also black. (because its a child of the mainwindow

      I tried this:

      mainWindow.setFixedSize(1100, 900);
          mainWindow.setPalette(QPalette(Qt::black));
      
          QLabel* brett = new QLabel();
          brett->setFixedSize(800, 800);
          brett->move(10, 10);
      
          brett->setPalette(QPalette(QPalette::Base, Qt::yellow));
      
          brett->show();
          mainWindow.show();
      

      now the label is yello, but its not a child of the mainwindow more, and so its showed in a new window.

      How to change the Color of one Label, without changing the color of the parent?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @Lukas02
      use "QPalette::Background" for the child widget

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      L 1 Reply Last reply
      0
      • L Lukas02

        I want to make a black mainwindow with a yello label in the window, (not in designer-mode).
        I tried this:

            mainWindow.setFixedSize(1100, 900);
            mainWindow.setPalette(QPalette(Qt::black));
        
            QLabel* brett = new QLabel(&mainWindow);
            brett->setFixedSize(800, 800);
            brett->move(10, 10);
        
            brett->setPalette(QPalette(QPalette::Base, Qt::yellow));
        
            brett->show();
            mainWindow.show();
        

        the mainwindow gets black, correctly, but the label is also black. (because its a child of the mainwindow

        I tried this:

        mainWindow.setFixedSize(1100, 900);
            mainWindow.setPalette(QPalette(Qt::black));
        
            QLabel* brett = new QLabel();
            brett->setFixedSize(800, 800);
            brett->move(10, 10);
        
            brett->setPalette(QPalette(QPalette::Base, Qt::yellow));
        
            brett->show();
            mainWindow.show();
        

        now the label is yello, but its not a child of the mainwindow more, and so its showed in a new window.

        How to change the Color of one Label, without changing the color of the parent?

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #3

        @Lukas02

        You can use a stylesheet for your mainWindow to make the background black and another stylesheet to make your label yellow.

        setStylesheet("QMainWindow#myWindow { background: black; }");
        brett->setStylesheet("QLabel#brett { color: yellow; }");
        

        brett->move(10, 10);

        You usually want to put your label in a layout.


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        L 1 Reply Last reply
        0
        • raven-worxR raven-worx

          @Lukas02
          use "QPalette::Background" for the child widget

          L Offline
          L Offline
          Lukas02
          wrote on last edited by
          #4

          @raven-worx
          i already tried this. But thx

          QLabel* brett = new QLabel(&mainWindow);
              brett->setFixedSize(800, 800);
              brett->move(10, 10);
          
              brett->setPalette(QPalette(QPalette::Background, Qt::yellow));
          

          the same problem

          1 Reply Last reply
          0
          • Pl45m4P Pl45m4

            @Lukas02

            You can use a stylesheet for your mainWindow to make the background black and another stylesheet to make your label yellow.

            setStylesheet("QMainWindow#myWindow { background: black; }");
            brett->setStylesheet("QLabel#brett { color: yellow; }");
            

            brett->move(10, 10);

            You usually want to put your label in a layout.

            L Offline
            L Offline
            Lukas02
            wrote on last edited by
            #5

            @Pl45m4
            The StyleShet - version fixed the problem. Thak you.
            But I dont understand my mistake in my first version.

            Pl45m4P 1 Reply Last reply
            0
            • L Lukas02

              @Pl45m4
              The StyleShet - version fixed the problem. Thak you.
              But I dont understand my mistake in my first version.

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by Pl45m4
              #6

              @Lukas02 said in How to create a childwidget with other color as parent:

              But I dont understand my mistake in my first version.

              QWidget propagates explicit palette roles from parent to child. If you assign a brush or color to a specific role on a palette and assign that palette to a widget, that role will propagate to all the widget's children, overriding any system defaults for that role. Note that palettes by default don't propagate to windows (see isWindow()) unless the Qt::WA_WindowPropagation attribute is enabled.

              (from: https://doc.qt.io/qt-5/qwidget.html#palette-prop)


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              1 Reply Last reply
              4

              • Login

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