Results 1 to 8 of 8

Thread: reacting to changes for non-QObject classes?

  1. #1
    Join Date
    Jul 2010
    Posts
    63
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default reacting to changes for non-QObject classes?

    i have a few clients reading from the same data holder. what method would you recommend using to make sure they're all informed when data changes for an object not able to emit signals?

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: reacting to changes for non-QObject classes?

    Observer pattern, you can find a lot more on that subject.

  3. #3
    Join Date
    Jul 2010
    Posts
    63
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: reacting to changes for non-QObject classes?

    would it be a good idea to use Qt's event system for that?

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

    Default Re: reacting to changes for non-QObject classes?

    In theory yes, but it requires that the (receiving) object inherits QObject and then you can use signals and slots instead.
    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.


  5. #5
    Join Date
    Jul 2010
    Posts
    63
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: reacting to changes for non-QObject classes?

    the sending element cant be a QObject, then this might be a good dir

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

    Default Re: reacting to changes for non-QObject classes?

    Still you can use a has-a relationship with a QObject instead of an is-a relation. Just like QFuture and QFutureWatcher are related.
    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.


  7. #7
    Join Date
    Jul 2010
    Posts
    63
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: reacting to changes for non-QObject classes?

    im not quite sure i know what you meant.. who has whom?
    anyhow, say i post an event - who is the receiver? will it reach the rest of the gui if i send it to the main window?

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

    Default Re: reacting to changes for non-QObject classes?

    Quote Originally Posted by jajdoo View Post
    im not quite sure i know what you meant.. who has whom?
    You can look into Qt's source code but I suspect it's something like this:

    Qt Code:
    1. class Watcher;
    2. class Object {
    3. private:
    4. QList<Watcher*> m_watchers;
    5. friend class Watcher;
    6. void notify();
    7. };
    8. class Watcher : public QObject {
    9. Q_OBJECT
    10. public:
    11. Watcher(Object *obj, QObject *parent = 0) : QObject(parent) { obj->m_watchers << this; }
    12. signals:
    13. void objectChanged(Object*);
    14. friend class Object;
    15. };
    16.  
    17. void Object::notify() {
    18. foreach(Watcher *w, m_watchers) emit w->objectChanged(this);
    19. }
    To copy to clipboard, switch view to plain text mode 

    anyhow, say i post an event - who is the receiver?
    Every object that wanted to receive notifications.

    will it reach the rest of the gui if i send it to the main window?
    No, unless each interested object installs an event filter on the main window object.
    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. The following user says thank you to wysota for this useful post:

    jajdoo (9th August 2011)

Similar Threads

  1. Replies: 2
    Last Post: 18th June 2013, 10:31
  2. reacting on moving or resizeing
    By WilliamSpiderWeb in forum Newbie
    Replies: 3
    Last Post: 3rd March 2011, 21:45
  3. Replies: 9
    Last Post: 26th October 2009, 00:13
  4. Inherit from QObject for data-classes?
    By niko in forum Qt Programming
    Replies: 7
    Last Post: 14th July 2007, 15:09
  5. Replies: 1
    Last Post: 13th July 2006, 21:10

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
  •  
Qt is a trademark of The Qt Company.