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. How to share property in different classes?
Forum Updated to NodeBB v4.3 + New Features

How to share property in different classes?

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

    In my project there are many classes some of which have the same property, I want to reuse those property definitions in different classes, but could not find a way.
    E.g: There are three properties:name, pic, desc which are used by class A,B,C. I don't want to write the properties multiple times in different class. Is there anyway to do that?
    @class A: public QObject
    {
    Q_OBJECT
    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
    Q_PROPERTY(QString pic READ pic WRITE setPic NOTIFY picChanged)
    public:
    A(QObject *parent = 0);

    QString name();
    void setName(QString name);
    
    QString pic();
    void setPic(QString pic);
    

    signals:
    void nameChanged();
    void picChanged();

    private:
    QString m_name;
    QString m_pic;
    }

    class B: public QObject
    {
    Q_OBJECT
    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
    Q_PROPERTY(QString desc READ desc WRITE setDesc NOTIFY descChanged)
    public:
    B(QObject *parent = 0);

    QString name();
    void setName(QString name);
    
    QString desc();
    void setDesc(QString desc);
    

    signals:
    void nameChanged();
    void descChanged();

    private:
    QString m_name;
    QString m_desc;
    }

    class C: public QObject
    {
    Q_OBJECT
    Q_PROPERTY(QString pic READ pic WRITE setPic NOTIFY picChanged)
    Q_PROPERTY(QString desc READ desc WRITE setDesc NOTIFY descChanged)
    public:
    C(QObject *parent = 0);

    QString pic();
    void setPic(QString pic);
    
    QString desc();
    void setDesc(QString desc);
    

    signals:
    void picChanged();
    void descChanged();

    private:
    QString m_pic;
    QString m_desc;
    }@

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      -Sounds like classic example for inheritance.-

      I've just realised that your classes use different properties.

      You might create a template class which has storage, a setter and a getter for a property (and see if such a case is supported by the Q_PROPERTY implementation).
      Or you use a preprocessor macro (or a combination of both) which expands to the actual code.

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

        [quote author="Lukas Geyer" date="1312642365"]Sounds like classic example for inheritance.[/quote]

        No, can not use multiple inheritance, because multiple QObject inheritance is not supported, but Q_PROPERTY definition needs class to inherit from QObject.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          [quote author="Alex Liu" date="1312642738"]
          [quote author="Lukas Geyer" date="1312642365"]Sounds like classic example for inheritance.[/quote]

          No, can not use multiple inheritance, because multiple QObject inheritance is not supported, but Q_PROPERTY definition needs class to inherit from QObject.

          [/quote]

          There is no need to inherit QObject multiple times as the base class already inherits QObject (which allows for using properties).

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

            @Lukas Geyer
            Could you give some example code which B,C can share the same code for property desc, A,B for name, A,C for pic? I tried use MICRO, it would not work.

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lgeyer
              wrote on last edited by
              #6

              I just tried the template solution and passing member functions as setters and getters to Q_PROPERTY does, as suspected, not work.

              The preprocessor solution does work as expected.
              @
              #define PROPERTY(Type, Name) private:
              Type Name;
              public:
              void set##Name(Type value) { Name = value; }
              Type get##Name() const { return Name; }

              // ...

              class A : public QObject
              {
              Q_OBJECT

              Q_PROPERTY(QString name READ getname WRITE setname)
              Q_PROPERTY(QString description READ getdescription WRITE setdescription)
              
              PROPERTY(QString, name)
              PROPERTY(QString, description)
              

              };
              @
              You cannot include the Q_PROPERTY macro in the PROPERTY macro as the source files are moc'ed before preprocessed and moc does not replace in macros.

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

                Thanks Lukas, I even don't want to write Q_PROPERTY definition. But as you mentioned, because of the moc, MACRO for define Q_PROPERTY does not work, and in other hand, multiple inheritance is not work too. So I think I need to write properties for each class separately.

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

                  Hi,

                  MOC can't work with #defines, we also wanted to do such things :-(
                  BUt it just scans the source code and has no pre processor working in front.

                  So you will have to type it in each class seperately...

                  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
                    alex2202375
                    wrote on last edited by
                    #9

                    Thanks guys, looks I still need to manually write the code many times:(

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      joonhwan
                      wrote on last edited by
                      #10

                      Though it has been quite old post, I think it's interesting topic. Hmm. How about introducing another simple moc-like code generating tool for this, if there is a certain reason why moc cannot not support getter/setter generation like this.

                      joonhwan at gmail dot com

                      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