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. A "wholesome" QProperty
QtWS25 Last Chance

A "wholesome" QProperty

Scheduled Pinned Locked Moved General and Desktop
10 Posts 7 Posters 5.3k 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.
  • R Offline
    R Offline
    ronM71
    wrote on last edited by
    #1

    It's a common practice, define private data member, define public (or protected) data accessor (set/get methods).

    I've been looking at QProperty to help me reduce lines of code, but found out it requires me to write the set/get methods. Is there a more "wholesome" Qt Macro that would also provide the content of the simple "get/set" methods?

    I know, I can easily write one myself, just wanted to know if Qt already has it.

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

      No, there is not.
      The point is, the get/set methods are needed by moc for creation of the meta object code.
      And moc does not do pre compiling.

      You have to write the getter and setter.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

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

        I think "this discussion":http://developer.qt.nokia.com/forums/viewthread/4382/ may interest you.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          florent.revelut
          wrote on last edited by
          #4

          an easy way is define your own macro if you always have the same thing to do :
          @

          #define commonMember(type,x)
          protected:
          type m_ ## x
          public:
          type get ## x () { return m_ ## x;}
          void set ## x (type a){ m_## x = a;}
          @

          From my point of view however, it tends to ugliness : I prefer to write my trivial getters on demand, if required (less code, less bug, less test)

          _Macro not compiled, not tested, so not working, it's just to give the spirit of it ;-) _

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            THis macro has one problem: It does not work with Q_PROPERTY, and that's what he asked for.
            For Q_PROPERTY it's currently not possible.

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • Z Offline
              Z Offline
              ZapB
              wrote on last edited by
              #6

              But you could use this macro in conjunction with Q_PROPERTY couldn't you? Or does moc try to be smart and actually complain if it can't find the getter/setter? I've not tried it.

              Nokia Certified Qt Specialist
              Interested in hearing about Qt related work

              1 Reply Last reply
              0
              • G Offline
                G Offline
                giesbert
                wrote on last edited by
                #7

                We tried one combined macro, not two...
                It's wrth a try

                Nokia Certified Qt Specialist.
                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  giesbert
                  wrote on last edited by
                  #8

                  Ok,

                  I tried it out. This works (is compilable):

                  @
                  #define commonMember(type,x)
                  protected:
                  type m_ ## x;
                  public:
                  type get ## x () { return m_ ## x;}
                  void set ## x (type a){ m_ ## x = a;}

                  class MainWindow : public QMainWindow
                  {
                  Q_OBJECT
                  Q_PROPERTY(bool test READ gettest WRITE settest)

                  commonMember(bool, test)
                  

                  public:

                  @

                  Nokia Certified Qt Specialist.
                  Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    JulienMaille
                    wrote on last edited by
                    #9

                    Is it still impossible to generate Q_PROPERTY from a macro?

                    1 Reply Last reply
                    0
                    • raven-worxR Offline
                      raven-worxR Offline
                      raven-worx
                      Moderators
                      wrote on last edited by
                      #10

                      "Qt 5.1 introduces":http://qt-project.org/wiki/New-Features-in-Qt-5.1 a new keyword for Q_PROPERTY:
                      @New keyword in Q_PROPERTY: MEMBER let you bind a property to a class member without requiring to have a getter or a setter.@

                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                      If you have a question please use the forum so others can benefit from the solution in the future

                      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