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. Programmatically creating stylesheets.
Qt 6.11 is out! See what's new in the release blog

Programmatically creating stylesheets.

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 1.3k Views 2 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.
  • G Offline
    G Offline
    gantzm
    wrote on last edited by
    #1

    Is there a way to programmatically create stylesheet strings? I have some style sheets that need to be generated and some of the attributes come from user input. I'm looking for something that can create the stylesheets while sanitizing/validating the user provided parameters. I could just bash the strings together, but that could result in something that isn't a valid style sheet depending on user input.

    JonBJ 1 Reply Last reply
    0
    • G gantzm

      Is there a way to programmatically create stylesheet strings? I have some style sheets that need to be generated and some of the attributes come from user input. I'm looking for something that can create the stylesheets while sanitizing/validating the user provided parameters. I could just bash the strings together, but that could result in something that isn't a valid style sheet depending on user input.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @gantzm
      Hello and welcome.

      Not so far as I know, no.

      All you can really do is escape certain characters to prevent it being "illegal". Probably forbid most punctuation characters from user, if possible! Usually malformed CSS/QSS is simply ignored. Far from perfect, but I believe you have to roll your own santizers/verifiers.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mchinand
        wrote on last edited by
        #3

        What sort of input are you expecting from the user and how are you getting it? If you have a template stylesheet and then get user input via comboboxes, checkboxes, and/or colorpicker widgets and then fill values into your base stylesheet it wouldn't be too bad. Any free text entry will put more of a burden on validating the user input, of course.

        G 1 Reply Last reply
        2
        • M mchinand

          What sort of input are you expecting from the user and how are you getting it? If you have a template stylesheet and then get user input via comboboxes, checkboxes, and/or colorpicker widgets and then fill values into your base stylesheet it wouldn't be too bad. Any free text entry will put more of a burden on validating the user input, of course.

          G Offline
          G Offline
          gantzm
          wrote on last edited by
          #4

          @mchinand The input is coming from a json file that can be hand edited. I'm thinking that maybe for something like a color I'll create a QColor from the text and check if it's valid. Then convert that QColor back to a string to use in the stylesheet.

          Most of my work has a focus on security ( although this app does not ) and just smashing user supplied strings together is usually a bad smell. That's why I was hoping there might have been a sanitizing stylesheet builder or something similiar.

          JonBJ 1 Reply Last reply
          0
          • G gantzm

            @mchinand The input is coming from a json file that can be hand edited. I'm thinking that maybe for something like a color I'll create a QColor from the text and check if it's valid. Then convert that QColor back to a string to use in the stylesheet.

            Most of my work has a focus on security ( although this app does not ) and just smashing user supplied strings together is usually a bad smell. That's why I was hoping there might have been a sanitizing stylesheet builder or something similiar.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @gantzm

            I'll create a QColor from the text and check if it's valid

            I don't believe that is actually correct. I think the names of colors which can be used in a stylesheet are a separate set from those accepted for a QColor. But I don't have a reference for this, Doubtless will work fine for most like red, may not for the more esoteric names. (Ah, I see QColor says it uses names per https://www.w3.org/TR/SVG11/types.html#ColorKeywords. Maybe those are the same as the names used in QSS/CSS.)

            Most of my work has a focus on security ( although this app does not ) and just smashing user supplied strings together is usually a bad smell.

            Absolutely!

            JoeCFDJ 1 Reply Last reply
            0
            • JonBJ JonB

              @gantzm

              I'll create a QColor from the text and check if it's valid

              I don't believe that is actually correct. I think the names of colors which can be used in a stylesheet are a separate set from those accepted for a QColor. But I don't have a reference for this, Doubtless will work fine for most like red, may not for the more esoteric names. (Ah, I see QColor says it uses names per https://www.w3.org/TR/SVG11/types.html#ColorKeywords. Maybe those are the same as the names used in QSS/CSS.)

              Most of my work has a focus on security ( although this app does not ) and just smashing user supplied strings together is usually a bad smell.

              Absolutely!

              JoeCFDJ Offline
              JoeCFDJ Offline
              JoeCFD
              wrote on last edited by
              #6

              @JonB bool QColor::isValidColor(const QString &name) can be used to check if the string color is valid or not. Then create
              a QColor with the string and call QString QColor::name(QColor::NameFormat format) to get a valid name for stylesheet. It is doable.

              JonBJ 1 Reply Last reply
              0
              • JoeCFDJ JoeCFD

                @JonB bool QColor::isValidColor(const QString &name) can be used to check if the string color is valid or not. Then create
                a QColor with the string and call QString QColor::name(QColor::NameFormat format) to get a valid name for stylesheet. It is doable.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @JoeCFD
                ? The question is whether the names recognised by QColor::isValidColor() are the same as those usable as color names in a Qt stylesheet. They may be, or they may not be, do you have a doc reference either way?

                JoeCFDJ 1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi,

                  Since the input is a json file, did you consider using jsonschema to validate the input your get ?

                  That way if the json is invalid with regard to the schema it will not get used at all.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • JonBJ JonB

                    @JoeCFD
                    ? The question is whether the names recognised by QColor::isValidColor() are the same as those usable as color names in a Qt stylesheet. They may be, or they may not be, do you have a doc reference either way?

                    JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by JoeCFD
                    #9

                    @JonB https://doc.qt.io/qt-5/qcolor.html#setNamedColor
                    He has to make his protocol for this with all possible mappings.

                    G 1 Reply Last reply
                    0
                    • JoeCFDJ JoeCFD

                      @JonB https://doc.qt.io/qt-5/qcolor.html#setNamedColor
                      He has to make his protocol for this with all possible mappings.

                      G Offline
                      G Offline
                      gantzm
                      wrote on last edited by
                      #10

                      @JoeCFD That actually works perfectly as the colors will be specified in "#" hex values. So I can verify them with QColor with no problems.

                      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