connect signal to slot crazy problem

  • this is my main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QTcpSocket>
    #include <QTcpServer>
    #include "handler.h"
    handler *h = new handler();
    int main(int argc, char *argv[])
        QGuiApplication app(argc, argv);
        QQmlApplicationEngine engine;
        return app.exec();

    this is my handler.h

    #ifndef HANDLER_H
    #define HANDLER_H
    #include <QObject>
    #include <QTcpServer>
    #include <QTcpSocket>
    class handler : public QObject
        explicit handler(QObject *parent = 0);
     void mysignal();
    public slots:
        void newConnection();
        QTcpServer *mserver;
        QTcpSocket* socket;
    #endif // HANDLER_H

    this is my handler.cpp

    #include "handler.h"
    handler::handler(QObject *parent) : QObject(parent)
        mserver =  new QTcpServer();
             mserver, &QTcpServer::newConnection,
             this , &handler::newConnection
        if(!mserver->listen(QHostAddress::Any, 1234)){
                qDebug() << "connection failed";
                qDebug() << "connection success!!";
    void handler::newConnection(){
        qDebug() << "connection Accepted";
        QTcpSocket* socket = mserver->nextPendingConnection();
        socket->write("HELLO WORLD!\r\n");

    this code should should should be work but does not work why?
    newConnection slot does not fire

  • @fireghostea said in connect signal to slot crazy problem:

    handler *h = new handler();

    The position of this code line outside of any method does not make much sense to me.

  • @Asperamanca i change the position to in the main function and the problem does not solve

  • Moderators

    @fireghostea Where exactly did you put this line now? It must be after QGuiApplication app(argc, argv);

  • @jsulm yeeeeees you solve it tnx

  • HI @fireghostea

    Before initializing any Qt object, "QApplication app(argc, argv);" should be initialized. Because it initializes all the QObject , Meta object related objects.
    Without initializing QApplication if signal & slots connected, there is no use.

    The same case with your case , where " handler *h = new handler(); " is initialized before QApplication.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.