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. Howto set a single item in stylesheet
Forum Updated to NodeBB v4.3 + New Features

Howto set a single item in stylesheet

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 8.5k 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.
  • N Offline
    N Offline
    novaktamas
    wrote on last edited by
    #1

    I have a quite complex stylesheet

    @font: 72pt "Arial";
    color: rgb(0, 0, 209);
    background-color: qlineargradient(spread:pad, x1:0, y1:1, x2:0, y2:0, stop:0 rgba(212, 255, 166), stop:1 rgba(177, 255, 255));
    border-radius: 25px;
    border-width: 8px;
    padding: 6px;
    border-style: outset;
    border-color: rgb(0, 0, 127);
    @

    and I'd like to set the "color" property to different colors at runtime.
    The
    @int actualcolor= 1;
    setProperty("whichone", actualcolor);
    unpolish();
    polish();
    @...and in stylesheet use conditionals on the "whichone" property:

    @QLineEdit[whichone=1] {color: black;}
    QLineEdit[whichone=2] {color: red;}
    @

    approach can't be used if more than a couple different colors to set.

    I must cut and replace the "color: rgb(0, 0, 209);" part of the stylesheet with string operations:
    @
    QString ss= widget->styleSheet();
    ss.replace_somehow();
    widget->setStyleSheet(ss);
    @

    Has anyone seen a function like
    @setStyleSheetProperty("color","rgb(250, 175,143)");@?
    (Qt's internal stylesheet-string-interpreter probably will process the string like that.)

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Why don't you just override the color on the widgets where you need that by setting that as a separate style sheet on the widget that should have your special color?

      1 Reply Last reply
      0
      • N Offline
        N Offline
        novaktamas
        wrote on last edited by
        #3

        Yes, this is working now:

        @QString basestyle= "font: 72pt "Arial"; border-radius: 25px; border-width: 8px; padding: 6px; border-style: outset; ";
        if (value < 1) {
        ui->lbPozicio->setStyleSheet(basestyle);
        return;
        }
        else if (value < 5) {
        ui->lbPozicio->setStyleSheet(basestyle + "border-color: rgb(0, 0, 127); background-color: rgba(212, 255, 166); color: rgb(0, 0, 209);");
        return;
        }
        else if (value < 10) {
        ui->lbPozicio->setStyleSheet(basestyle + "border-color: rgb(85, 0, 0); color: rgb(130, 0, 0); background-color: rgba(255, 212, 166);");
        return;
        }
        ...
        @
        It is really a rare case, that I don't know the "base style" for the widget, so I have to take off from the actual string, cut the old property value, and replace with another one.
        Maybe this
        @setStyleSheetProperty(QString propertyname, QString newvalue)@
        function should go to the 4.8 wishlist only.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          Why do you re-set the base style also? Style sheets are called cascading for a reason. You should be able to set only the properties you want to change relative to the base style, and set the base style as your style for the whole dialog.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            novaktamas
            wrote on last edited by
            #5

            This is a "big display" QLabel, most of its properties are different from those of its parent.
            (Bigger font, thicker border with higher radius etc.)
            My first try was simply

            @ui->lbPozicio->setStyleSheet("border-color: rgb(85, 0, 0); color: rgb(130, 0, 0); background-color: rgba(255, 212, 166);");
            @
            but properties missing from this expression got their (inappropriate) values from QLabel's parent.

            It would be much more pleasant behaviour for me to have a
            setStyleSheetProperty() and
            clearStyleSheetProperty()

            If I have plenty of time, I am going to look after the re-evaluation of stylesheet-chain in the Qt source. I think there must be a point, where these functions can be added quite easily.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              Shell out plenty of time then, because I for one thought it was quite a complex piece of code...

              It is an inherent property of cascading style sheets that styles set on a parent item cascade down to the child items. Sometimes, that can be annoying, but it can usually be solved by being more specific in defining the selectors for the style in the first place.

              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