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

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 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.HilkJ JonBJ 2 Replies Last reply
    0
    • H hobbyProgrammer

      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!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #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.HilkJ 1 Reply Last reply
      1
      • H hobbyProgrammer

        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.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by J.Hilk
        #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

          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!

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #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.HilkJ 1 Reply Last reply
          1
          • JonBJ JonB

            @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.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on 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

            • Login

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