Results 1 to 4 of 4

Thread: QTcpServer connected signal does not invoke corresponding slot in GUI application!!!!

  1. #1
    Join Date
    Sep 2013
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X

    Default QTcpServer connected signal does not invoke corresponding slot in GUI application!!!!

    I have tried so many things but still problem persists.

    I can see that server is started and can connect using telnet. But it does not invoke new connection slot. What seems to be wrong?

    main.cpp
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. core = new QGCCore(firstStart, argc, argv);
    4. val = core->exec();
    5. }
    To copy to clipboard, switch view to plain text mode 

    QGCCore.cpp:

    Qt Code:
    1. QGCCore::QGCCore(bool firstStart, int &argc, char* argv[]) : QApplication(argc, argv),
    2. restartRequested(false),
    3. welcome(NULL)
    4. {
    5. Server s;
    6. s.listen();
    7. }
    To copy to clipboard, switch view to plain text mode 

    server.cpp
    Qt Code:
    1. #include "server.h"
    2. #include <QTcpServer>
    3. #include <QTcpSocket>
    4. #include <cstdio>
    5. #include <QDebug>
    6.  
    7. Server::Server(QObject *parent) :
    8. QObject(parent)
    9. {
    10. server = new QTcpServer(this);
    11. connect(server, SIGNAL(newConnection()),
    12. this, SLOT(on_newConnection()));
    13. qDebug() << "Server instance created";
    14. }
    15.  
    16. void Server::listen()
    17. {
    18. server->listen(QHostAddress::Any, 1234);
    19. qDebug() << "Server listening to 1234";
    20. }
    21.  
    22. void Server::on_newConnection()
    23. {
    24. qDebug() << "New connection made!";
    25. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpServer connected signal does not invoke corresponding slot in GUI application

    By the time event processing starts there is no longer any slot to call, because the receiver object is already gone.

    The Server object is created, its listen is called and then it is destroyed.

    If you want your Server instance to be a signal receiver, don't destroy it, keep it

    Cheers,
    _

  3. #3
    Join Date
    Sep 2013
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X

    Thumbs up Re: QTcpServer connected signal does not invoke corresponding slot in GUI application

    Thanks anda_skoa so much..

    I created a static singleton instance of server which stayed around..and signal/slot for new connection works like charm..

    Newbie to c++, so still struggling with scopes...


  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpServer connected signal does not invoke corresponding slot in GUI application

    It might have been easier to just add a Server member to QGCCore class
    Or directly use the QTcpServer inside QGCore and connecting to its slots.

    Cheers,
    _

Similar Threads

  1. Replies: 11
    Last Post: 2nd February 2013, 14:39
  2. Replies: 2
    Last Post: 26th August 2011, 08:51
  3. Replies: 4
    Last Post: 30th November 2010, 21:09
  4. Signal connected to slot (or signal)
    By Althor in forum Newbie
    Replies: 2
    Last Post: 6th July 2010, 10:00
  5. QTcpServer and QThread signal/slot problem
    By Diph in forum Qt Programming
    Replies: 4
    Last Post: 28th July 2009, 19:34

Tags for this Thread

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.