Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTCPServer & QThread



  • I've started a thread and I can't connect the signals of the server to it.

    thread.h
    @#include <QtNetwork/QTcpServer>
    #include <QThread>

    class Thread: public QThread
    {
    Q_OBJECT

    public:
    Thread();
    void run();

    private:
    QTcpServer m_server;

    private slots:
    void onReceiveNewConnection();
    };@

    thread.cpp
    @#include "thread.h"

    Thread::Thread()
    {
    start();
    }

    void Thread::run()
    {
    m_server = new QTcpServer();
    connect(m_server, SIGNAL(newConnection()), /this,/ SLOT(onReceiveNewConnection()));
    m_server->listen(QHostAddress::Any, port);
    while(!isFinished())
    {
    //...
    }
    }

    void Thread::onReceiveNewConnection()
    {
    qDebug() << "New connection!";
    }@

    The code never reaches the slot onReceiveNewConnection.
    Could anyone help me to solve this out?



  • [quote author="Cayan" date="1324941299"]
    @
    void Thread::run()
    {
    m_server = new QTcpServer();
    connect(m_server, SIGNAL(newConnection()), /this,/ SLOT(onReceiveNewConnection()));
    m_server->listen(QHostAddress::Any, port);
    while(!isFinished())
    {
    //...
    }
    }

    void Proxy::onReceiveNewConnection()
    {
    qDebug() << "New connection!";
    }@
    [/quote]

    You should check the return value of connect, it will indicate false. You are trying to connect to "this" indcating that there you should be a slot called onReceiveConnection in Thread, but it is a slot in Proxy. It is this way at least in your post.




Log in to reply