Results 1 to 20 of 23

Thread: Qt bug? Signal emitted once, slot called multiple times

Hybrid View

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

    Default Re: Qt bug? Signal emitted once, slot called multiple times

    a) I didn't at first realize that when you move a class instance to a new thread, you don't move its members, that has to be done separately
    Where do you get this from?

    @Witek - can you confirm this?
    ==========================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.

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

    Default Re: Qt bug? Signal emitted once, slot called multiple times

    Quote Originally Posted by high_flyer View Post
    @Witek - can you confirm this?
    Not really, it's quite the opposite. You can't move an object that has a parent in different thread. Moving an object that has no parent but it has children is fine. But they have to be children as in parent-child relationship, not as in class member relationship (in other words you have to have a "QObject" relationship between the object and its members).
    Last edited by wysota; 19th November 2010 at 11:39.
    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.


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

    MattPhillips (1st December 2010)

  4. #3
    Join Date
    Aug 2009
    Posts
    140
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    22

    Default Re: Qt bug? Signal emitted once, slot called multiple times

    Hi,

    Sorry this is so late--but ok, perhapds it's not strictly accurate to say that the members aren't moved. For example in the case of pointer members, the pointers themselves may be moved (if pointers even have a thread affinity--don't know), but the objects *pointed to*--which is generally what you care about--are not. It's analogous to a shallow copy. To illustrate, here is a version of my code with more cerr statements in it. MyObject::sock is a QLocalSocket, defined in the MyObject constructor.

    Qt Code:
    1. MainWindow::MainWindow()
    2. {
    3. ...
    4. QThread* thd = new QThread();
    5. my_object = new MyObject(this);
    6.  
    7. cerr << "GUI thread: " << thread() << endl;
    8. cerr << "Object thread: " << thd << endl;
    9. cerr << "Before move: " << endl;
    10. cerr << "my_object thread affinity: " << my_object->thread() << endl;
    11. cerr << "my_object socket thread affinity: " << my_object->sock->thread() << endl;
    12.  
    13. my_object->moveToThread(thd);
    14. thd->start();
    15.  
    16. cerr << "After move: " << endl;
    17. cerr << "my_object thread affinity: " << my_object->thread() << endl;
    18. cerr << "my_object socket thread affinity: " << my_object->sock->thread() << endl;
    19. }
    To copy to clipboard, switch view to plain text mode 

    The output is as follows:

    GUI thread: 0x650f90
    Object thread: 0x87b850

    Before move:
    my_object thread affinity: 0x650f90
    my_object socket thread affinity: 0x650f90

    After move:
    my_object thread affinity: 0x87b850
    my_object socket thread affinity: 0x650f90
    So, as this example makes clear, when moveToThread is used to change the thread affinity of an object, its pointer members retain the thread affinity they had prior to the move. wysota, I wasn't referring to the parent/child relationship with a) so I don't think we're necessarily disagreeing about anything.

    Best,
    Matt

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

    Default Re: Qt bug? Signal emitted once, slot called multiple times

    You can't prove anything without showing how you create your socket object. I still claim the whole tree of objects is moved to the new thread. If you say something about pointers then you probably don't really understand what thread affinity in Qt is.
    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.


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

    MattPhillips (1st December 2010)

  7. #5
    Join Date
    Aug 2009
    Posts
    140
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    22

    Default Re: Qt bug? Signal emitted once, slot called multiple times

    wysota, it was

    Qt Code:
    1. class MyObject : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. MyObject(...);
    7.  
    8. ...
    9.  
    10. QLocalSocket* sock;
    11. };
    12.  
    13. MyObject::MyObject(...)
    14. {
    15. sock = new QLocalSocket();
    16. }
    To copy to clipboard, switch view to plain text mode 

    But, after reading your comment I tried it with

    sock = new QLocalSocket(this);

    and indeed, like you said, sock *did* pick up the thread affinity of the parent. So it looks like the ConnectSignal workaround I came up with earlier wasn't necessary. So, thanks indeed for the insight and better solution. But it's still the case that *unless* you make a class member the child of the class instance, then member thread affinity is not changed when class affinity is, no?

    Matt

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

    Default Re: Qt bug? Signal emitted once, slot called multiple times

    Of course the member affinity is not changed. Why would it be? Besides, the socket object is not a member of your class. Only a pointer to a socket object is and that does not have thread affinity.
    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.


Similar Threads

  1. [SOLVED] Two signals emitted, only one slot called!
    By codeverse in forum Qt Programming
    Replies: 0
    Last Post: 11th August 2010, 15:46
  2. Replies: 1
    Last Post: 7th December 2009, 18:49
  3. filterAcceptRows() is being called many times for same souceRow.
    By kaushal_gaurav in forum Qt Programming
    Replies: 2
    Last Post: 19th February 2009, 03:49
  4. Replies: 0
    Last Post: 17th May 2008, 18:06
  5. Replies: 2
    Last Post: 16th August 2007, 00:20

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.