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. [SOLVED] Set dynamic property by stylesheet?
QtWS25 Last Chance

[SOLVED] Set dynamic property by stylesheet?

Scheduled Pinned Locked Moved General and Desktop
dynamic propertstylesheet
8 Posts 4 Posters 6.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.
  • Q Offline
    Q Offline
    qtacc32
    wrote on 25 Apr 2015, 16:06 last edited by qtacc32
    #1

    Hello,

    I would like to set a dynamic property (one that hasn't been declared by Q_PROPERTY but added at runtime) by stylesheet.

    svg->setProperty("color", 123);
    svg->setStyleSheet("qproperty-color: 456;");
    cout << "PROP: " << svg->property("color").toInt() << endl;
    

    Unfortunately, the output is 123, so it has not been set by the stylesheet.

    It works however if the property is declared by Q_PROPERTY first.

    Is this by design? Is there a way to set dynamically added properties by stylesheet?

    Thank you very much!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      aliks-os
      wrote on 25 Apr 2015, 16:40 last edited by aliks-os
      #2

      To proper use dynamamic property you must first of all use Q_PROPERTY. Second, you must have appropriate getter and setter.
      Third, property and styleSheet is a different thinkgs.
      cout << "PROP: " << svg->property("color").toInt() << endl; //What are you expected here??? according your code you must have here 123.

      I think you must write something like bellow

      QString color = svg->getProperty("color", 123);
      svg->setStyleSheet("qproperty-color: "+color+";");
      cout << "PROP: " << svg->property("color").toInt() << endl;

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qtacc32
        wrote on 25 Apr 2015, 17:59 last edited by
        #3

        I don't think that Q_PROPERTY is necessary to use a property.
        From http://doc.qt.io/qt-5/properties.html#dynamic-properties:

        QObject::setProperty() can also be used to add new properties to an instance of a class at runtime. [...] But if the property with the given name doesn't exist in the QObject (i.e., if it wasn't declared with Q_PROPERTY()), a new property with the given name and value is automatically added to the QObject [...]

        I am expecting an output of 456 because I have changed the previously added property "color" by stylesheet to 456 with "qproperty-color: 456;".

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Sam
          wrote on 25 Apr 2015, 19:18 last edited by
          #4

          Hi @qtacc32

          You can have a look at Dynamic Properties and Stylesheets

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qtacc32
            wrote on 25 Apr 2015, 19:31 last edited by
            #5

            Hi,
            thanks but this just isn't what I want to do.

            I want to add a new property at runtime with setProperty().
            Then I want to change that property value via stylesheet.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 25 Apr 2015, 21:49 last edited by
              #6

              Hi,

              AFAIK, you can't for dynamic properties. Setting a property from sa tyle sheet requires that property to be designable Q_PROPERTY (explained here)

              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
              1
              • Q Offline
                Q Offline
                qtacc32
                wrote on 25 Apr 2015, 22:03 last edited by
                #7

                Okay, thank you. I missed "designable", probably because I didn't know what it means.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 25 Apr 2015, 22:08 last edited by
                  #8

                  You're welcome !

                  You have more information about "designable" here

                  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
                  • P Pl45m4 referenced this topic on 14 Jul 2023, 05:44

                  1/8

                  25 Apr 2015, 16:06

                  • Login

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