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 set the background color equal to background color of stylesheets
Forum Update on Monday, May 27th 2025

how to set the background color equal to background color of stylesheets

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 5 Posters 14.3k 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by
    #1

    class edit : public QLineEdit {
    public:
    edit(QString text);
    }

    edit::edit(QString text) {
    QColor color(QWidget::palette().color(QWidget::backgroundRole()));
    setStyleSheet("background-color: color; border: 0px ;")
    setText(text);

    }

    I am not able to set the color as background color in styleSheet .

    Color::setNamedColor: Unknown color name 'color'

    Could you please review the same

    J.HilkJ 1 Reply Last reply
    0
    • Q Qt Enthusiast

      class edit : public QLineEdit {
      public:
      edit(QString text);
      }

      edit::edit(QString text) {
      QColor color(QWidget::palette().color(QWidget::backgroundRole()));
      setStyleSheet("background-color: color; border: 0px ;")
      setText(text);

      }

      I am not able to set the color as background color in styleSheet .

      Color::setNamedColor: Unknown color name 'color'

      Could you please review the same

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @Qt-Enthusiast
      setStyleSheet is basically expecting a string, and you try to give him a QColor container, that won't work.

      Something like this should work.

      QColor color(QWidget::palette().color(QWidget::backgroundRole()));
      QString style("background-color: rgb(1%,2%,3%); border: 0px ;");
      style.arg(color.red).arg(color.green).arg(color.blue);
      setStyleSheet(style);
      

      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
      2
      • Q Offline
        Q Offline
        Qt Enthusiast
        wrote on last edited by
        #3

        This code is not working

        J.HilkJ jsulmJ 2 Replies Last reply
        0
        • Q Qt Enthusiast

          This code is not working

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by J.Hilk
          #4

          @Qt-Enthusiast I admit, I made a pretty big typo there, but I would expect you to find that.

          QColor color(QWidget::palette().color(QWidget::backgroundRole()));
          QString style("background-color: rgb(%1,%2,%3); border: 0px ;").arg(color.red).arg(color.green).arg(color.blue);
          setStyleSheet(style);
          

          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
          • Q Qt Enthusiast

            This code is not working

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Qt-Enthusiast "This code is not working" - really, you should at least try to find out what is wrong in the code you get here!

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • Q Offline
              Q Offline
              Qt Enthusiast
              wrote on last edited by
              #6

              I am getting black color as background color

              J.HilkJ 1 Reply Last reply
              0
              • Q Qt Enthusiast

                I am getting black color as background color

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @Qt-Enthusiast
                I would understand if you get an unresolved overloaded function type error.
                Because in the example I did not write rgb.red() as I should have but rgb.red, A clear sign, that I rely to heavily on Qt-Creators auto complete...

                this works, I just tested this in a small example

                QLabel *l = new QLabel();
                    l->resize(50,50);
                    l->show();
                    QColor rgb(QWidget::palette().color(QWidget::backgroundRole()));
                //    QColor rgb(150,150,150);
                    QString style = QString("background-color: rgb(%1,%2,%3); border: 0px ;").arg(rgb.red()).arg(rgb.green()).arg(rgb.blue());
                    l->setStyleSheet(style);
                    connect(this, &Widget::destroyed, l, &QLabel::deleteLater);
                

                What do you get as values, if you print your QColor in e.g. qDebug()?


                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
                • Q Offline
                  Q Offline
                  Qt Enthusiast
                  wrote on last edited by
                  #8
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • Q Offline
                    Q Offline
                    Qt Enthusiast
                    wrote on last edited by
                    #9

                    My back ground color is wrong

                    color1.red 255 color gree 255 color blue 255 but in actual my background is grey how to get background color as grey

                    1 Reply Last reply
                    0
                    • EddyE Offline
                      EddyE Offline
                      Eddy
                      wrote on last edited by
                      #10

                      the solution is in your previous post :
                      // QColor rgb(150,150,150);
                      but you commented it out

                      Do you understand the concept of RGB color values?

                      Qt Certified Specialist
                      www.edalsolutions.be

                      Q 1 Reply Last reply
                      2
                      • Q Offline
                        Q Offline
                        Qt Enthusiast
                        wrote on last edited by Qt Enthusiast
                        #11

                        @Eddy I do understand the RGB values concept . but I do not want to set a harcorred color .,I want to keep the color of QLIneEdit same as that of it background , with out harding the RGB values my concern is
                        QColor rgb(QWidget::palette().color(QWidget::backgroundRole())); is not giving me proper background color of the frame ? how to get that

                        1 Reply Last reply
                        0
                        • EddyE Eddy

                          the solution is in your previous post :
                          // QColor rgb(150,150,150);
                          but you commented it out

                          Do you understand the concept of RGB color values?

                          Q Offline
                          Q Offline
                          Qt Enthusiast
                          wrote on last edited by
                          #12
                          This post is deleted!
                          1 Reply Last reply
                          0
                          • EddyE Offline
                            EddyE Offline
                            Eddy
                            wrote on last edited by
                            #13

                            @Qt-Enthusiast
                            We already gave you the solution to that question in another post

                            Did you try it? What is not ok with that solution?

                            Qt Certified Specialist
                            www.edalsolutions.be

                            1 Reply Last reply
                            1
                            • Q Offline
                              Q Offline
                              Qt Enthusiast
                              wrote on last edited by
                              #14

                              no the color mismacthes from background and it is not perfect macth

                              1 Reply Last reply
                              0
                              • EddyE Offline
                                EddyE Offline
                                Eddy
                                wrote on last edited by Eddy
                                #15

                                Could you show us an image of the actual background with the lineedit in it?
                                What OS are you on?

                                Qt Certified Specialist
                                www.edalsolutions.be

                                1 Reply Last reply
                                0
                                • Ioseph12I Offline
                                  Ioseph12I Offline
                                  Ioseph12
                                  wrote on last edited by Ioseph12
                                  #16

                                  You should do somethig like this

                                  std:string clr = "background-color: rgb(" + std::to_string(color.red()) + "," + std::to_string(color.green()) + "," + std::to_string(color.blue()) + "); border: 0px;";
                                  setStyleSheet(clr.c_str());

                                  Another way:

                                  setStyleSeet("background-color: transparent; border 0px;");

                                  1 Reply Last reply
                                  0
                                  • Q Offline
                                    Q Offline
                                    Qt Enthusiast
                                    wrote on last edited by
                                    #17

                                    I will try

                                    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