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. Q_PROPERTY[SOLVED]
Qt 6.11 is out! See what's new in the release blog

Q_PROPERTY[SOLVED]

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 6.1k 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.
  • D Offline
    D Offline
    dmitrij
    wrote on last edited by
    #1

    Hello!

    I'm wondering, to be honest, why in Qt is such thing as class properties? I understand, why it is in .Net: it's a convenient way to represent setter and getter for the private fields in your class and during runtime get and set methods will be generated for you. But in Qt you still have to implement them on your own. OK, you may say me, that by properties you can made the variable read-only. Isn't more convenient just forget about setVariable() -method and implement only const type getVariable()const{return var;} -method?

    So what I want to ask you about is, what's the point of properties in Qt?

    Thank you beforehand for answers.

    p.s. It's only my curiosity and the way to learn about things.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Q_PROPERTY is to be seen apart from getter and setter functions. It is just a macro which enables Qt specific options/operations on a class. Only with Q_PROPERTY you can work with a class via QMetaObject and other things. (in other words you expose function to Qt, which whom Qt can work)

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        There is no such thing as a property in C++, and there is no such thing as setters and getters either.

        But sometimes you are required to query and modify the properties of an object at runtime, two examples are the UI Designer and QML. The latter one takes advantage of another important property feature, change notifications.

        See "The Property System":http://qt-project.org/doc/qt-4.8/properties.html.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dmitrij
          wrote on last edited by
          #4

          Thank you, guys, for your answers.
          Yes, I know, that in C++ there are no such things as properties. And about NOTIFY -part, you still have to write something like that: emit valueChanged(type).

          As I understood, it's only for the meta-object information and making some "black magic" with objects during run-time, right?

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

            The Qt property system does two things:

            It semantically groups a set of methods to create a property with a getter, and optionaly a setter, a changed signal and a reset method. So, it groups them together for you as a programmer and the users of the API's you build.

            It enables the Qt Meta Object system to generate code to allow introspection of QObjects, to store some meta-information on these properties ("designable", "stored", "user"), and to provide a single way of accessing all these through the property and setProperty methods. All that can be very useful in some contexts.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dmitrij
              wrote on last edited by
              #6

              Thank you for your answer. It looks like, that in some cases it's very useful thing to consider. How it(using Q_PROPERTY -macro) will affect on performance(compilation and executing)?

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

                As code is generated for it, there will be a very small compilation time penalty. It should not have any effect on execution speed.

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dmitrij
                  wrote on last edited by
                  #8

                  Thank you very much.
                  I guess, now case is closed=)

                  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