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. Can Background color be a variable in stylesheet
Forum Updated to NodeBB v4.3 + New Features

Can Background color be a variable in stylesheet

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.3k 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.
  • Q Offline
    Q Offline
    qt_emp
    wrote on last edited by
    #1

    I am changing the appearance of my QComboBoxes with the following:

    ui->comboBox_0->setStyleSheet ("QComboBox::drop-down {border-width: 0px;} QComboBox::down-arrow {image: url(noimg); border-width: 0px;} QComboBox { background:rgb(241, 231, 218); }");

    Is it possible to replace the { background:rgb(241, 231, 218);} with a variable name?

    IE: QString bgrgb = "background:rgb(241, 231, 218);"

    then ui->comboBox_0->setStyleSheet ("QComboBox::drop-down {border-width: 0px;} QComboBox::down-arrow {image: url(noimg); border-width: 0px;} QComboBox { bgrgb }");

    This does not work. the comboBox appears black in this implementation.

    Thanks to all for any help

    Gojir4G 1 Reply Last reply
    0
    • Q qt_emp

      I am changing the appearance of my QComboBoxes with the following:

      ui->comboBox_0->setStyleSheet ("QComboBox::drop-down {border-width: 0px;} QComboBox::down-arrow {image: url(noimg); border-width: 0px;} QComboBox { background:rgb(241, 231, 218); }");

      Is it possible to replace the { background:rgb(241, 231, 218);} with a variable name?

      IE: QString bgrgb = "background:rgb(241, 231, 218);"

      then ui->comboBox_0->setStyleSheet ("QComboBox::drop-down {border-width: 0px;} QComboBox::down-arrow {image: url(noimg); border-width: 0px;} QComboBox { bgrgb }");

      This does not work. the comboBox appears black in this implementation.

      Thanks to all for any help

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by
      #2

      Hi, @qt_emp

      I guess you need to do it by yourself.

      What about defining a const string and changing only the color ?

      const QString ss = "QComboBox::drop-down {border-width: 0px;} QComboBox::down-arrow {image: url(noimg); border-width: 0px;} QComboBox { background:%1 }";
      QColor c(Qt::red);  
      //Set background red
      ui->comboBox_0->setStyleSheet(ss.arg(c.name()));
      
      //...
       c = QColor(Qt::blue); 
      //Set background blue
      ui->comboBox_0->setStyleSheet(ss.arg(c.name()));
      
      

      I have used color name, instead of r, g, b, so only one value (%1) is replaced in the string .

      1 Reply Last reply
      2
      • Q Offline
        Q Offline
        qt_emp
        wrote on last edited by
        #3

        The color names available in the palette are too limited. I needed other colors beyond the palette, thus the RGB implementation.

        Gojir4G 1 Reply Last reply
        0
        • Q qt_emp

          The color names available in the palette are too limited. I needed other colors beyond the palette, thus the RGB implementation.

          Gojir4G Offline
          Gojir4G Offline
          Gojir4
          wrote on last edited by
          #4

          @qt_emp
          That's not what I mean.
          I used QColor::name() which returns string containing "#RRGGBB" in hexadecimal.
          You can put any color you want in QColor.

          //Setting color using hexadecimal
          QColor c = "#AABBCC"
          ui->comboBox_0->setStyleSheet(ss.arg(c.name()));
          //Another example without using QColor
          ui->comboBox_0->setStyleSheet(ss.arg("#AABBCC"));
          
          1 Reply Last reply
          2
          • Q Offline
            Q Offline
            qt_emp
            wrote on last edited by
            #5

            Understood. I implemented it and it works great. Thanks for all the help

            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