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 change background-color of widget, but not items
QtWS25 Last Chance

How to change background-color of widget, but not items

Scheduled Pinned Locked Moved Solved General and Desktop
qwidgetqstylesheet
4 Posts 3 Posters 2.6k Views
  • 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.
  • H Offline
    H Offline
    hobbyProgrammer
    wrote on 9 Jan 2020, 10:22 last edited by
    #1

    I would like to create a widget with a background color, but I do not want to change the color of the buttons & comboboxes.

    I already tried this:

    QString style = "background-color: #D8A09F";
    this->setStyleSheet(style);
    

    which sets the background to the color I desire but it also changes the color of any item in the widget.
    Would somebody like to help me out?
    Thanks!

    J J 2 Replies Last reply 9 Jan 2020, 10:28
    0
    • H hobbyProgrammer
      9 Jan 2020, 10:22

      I would like to create a widget with a background color, but I do not want to change the color of the buttons & comboboxes.

      I already tried this:

      QString style = "background-color: #D8A09F";
      this->setStyleSheet(style);
      

      which sets the background to the color I desire but it also changes the color of any item in the widget.
      Would somebody like to help me out?
      Thanks!

      J Offline
      J Offline
      JonB
      wrote on 9 Jan 2020, 10:30 last edited by JonB 1 Sept 2020, 10:31
      #3

      @hobbyProgrammer
      I do not know whether this would make the difference, but if you widget is of some type YourWidget you could try QString style = "YourWidget { background-color: #D8A09F; }"; to see if by applying that to the specified widget stops it propagating to child widgets?

      [Looks like @J-Hilk types faster than I can... :) ]

      J 1 Reply Last reply 9 Jan 2020, 10:36
      1
      • H hobbyProgrammer
        9 Jan 2020, 10:22

        I would like to create a widget with a background color, but I do not want to change the color of the buttons & comboboxes.

        I already tried this:

        QString style = "background-color: #D8A09F";
        this->setStyleSheet(style);
        

        which sets the background to the color I desire but it also changes the color of any item in the widget.
        Would somebody like to help me out?
        Thanks!

        J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 9 Jan 2020, 10:28 last edited by J.Hilk 1 Sept 2020, 10:35
        #2

        hi @hobbyProgrammer
        by using specifiers of course :)

        first of define the Qt Widget type you want to target e.G. QPushButton

        QString style = "QPushButton{background-color: #D8A09F;}"
        

        now you can further narrow it down by using the objectName property. Widgets added via QtDesigner always have a unique objectName. Widgets created and added via Code do not have one by default, and you'll have to set it first.

        for example your QPushButton has the objectName btnHome

        QString style = "QPushButton#btnHome{background-color: #D8A09F;}"
        

        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
        4
        • H hobbyProgrammer
          9 Jan 2020, 10:22

          I would like to create a widget with a background color, but I do not want to change the color of the buttons & comboboxes.

          I already tried this:

          QString style = "background-color: #D8A09F";
          this->setStyleSheet(style);
          

          which sets the background to the color I desire but it also changes the color of any item in the widget.
          Would somebody like to help me out?
          Thanks!

          J Offline
          J Offline
          JonB
          wrote on 9 Jan 2020, 10:30 last edited by JonB 1 Sept 2020, 10:31
          #3

          @hobbyProgrammer
          I do not know whether this would make the difference, but if you widget is of some type YourWidget you could try QString style = "YourWidget { background-color: #D8A09F; }"; to see if by applying that to the specified widget stops it propagating to child widgets?

          [Looks like @J-Hilk types faster than I can... :) ]

          J 1 Reply Last reply 9 Jan 2020, 10:36
          1
          • J JonB
            9 Jan 2020, 10:30

            @hobbyProgrammer
            I do not know whether this would make the difference, but if you widget is of some type YourWidget you could try QString style = "YourWidget { background-color: #D8A09F; }"; to see if by applying that to the specified widget stops it propagating to child widgets?

            [Looks like @J-Hilk types faster than I can... :) ]

            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 9 Jan 2020, 10:36 last edited by
            #4

            @JonB said in How to change background-color of widget, but not items:

            [Looks like @J-Hilk types faster than I can... :) ]

            Sometimes, but I imagine I write with more errors as well :)


            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
            0

            1/4

            9 Jan 2020, 10:22

            • Login

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