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
    Aug 2009
    Posts
    140
    Thanks
    22
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

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

    Hi,

    Thanks a lot to both of you, especially tbscope for your instructions. I will definitely try to reorganize my code along those lines, though I probably can't get to it right away. It sounds more elegant than what I'm doing now. But as for what 'thd->WriteToProc(qb)' does, do I understand it? What level of understanding do you have in mind? thd is a pointer to a MyWorkerThread instance, '->' means 'access the member of', qb is a QByteArray, and WriteToProc is a member function of MyWorkerThread that sends qb to the auxiliary process using a QLocalSocket. 'emit WriteToProc(qb)' is essentially the same thing if the connection is a Qt:irectConnection (according to Qt documentation), but if it's a Qt::QueuedConnection then something like an event is generated which is processed within the receiver's event loop when it is ready. About Qt's implementation of signals/slots I know virtually nothing.

    Understand that the QThread class by itself is NOT a different thread.
    Right. I probably haven't had this clear in my mind. Though moveToThread(this) does kind of let you get away with not making that distinction--which is probably why I've seen you discourage that here and in other places wysota. Thinking about it more, it does seem like my way of doing it is conceptually mistaken or at least sub-optimal, compared to what you guys have described here. But basically on the new way, all of the methods and data members currently in my QThread subclass will go into the QObject subclass you described in 2). Googling moveToThread(this), the top post is a former QtCentre thread of mine asking about this very issue--Lol!! That's where the technique was suggested to me in the first place. NB, this guy

    http://thesmithfam.org/blog/2009/09/...reading-in-qt/

    does moveToThread(this), but this guy

    http://labs.qt.nokia.com/2010/06/17/...oing-it-wrong/

    hates it for what sounds like the same reasons.

    Anyway, none of this answers my initial question but I guess after I reorganize things that won't matter.

    Matt

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

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

    Quote Originally Posted by MattPhillips View Post
    Anyway, none of this answers my initial question but I guess after I reorganize things that won't matter.
    You provided too little information to see where the double connection occurs.
    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. #3
    Join Date
    Aug 2009
    Posts
    140
    Thanks
    22
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

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

    So.... I rewrote my code along the lines described by tbscope. The result is that now the object I moved to the QThread, MyObject, does not process events when the main gui thread has control. I've verified that MyObject is indeed owned by the QThread, and in addition they do non-event-processing related processing in parallel. My code in the gui window constructor is this:

    Qt Code:
    1. QThread* thd = new QThread();
    2. my_object = new MyObject();
    3. my_object->moveToThread(thd);
    4. thd->start();
    To copy to clipboard, switch view to plain text mode 

    All the signals and slots I do in the MyObject constructor, but I get the same thing when they are connected in this constructor. The order of the moveToThread, start() statements doesn't matter. I guess I haven't yet found the right way to do this, any ideas on what is? Thanks--

    Edit: The events in question are readyRead() signals generated over a QLocalSocket connection. The QLocalSocket, my_local_socket, is created within MyObject(). However, even after making my_local_socket a child of the QThread, and calling my_local_socket->moveToThread(my_thread), the problem persists.

    Matt
    Last edited by MattPhillips; 17th November 2010 at 03:51. Reason: More information

  4. #4
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

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

    Check this article about QThread: "You’re doing it wrong…"

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

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

    Show us how you establish your signal/slot connections.
    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. #6
    Join Date
    Aug 2009
    Posts
    140
    Thanks
    22
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

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

    Sweet mother of Jesus, I figured it out. My problems were that 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, and b) you can't use moveToThread at all on a QLocalSocket. According to the warning message I got, my QLocalSocket instance had children, and you can't move an object with children. However I never created an object with my_local_socket as a parent, nor did I see such in the corresponding moc file. As a result QLocalSocket stayed in the gui thread, and I got the results I did. Here's code that actually works (knock on wood):

    Qt Code:
    1. //include file declaring MyObject
    2.  
    3. class MainWindow : public QGLWidget //The fact that I'm using GL here is not important
    4. {
    5. Q_OBJECT
    6.  
    7. MainWindow(QGLWidget* parent)
    8. ...
    9.  
    10. signals:
    11. void ConnectSocket();
    12. ...
    13.  
    14. public slots:
    15. //... various slots
    16. ...
    17.  
    18. private:
    19. MyObject* my_object;
    20. };
    21.  
    22. MainWindow::MainWindow
    23. {
    24. QThread* thd = new QThread();
    25. my_object = new MyObject(this); //main_window is not the parent of my_object, it's a receiver
    26. my_object->moveToThread(thd);
    27. cerr << "GUI thread: " << thread() << endl;
    28. cerr << "Object thread: " << thd << endl;
    29. cerr << "my_object thread: " << my_object->thread() << endl;
    30. thd->start();
    31.  
    32. connect(this, SIGNAL(ConnectSocket()), my_object, SLOT(ConnectSocket()), Qt::QueuedConnection);
    33. //I think this maneuver is the *only* way to ensure the QLocalSocket instance is in the worker thread.
    34. emit ConnectSocket();
    35. }
    36.  
    37.  
    38. class MyObject : public QObject
    39. {
    40. Q_OBJECT
    41.  
    42. public:
    43. MyObject(MainWindow* _gui);
    44.  
    45. signals:
    46. ...
    47. //various signals sent to the MainWindow instance
    48.  
    49. public slots:
    50. void ConnectSocket();
    51. void ReadyRead();
    52. void LostConnection();
    53. void SocketError(QLocalSocket::LocalSocketError err);
    54.  
    55. private:
    56. MainWindow* gui;
    57. QLocalSocket* my_local_socket;
    58. };
    59.  
    60. MyObject::MyObject(MainWindow* _gui) : gui(_gui)
    61. {
    62. }
    63. void MyObject::ConnectSocket()
    64. {
    65. bool b(true);
    66. my_local_socket = new QLocalSocket();
    67. my_local_socket->connectToServer("my_local_server", QLocalSocket::ReadOnly);
    68. cerr << "my_local_socket thread: " <<my_local_socket->thread() << endl;
    69. b &= connect(my_local_socket, SIGNAL(readyRead()), this, SLOT(ReadyRead()));
    70. b &= connect(my_local_socket ,SIGNAL(error(QLocalSocket::LocalSocketError)),this, SLOT(SocketError(QLocalSocket::LocalSocketError)));
    71.  
    72. b &= connect(this,SIGNAL(my_object_signal()), gui, SLOT(gui_slot()), Qt::QueuedConnection);
    73. // ... and several more of these
    74.  
    75. cerr << "Connect success, MyObject::ConnectSocket: " << b << endl;
    76. }
    To copy to clipboard, switch view to plain text mode 

    The cerr statements output what they should; my_object and my_local_socket are in the worker thread, not the gui thread, and the connection is a success.

    Matt

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

    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.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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.


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

    MattPhillips (1st December 2010)

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

    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

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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.


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

    MattPhillips (1st December 2010)

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

    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

  14. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.