Results 1 to 10 of 10

Thread: Q_PROPERTY compile problem

  1. #1
    Join Date
    Aug 2010
    Posts
    65
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Q_PROPERTY compile problem

    I'm starting to want to use the Q_PROPERTY system for a specific case. I have a class with a member which contains the information used in various modeless dialogs. However, since these dialogs are modeless, the information might be altered while they are running. So I thought of transforming this member into a Q_PROPERTY with a NOTIFY signal that I could then connect to the dialogs so they'd refresh themselves should this data be altered. However, the member is now no longer detected as a class data member and thus the class fails to compile (giving hundreds of "undeclared identifier" errors).

    The class declaration is the following:
    Qt Code:
    1. class Canvas : public QSFMLCanvas
    2. {
    3. Q_OBJECT
    4. Q_PROPERTY(std::vector<Fault> faults READ GetFaults WRITE SetFaults NOTIFY FaultsEdited)
    5. Q_PROPERTY(std::vector<Horizon> horizons READ GetHorizons NOTIFY HorizonsEdited)
    6. /*...*/
    7. //std::vector<Fault> faults;
    8. //std::vector<Horizon> horizons;
    9. /*...*/
    10. public:
    11. std::vector<Fault> GetFaults() const;
    12. void SetFaults(std::vector<Fault> v);
    13. std::vector<Horizon> GetHorizons() const;
    14. /*...*/
    15. signals:
    16. void FaultsEdited();
    17. void HorizonsEdited();
    18. };
    To copy to clipboard, switch view to plain text mode 

    As well, should I uncomment the original data members, it compiles just fine, but the NOTIFY signal is not emitted.
    Last edited by Wasabi; 7th January 2011 at 14:26.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Q_PROPERTY compile problem

    Create your obvious private members.

    In the rest of the code, use the property functions like getFaults and setFaults.

  3. #3
    Join Date
    Aug 2010
    Posts
    65
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Q_PROPERTY compile problem

    They are created. The compiling problem isn't with the functions, but with the data members, faults and horizons.

    1>.\Canvas.cpp(74) : error C2065: 'faults' : undeclared identifier
    1>.\Canvas.cpp(78) : error C2065: 'horizons' : undeclared identifier

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Q_PROPERTY compile problem

    They are created.
    Not in the header you posted.
    You need a member that can store the values you set and get.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Aug 2010
    Posts
    65
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Q_PROPERTY compile problem

    Wait, in the examples in the doc (http://doc.trolltech.com/latest/prop...simple-example), the member is given in the Q_PROPERTY line and never again.
    Qt Code:
    1. Q_PROPERTY(Priority priority READ priority WRITE setPriority)
    To copy to clipboard, switch view to plain text mode 
    It later defines the enum type Priority, but doesn't create a member priority.
    So, as I understand, the macro already creates the data member.

    As well, should I uncomment the original data members (which are commented in the given header), it compiles just fine, as stated in the OP. However, the signal is not emitted. Even if the value is altered via the WRITE function.

    I mean, I'm not supposed to manually emit() the signal from within the WRITE function, right? That's precisely what the NOTIFY keyword is supposed to do for me, right?

    EDIT: Just read this, which cleared my doubt regarding the variable declaration, but there's still the issue of the signal not being emitted.
    Last edited by Wasabi; 7th January 2011 at 16:51.

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Q_PROPERTY compile problem

    but there's still the issue of the signal not being emitted.
    This is an implementation issue, with out the code responsible for it, its hard to tell what you are doing wrong.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Aug 2010
    Posts
    65
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Q_PROPERTY compile problem

    As a pure test, I created a bogus slot in Canvas (::asd()).
    I then did the following:
    Qt Code:
    1. class Canvas : public QSFMLCanvas
    2. {
    3. Q_OBJECT
    4. Q_PROPERTY(std::vector<Fault> faults READ GetFaults WRITE SetFaults NOTIFY FaultsEdited)
    5. Q_PROPERTY(std::vector<Horizon> horizons READ GetHorizons NOTIFY HorizonsEdited)
    6. std::vector<Fault> faults; //contains all faults in project
    7. std::vector<Horizon> horizons; //contains all horizons in project
    8. public:
    9. Canvas(const QPoint& Position, const QSize& Size, QWidget* Parent=0);
    10. Canvas();
    11. std::vector<Fault> GetFaults() const;
    12. void SetFaults(std::vector<Fault> v);
    13. std::vector<Horizon> GetHorizons() const;
    14. public slots:
    15. void asd();
    16. signals:
    17. void FaultsEdited();
    18. void HorizonsEdited();
    19. };
    20.  
    21. Canvas::Canvas()
    22. {
    23. //...
    24. connect(this,SIGNAL(FaultsEdited()),this,SLOT(asd()));
    25. std::vector<Fault> v;
    26. v.push_back(Fault());
    27. SetFaults(v);
    28. //...
    29. }
    30. void Canvas::SetFaults(std::vector<Fault> v)
    31. {
    32. faults=v;
    33. }
    34. void Canvas::asd()
    35. {
    36. int u=1;
    37. }
    To copy to clipboard, switch view to plain text mode 

    I run this in Debug Mode with a breakpoint inside asd() so that, should the signal be emitted and received, the program simply stops at the breakpoint. This is a quick and easy method I use often.

    But this time, nothing happens, telling me the signal wasn't emitted. Again, I don't have to emit the NOTIFY signal myself, do I? The macro should do that for me, right?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Q_PROPERTY compile problem

    There is no magic there. Qt won't do your work for you. Q_PROPERTY only marks certain things as available for the property system but you have to implement all the getters and setters yourself. Including all signal emissions. Only you know what should happen inside a setter and if you just add "emit FaultsEdited();" in your setter then this will not be a proper solution. You can only emit the signal if the contents of the property really changed.

    Qt Code:
    1. void Canvas::SetFaults(std::vector<Fault> &v) {
    2. if(faults==v) return;
    3. faults = v;
    4. emit FaultsEdited();
    5. // better yet: emit FaultsEdited(faults);
    6. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Aug 2010
    Posts
    65
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Q_PROPERTY compile problem

    Ah, okay. I thought that Q_PROPERTY was just as magic-imbued as SIGNALS and SLOTS. I also thought that there might be uses for it outside of the Meta-Object System. But that answers all my doubts. Merci.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Q_PROPERTY compile problem

    Quote Originally Posted by Wasabi View Post
    Ah, okay. I thought that Q_PROPERTY was just as magic-imbued as SIGNALS and SLOTS.
    Not that much. There are properties that are not mapped to fields in the class so if Qt did that automatically, it would bring needless memory overhead. Just look at QWidget - there are a couple of properties related to geometry of the widget (geometry, pos, rect, size, width, height, x, y) but they all work on one single rectangle.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. The following user says thank you to wysota for this useful post:

    boudie (7th January 2011)

Similar Threads

  1. how to implement QApplication.notify()
    By di_zou in forum Newbie
    Replies: 1
    Last Post: 15th February 2010, 18:12
  2. QApplication::notify()
    By jeffpogo in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2009, 22:46
  3. Qt web notify
    By bunjee in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2008, 13:24
  4. Why and when to use Q_PROPERTY
    By Vanir in forum Qt Programming
    Replies: 4
    Last Post: 22nd November 2007, 09:25
  5. How to Use Q_PROPERTY
    By mitesh_modi in forum Qt Programming
    Replies: 7
    Last Post: 20th June 2006, 14:49

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.