Results 1 to 13 of 13

Thread: [QT4] threads, signals, and slots, please help.

  1. #1
    Join Date
    Jan 2006
    Posts
    44
    Thanks
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default [QT4] threads, signals, and slots, please help.

    I am in learning Qt phase here, so please bear with me.

    Imagine a threaded fortune server that serves a different fortune base don a strign you send to it when you conenct. Got the idea? Good.

    Now, here is the general shape of what I have:

    main_thread
    (QtCoreApplication used, no GUI at all)
    QTcpServer based class
    FortuneDBServerObject (not a *network* server)
    connection threads (one per connection)

    Essentially the connection threads are "siblings" of the db object, and all are children of the QTcpServer object.

    What I need to do is pass a QString (call it StringA) from a connection thread to the FortuneDBServer, and get a response (StringB) back.

    I figured I could use a direct connection on the signal to send StringA to the fortunedb object (QObject) with a return value of StringB. Initially I set the signal up inside the thread but that failed wonderfully (i.e. appeared to work but did not). Now I am trying to connect the signal in the QTcpServer class and it is simply nto getting called. At the moment I've changed the dbserver slot to only print it was called and return a static string, just to verify it gets called. Which it does not. No errors, no warnings it just never calls it.

    I've tested the networking separately and it all works perfectly. I've even tested the dbserver seperately and it also works perfectly. It is solely the communication bewteen the two I am having difficulty with.

    Links, pointers, and example code welcome. Even if it means redesigning it because I have the wrong "model".

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: [QT4] threads, signals, and slots, please help.

    Can you post some code of yours here?

  3. #3
    Join Date
    Jan 2006
    Posts
    44
    Thanks
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Re: [QT4] threads, signals, and slots, please help.

    Quote Originally Posted by wysota
    Can you post some code of yours here?
    Which part(s) do you want?

    In the connection thread I have this:
    Qt Code:
    1. if ( has_target ) {
    2. cout << "Have target. " ;
    3. if ( have_terminator ) {
    4. cout << "Have terminator " <<endl;
    5. action = emit getActionForTarget(target);
    To copy to clipboard, switch view to plain text mode 

    Which as you can see is where the signal is emitted. The couts before and after the emit do indeed print out.

    The QTcpServer based class has:
    Qt Code:
    1. void Server::incomingConnection(int socketDescriptor) {
    2. DBServerA* dbserver = &db;
    3. ConnectionThread *thread = new ConnectionThread(socketDescriptor, this);
    4. connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()), Qt::DirectConnection );
    5. connect(thread, SIGNAL(getActionForTarget()), dbserver, SLOT(getActionForTarget()), Qt::DirectConnection );
    6. thread->start();
    7. }
    To copy to clipboard, switch view to plain text mode 

    and the slot in the dbserver (a QObject) is:
    Qt Code:
    1. int DBServerA::getActionForTarget( QString target ) {
    2. qDebug() << "getAction called for target" <<target <<endl;
    3. int action = 0;
    4. }
    To copy to clipboard, switch view to plain text mode 

    (Yes other debug statements are showing up. )
    The original code for the tcpserver came from the qt-interest list, (http://lists.trolltech.com/qt-intere...ad01010-0.html) and as I said works exactly as expected ... except of course that the signal to the dbobject does seem to be getting emitted (or maybe it is but nobody is listening).
    --
    The Real Bill

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    85
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: [QT4] threads, signals, and slots, please help.

    You connected the signal
    Qt Code:
    1. getActionForTarget()
    To copy to clipboard, switch view to plain text mode 
    instead of
    Qt Code:
    1. getActionForTarget(QString)
    To copy to clipboard, switch view to plain text mode 
    You have to fix the SIGNAL and the SLOT. And why do you use direct connections? You can use those only securely when the sending and receiving object are owned by the current thread. Just use the default - Qt should do the right thing

    Do you call QThread::exec in your threads to allow event processing of queued events?

  5. #5
    Join Date
    Jan 2006
    Posts
    44
    Thanks
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Talking Re: [QT4] threads, signals, and slots, please help.

    And why do you use direct connections?
    Well that's what the advice was in the link I listed.

    As far as calling exec ... no, but other signals/slots are working fine (i.e. the signals from the socket).

    .... Added QString as you said and it is now working. Thanks. Why didn't that throw some error or warning at compile time?
    --
    The Real Bill

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: [QT4] threads, signals, and slots, please help.

    Qt Code:
    1. action = emit getActionForTarget(target);
    To copy to clipboard, switch view to plain text mode 

    Since when does "emit" have a return value?

    Maybe you should just "register" an object will return the action and call it directly here instead of such weird statements as the one above?
    Last edited by wysota; 24th January 2006 at 13:04.

  7. #7
    Join Date
    Jan 2006
    Posts
    44
    Thanks
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Question on minor correction ...

    The slot is being activated as desired, and has an appropriate return value, but back in the connection thread, the value I get is NOT what I expect. I am expecting an int back, and well I get a number but it isn't the single digit int I send back.

    a..l..m..os..t..there
    --
    The Real Bill

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: on minor correction ...

    Quote Originally Posted by ucntcme
    The slot is being activated as desired, and has an appropriate return value, but back in the connection thread, the value I get is NOT what I expect. I am expecting an int back, and well I get a number but it isn't the single digit int I send back.

    a..l..m..os..t..there
    Probably because return values from slots are ignored and you get a random value here. Look at my previous post, I suggested a solution there.

  9. #9
    Join Date
    Jan 2006
    Posts
    44
    Thanks
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Re: [QT4] threads, signals, and slots, please help.

    Quote Originally Posted by wysota
    Qt Code:
    1. action = emit getActionForTarget(target);
    To copy to clipboard, switch view to plain text mode 

    Since when does "emit" have a return value?
    Since I wanted it to, naturally.

    Actually, I was mislead by statements (elsewhere) that emit was "just a decoration for the programmer", that the signal was still "just a function call". I was actually thiking about this last night while sleeping and thinking of alternate routes of getting the data back to the connection thread.

    Quote Originally Posted by wysota
    Maybe you should just "register" an object will return the action and call it directly here instead of such weird statements as the one above?
    As soon as I figure out just what you mean, I'm sure I'll try it.

    Thanks again.

    Cheers,
    Bill
    --
    The Real Bill

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: [QT4] threads, signals, and slots, please help.

    Quote Originally Posted by ucntcme
    Since I wanted it to, naturally.

    Actually, I was mislead by statements (elsewhere) that emit was "just a decoration for the programmer", that the signal was still "just a function call".
    It is (usually) a function call. But! What happens if there are two or more slots connected to a signal? Return value of which of them should then be returned?

    As soon as I figure out just what you mean, I'm sure I'll try it.
    I mean to pass a pointer or reference of the object which is to deliver some information to the other one. This way you'll be able to call the method directly on a single object, without signal/slot mechanism:

    Qt Code:
    1. struct aaa {
    2. int action(someclass &);
    3. };
    4.  
    5. class bbb {
    6. public:
    7. void registerDeliverer(aaa *a){ m_ad = a; }
    8. void do_something();
    9. private:
    10. aaa *m_ad;
    11. }
    12.  
    13. void bbb::do_something(){
    14. //...
    15. if(!m_ad)
    16. return;
    17. int act = m_ad->action(); // fetch an int from registered object
    18. //...
    19. }
    20. //...
    21. aaa action_deliverer;
    22. bbb obj;
    23. //...
    24. obj->registerDeliverer(&action_deliverer);
    25. //...
    26. obj->do_something();
    27. //...
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Jan 2006
    Posts
    44
    Thanks
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Re: [QT4] threads, signals, and slots, please help.

    Quote Originally Posted by wysota
    It is (usually) a function call. But! What happens if there are two or more slots connected to a signal? Return value of which of them should then be returned?
    Well obviously the only one I am interested in!

    Seriously though, that makes sense explained that way.
    Perhaps it would be nice if a warning/error was issued at compile time if a signal was declared as anything other than void? As a newbie that would make some sense to me, and would have alerted me to the head-banging route I was travelling upon.


    Quote Originally Posted by wysota
    I mean to pass a pointer or reference of the object which is to deliver some information to the other one. This way you'll be able to call the method directly on a single object, without signal/slot mechanism:

    Qt Code:
    1. struct aaa {
    2. int action(someclass &);
    3. };
    4.  
    5. class bbb {
    6. public:
    7. void registerDeliverer(aaa *a){ m_ad = a; }
    8. void do_something();
    9. private:
    10. aaa *m_ad;
    11. }
    12.  
    13. void bbb::do_something(){
    14. //...
    15. if(!m_ad)
    16. return;
    17. int act = m_ad->action(); // fetch an int from registered object
    18. //...
    19. }
    20. //...
    21. aaa action_deliverer;
    22. bbb obj;
    23. //...
    24. obj->registerDeliverer(&action_deliverer);
    25. //...
    26. obj->do_something();
    27. //...
    To copy to clipboard, switch view to plain text mode 

    I suspected that was what you meant, and had begun working on that. Of course, I've done it wrong and get segfaults, but I'm sure it's not a Qt related segfault, rather programmer naivete. Some of it I just *have* to bang away at until I get it. But then again, once I get it, I've got it. Know what I mean?


    But one quick question on the above ... what is "someclass" representing?
    --
    The Real Bill

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: [QT4] threads, signals, and slots, please help.

    Quote Originally Posted by ucntcme
    Perhaps it would be nice if a warning/error was issued at compile time if a signal was declared as anything other than void? As a newbie that would make some sense to me, and would have alerted me to the head-banging route I was travelling upon.
    No, because slots are regular methods, just with additional capabilities. You can invoke them as normal methods and use their return values as usual.

    But one quick question on the above ... what is "someclass" representing?
    Some data which is processed by "action" method... That's just an example, nothing specific.

  13. #13
    Join Date
    Jan 2006
    Posts
    44
    Thanks
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Re: [QT4] threads, signals, and slots, please help.

    Quote Originally Posted by wysota
    No, because slots are regular methods, just with additional capabilities. You can invoke them as normal methods and use their return values as usual.
    Well it was a nice thought anyway.

    Quote Originally Posted by wysota
    Some data which is processed by "action" method... That's just an example, nothing specific.
    Well I finally found the route out of segfault land (a nap can do wonders ) and it is all working perfectly. Thanks again!
    --
    The Real Bill

Similar Threads

  1. Signals and Slots Problem
    By GenericProdigy in forum Qt Programming
    Replies: 4
    Last Post: 2nd February 2009, 09:06
  2. Signals and Slots
    By 83.manish in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2008, 10:31
  3. Problem with SpinBox signals and slots
    By ramstormrage in forum Newbie
    Replies: 4
    Last Post: 2nd May 2008, 01:45
  4. Signals and Slots Across Threads
    By themusicalguy in forum Qt Programming
    Replies: 1
    Last Post: 26th October 2007, 11:16
  5. Connecting signals & slots across different threads
    By jyoti kumar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 12:40

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.