Results 1 to 8 of 8

Thread: One signal more slots

  1. #1
    Join Date
    Oct 2009
    Posts
    70

    Default One signal more slots

    Hi,
    I've a manager who handles more objects, each of them lives in a different thread.
    During the creation of this objects, I connect a signal of a manager to a slot of the objects.

    My goal is to emit a signal from manager and execute only one object slot.

    I think I can enumerate all the objects and then invoke a signal with a parameters than identify the number of the object.
    So every object receive the signal but when compare the signal parameter, only one object execute the slot.

    I think this is a simple solution, but I don't think it's the best, in particular If I've a large number of objects.

    Do you have any solutions?

    Thanks

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: One signal more slots

    You can just keep a pointer of all objects in the manager and use a QHash using the id parameters to make a key

    Qt Code:
    1. struct Object
    2. {
    3. void bar(){/* do nothing */}
    4. };
    5.  
    6. class Manager
    7. {
    8. QHash<Key, Object*> map;
    9.  
    10. void foo()
    11. {
    12. // do something with 'key's object:
    13. Key key;
    14. map[key]->bar(); // no need to run method for each key/object
    15. }
    16. };
    To copy to clipboard, switch view to plain text mode 
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Oct 2009
    Posts
    70

    Default Re: One signal more slots

    I can't do this cause the object lives in different threads.

    I've something like this:
    Qt Code:
    1. class Manager : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. signals:
    6. void invokeObjMethod(int);
    7.  
    8. private:
    9. void initManager(){
    10.  
    11. for ( int idx = 0 ; idx < n; idx++){
    12. QThread * th = new QThread(this);
    13. MyObject * obj = new MyObject(idx);
    14. connect(this,SIGNAL(invokeObjMethod(int)),obj,SLOT(doSomething(int)));
    15. obj->moveToThread(th);
    16. th->start();
    17. }
    18. }
    19. }
    20.  
    21. class MyObject : public QObject
    22. {
    23. Q_OBJECT
    24. public:
    25. MyObject(int idx, QObject * parent = 0)
    26. :QObject(parent),
    27. m_index(idx){}
    28.  
    29. private:
    30. int m_index;
    31.  
    32. public slots:
    33. void doSomething(int idx){
    34. if ( idx != m_index)
    35. return;
    36.  
    37. ...do Something ...
    38. }
    39. }
    To copy to clipboard, switch view to plain text mode 

    When the manager emit the signal invokeObjMethod(int) all the objects invoke their slot but only one do something.
    I want to know if this is the right way or there are other best solu
    Last edited by paolom; 31st July 2012 at 09:40.

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: One signal more slots

    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Oct 2009
    Posts
    70

    Default Re: One signal more slots

    The QSignalMapper class bundles signals from identifiable senders.
    In my case I've only one sender.
    I think that is not possible to use QSignalMapper in my case.

    Am I wrong?

  6. #6
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: One signal more slots

    You are right. QSignalMapper cannot help you here.

    There is a simple solution to your problem: QMetaObject::invokeMethod(). No more indices, just do something like
    Qt Code:
    1. QMetaObject::invokeMethod(pointerToTheMyObjectInstance, "doSomething", Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Oct 2009
    Posts
    70

    Default Re: One signal more slots

    With this method I can directly call the thread slot of the single object, with QueuedConnection (safety for thread).

    I think it's the best solution....many thanks!!!

  8. #8
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: One signal more slots

    Quote Originally Posted by paolom View Post
    I can't do this cause the object lives in different threads.
    Yes you can, just use invokeMethod and use it with a queued connection slot.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Signal/Slots, Where can i use it?
    By Tio in forum Newbie
    Replies: 2
    Last Post: 25th May 2010, 01:36
  2. Signal and Slots
    By waynew in forum Newbie
    Replies: 3
    Last Post: 20th November 2009, 03:50
  3. add signal/slots at runtime?
    By oc2k1 in forum Qt Programming
    Replies: 10
    Last Post: 15th June 2009, 19:52
  4. signal and slots
    By vermarajeev in forum Qt Programming
    Replies: 4
    Last Post: 16th October 2007, 08:31
  5. Signal and slots
    By villy in forum Qt Programming
    Replies: 1
    Last Post: 12th January 2007, 10: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.