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. Property or manual variabile with setter+getter?
Forum Updated to NodeBB v4.3 + New Features

Property or manual variabile with setter+getter?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 2.2k 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.
  • F Offline
    F Offline
    fluca1978
    wrote on last edited by
    #1

    This could sound trivial, but when should I use a property in the case I need always a setter and a getter? I mean, if I'm not going to use metaobject protocol, which is the best approach?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      Moved to General (Qt question), this is not for C++ gurus.

      A Q_PROPERTY always needs the getter, and probably the setter method(s). If you have them, you can make your attribute a property. If you don't need it in a metaobject, you don't need the property. On the other hand, it doesn't harm :-) Oh, and Q_PROPERTY needs your class to be based on QObject, of course.

      http://www.catb.org/~esr/faqs/smart-questions.html

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

        A Q_PROPERTY can be seen as doing two things (IMHO):

        It adds a semantic meaning to a set of methods (a getter, and possibly a setter, a signal and/or a reset method). This tells the user of your API that these all belong together. It acts as a kind of documentation, which is a Good Thing(TM).

        It adds introspection capabilities for your property. The flags you can add to the property help at that task. This also makes it possible do things like

        @
        myObject->setProperty("foo", someValue);
        @

        I find the first to be the most important, though the second can bring real technical benefits as well, especially when you want to expose your classes to scripting, for D-BUS or to QML.

        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