Results 1 to 13 of 13

Thread: QTcpServer(this)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2018
    Posts
    22
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default QTcpServer(this)

    Hello everyone.

    I have been assigned with the responsibility of maintaining some old Qt program.
    In the process i had to convert it from Qt 4 to Qt 5.10 and from Qwt 4.x.x to Qwt 6.1.3

    Everything seemed to work and i can compile and run the program.
    However, when the server is set up to listen on the corresponding ports, new_Connection() is never called.

    I get the following warning:
    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is gui::model::Listener(0x2ba442c0), parent's thread is QThread(0x215f97b8), current thread is QThread(0x2ba445b0)
    More specifically (see code below) the line
    Qt Code:
    1. server_ = new QTcpServer(this);
    To copy to clipboard, switch view to plain text mode 
    is causing it.
    I have a strong feeling that it's because
    Qt Code:
    1. Listener
    To copy to clipboard, switch view to plain text mode 
    is not in the same thread as
    Qt Code:
    1. server_
    To copy to clipboard, switch view to plain text mode 

    I have search the internet thin, but I cannot seem to find any post that has relevance (or they are too old)

    Qt Code:
    1. #include <QtNetwork>
    2. #include <QtDebug>
    3. #include "listener.h"
    4. #include "sockethandler.h"
    5. #include <qthread.h>
    6.  
    7. // Assertion to ensure that char and short has the expected sizes
    8. // (required for protocol to work)
    9.  
    10. namespace gui { namespace model {
    11. Listener::Listener(QObject *parent, int port) :
    12. QObject(parent), port_(port), server_(0)
    13. { }
    14.  
    15. /**
    16.   * Create server and listen for connections
    17.   */
    18. void Listener::listen() {
    19. if (server_!=0) qFatal("listen() called on a connection that was already initialized");
    20. server_ = new QTcpServer(this);
    21. server_->listen(QHostAddress::Any, port_);
    22. qDebug() << connect(server_, SIGNAL(newConnection()),
    23. this, SLOT(new_Connection()));
    24. emit log(tr("Listening on port %1").arg(port_));
    25. }
    26.  
    27. /**
    28.   * Called on new connection.
    29.   * Check for duplicate and initialize socket.
    30.   */
    31. void Listener::new_Connection()
    32. {
    33. while (server_->hasPendingConnections()) {
    34. emit log(tr("Connection opened on port %1").arg(port_));
    35. emit connected(
    36. new SocketHandler(this, server_->nextPendingConnection())
    37. );
    38. }
    39. }
    40. }} // gui::model
    To copy to clipboard, switch view to plain text mode 

    Writing
    Qt Code:
    1. server_ = new QTcpServer();
    To copy to clipboard, switch view to plain text mode 
    instead of
    Qt Code:
    1. server_ = new QTcpServer(this);
    To copy to clipboard, switch view to plain text mode 
    removes the warning, but the problem persists.

    .h file:
    Qt Code:
    1. #ifndef CONNECTION_H
    2. #define CONNECTION_H
    3.  
    4. #include <QObject>
    5.  
    6. #include "connectassert.h"
    7.  
    8. QT_BEGIN_NAMESPACE
    9. class QTcpServer;
    10. class QTcpSocket;
    11. QT_END_NAMESPACE
    12.  
    13. namespace gui { namespace model {
    14. class SocketHandler;
    15. /**
    16.   * Listens on a single port for a connection from HAsim.
    17.   */
    18. class Listener : public QObject {
    19. Q_OBJECT
    20. public:
    21. /** Creates object */
    22. explicit Listener(QObject *parent, int port);
    23. /** Listens for connections */
    24. void listen();
    25.  
    26. signals:
    27. /** Emitted when a connection is opened */
    28. void connected(SocketHandler*);
    29. /** Emitted when there is information available about connection */
    30. void log(QString str);
    31.  
    32. private slots:
    33. void new_Connection();
    34.  
    35. private:
    36. int port_;
    37.  
    38. QTcpServer *server_;
    39. };
    40.  
    41. }} // gui::model
    42.  
    43. #endif // CONNECTION_H
    To copy to clipboard, switch view to plain text mode 

    So how can I correctly fix the error?

    EDIT: I forgot to add - This worked before (on Qt4), so what is changed from Qt4 to Qt5.10 that would cause this to fail?

    I hope you can help

    best regards
    David
    Last edited by DAVC; 19th March 2018 at 13:06. Reason: updated contents

Similar Threads

  1. QTcpServer
    By shreeja in forum Newbie
    Replies: 1
    Last Post: 28th December 2015, 09:21
  2. QTcpServer
    By DmitryNik in forum Newbie
    Replies: 0
    Last Post: 25th October 2011, 12:36
  3. QTcpServer
    By DmitryNik in forum Newbie
    Replies: 2
    Last Post: 1st October 2011, 08:07
  4. QTcpServer and GDB
    By baray98 in forum Qt Programming
    Replies: 2
    Last Post: 21st January 2009, 08:02
  5. Replies: 1
    Last Post: 18th June 2006, 10:12

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.