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. Can the moc automatically generate getter, setter, and notify methods for properties?
Forum Updated to NodeBB v4.3 + New Features

Can the moc automatically generate getter, setter, and notify methods for properties?

Scheduled Pinned Locked Moved Unsolved General and Desktop
propertiesmocqmakec++
3 Posts 3 Posters 4.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
    Guy Gizmo
    wrote on 21 Dec 2015, 19:00 last edited by Guy Gizmo
    #1

    I'm wondering if it's possible for Qt's moc / qmake to automatically generate getter, setter, and notify methods for properties? In theory it should be quite easy, as a basic getter / read method just returns the value of the property and a basic setter / write method just sets the value and emits the signal notifying that the value has changed.

    This would allow developers to save themselves a whole lot of boilerplate code that clutters up source files.

    Note that I'm aware it's possible to generate getter and setter methods using Qt Creator's refactor features, but that's not what I'm talking about. I'm hoping there's a way to add these methods in the files produced by qmake so that there's no reason for the boilerplate code to be in my source files at all.

    I found this old thread a little while ago that suggests this is possible starting with Qt5 if you just specify a member variable and a notify signal using Q_PROPERTY:
    http://www.qtcentre.org/threads/37845-Q_PROPERTY-Code-Generation
    ...but I tried it out and it doesn't work -- no getter or setter methods are generated.

    I also found the same feature request on the bug tracker:
    https://bugreports.qt.io/browse/QTBUG-16852
    ...which suggests the feature is in, but it looks like they consider using MEMBER in Q_PROPERTY sufficient, and while it does avoid the need for getters and setters in some cases, it's not the same thing. Simply assigning to the member variable will not trigger the notify signal to be emitted unless you write getters and setters, and now we're back to having boilerplate code.

    Is this feature actually in Qt? If not, is there any good or clever way to accomplish it? I have a lot of objects that have a lot of properties, and most of them it's important that they have a notify signal. It'd would simplify my code and workflow immensely if this could be done.

    K 1 Reply Last reply 21 Dec 2015, 19:15
    0
    • G Guy Gizmo
      21 Dec 2015, 19:00

      I'm wondering if it's possible for Qt's moc / qmake to automatically generate getter, setter, and notify methods for properties? In theory it should be quite easy, as a basic getter / read method just returns the value of the property and a basic setter / write method just sets the value and emits the signal notifying that the value has changed.

      This would allow developers to save themselves a whole lot of boilerplate code that clutters up source files.

      Note that I'm aware it's possible to generate getter and setter methods using Qt Creator's refactor features, but that's not what I'm talking about. I'm hoping there's a way to add these methods in the files produced by qmake so that there's no reason for the boilerplate code to be in my source files at all.

      I found this old thread a little while ago that suggests this is possible starting with Qt5 if you just specify a member variable and a notify signal using Q_PROPERTY:
      http://www.qtcentre.org/threads/37845-Q_PROPERTY-Code-Generation
      ...but I tried it out and it doesn't work -- no getter or setter methods are generated.

      I also found the same feature request on the bug tracker:
      https://bugreports.qt.io/browse/QTBUG-16852
      ...which suggests the feature is in, but it looks like they consider using MEMBER in Q_PROPERTY sufficient, and while it does avoid the need for getters and setters in some cases, it's not the same thing. Simply assigning to the member variable will not trigger the notify signal to be emitted unless you write getters and setters, and now we're back to having boilerplate code.

      Is this feature actually in Qt? If not, is there any good or clever way to accomplish it? I have a lot of objects that have a lot of properties, and most of them it's important that they have a notify signal. It'd would simplify my code and workflow immensely if this could be done.

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 21 Dec 2015, 19:15 last edited by kshegunov
      #2

      @Guy-Gizmo
      No, not to my knowledge. Q_PROPERTY only registers the property with the meta-object system and doesn't in fact generate the methods.

      while it does avoid the need for getters and setters in some cases, it's not the same thing.

      It in fact is very different, since it breaks the encapsulation principle in OOP.

      Is this feature actually in Qt? If not, is there any good or clever way to accomplish it? I have a lot of objects that have a lot of properties, and most of them it's important that they have a notify signal. It'd would simplify my code and workflow immensely if this could be done.

      You can always put a few macros to write the code for you. I don't consider it a very good practice, but it has been done and it's usable.

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 21 Dec 2015, 22:19 last edited by
        #3

        Hi,

        Actually it's a feature that's available within Q_PROPERTY declaration. See here for the MEMBER keyword and how to use it.

        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

        1/3

        21 Dec 2015, 19:00

        • Login

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