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 prevent Qt child windows from inheriting the style of the parent window?
Forum Updated to NodeBB v4.3 + New Features

How to prevent Qt child windows from inheriting the style of the parent window?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 315 Views 1 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.
  • A Offline
    A Offline
    Aizzzz
    wrote on last edited by
    #1

    For example, there are two windows named ParentWidget and ChildWidget, both of which have a QPushButton control with the default name pushButton.
    Now, create a widget in ParentWidget, and promote it to ChildWidget. Then I set the following style sheet for the outermost window in ParentWidget:

    #ParentWidget QPushButton#pushButton{ background:#1785E6; }
    

    After the program runs, this style sheet will affect both the button in ParentWidget and the button in ChildWidget. How can I make it only affect the controls in ParentWidget?

    Pl45m4P 1 Reply Last reply
    0
    • A Aizzzz

      For example, there are two windows named ParentWidget and ChildWidget, both of which have a QPushButton control with the default name pushButton.
      Now, create a widget in ParentWidget, and promote it to ChildWidget. Then I set the following style sheet for the outermost window in ParentWidget:

      #ParentWidget QPushButton#pushButton{ background:#1785E6; }
      

      After the program runs, this style sheet will affect both the button in ParentWidget and the button in ChildWidget. How can I make it only affect the controls in ParentWidget?

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

      @Aizzzz said in How to prevent Qt child windows from inheriting the style of the parent window?:

      How can I make it only affect the controls in ParentWidget?

      ParentWidget > QPushButton { ... }
      

      This will only affect QPushButtons which are direct children of ParentWidget.

      • https://doc.qt.io/qt-6/stylesheet-syntax.html#selector-types

      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
      2
      • IPlayGenji6I Offline
        IPlayGenji6I Offline
        IPlayGenji6
        wrote on last edited by
        #3

        Add the property parentName to all your QPushButton elements in parentWidget, and set the value to "parentWidget" for each of them.

        QObjectList buttons = ParentWidget->findChildren<QPushButton*>();
        for (QPushButton* button : buttons) {
                button->setProperty("parentName", "parentWidget");
        }
        

        in qss:

        QPushButton[parentName = "parentWidget"]
        {
        background: #123456;
        }
        

        1.png

        1 Reply Last reply
        0

        • Login

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