Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. connect signal to slot crazy problem
Qt 6.11 is out! See what's new in the release blog

connect signal to slot crazy problem

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 1.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fireghostea
    wrote on last edited by
    #1

    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[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QLatin1String("qrc:/main.qml")));
        return app.exec();
    }
    
    

    this is my handler.h

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

    this is my handler.cpp

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

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

    1 Reply Last reply
    0
    • F fireghostea

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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #4

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      F 1 Reply Last reply
      1
      • A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #2

        @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.

        F 1 Reply Last reply
        0
        • A Asperamanca

          @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.

          F Offline
          F Offline
          fireghostea
          wrote on last edited by
          #3

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

          jsulmJ 1 Reply Last reply
          0
          • F fireghostea

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

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

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

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            F 1 Reply Last reply
            1
            • jsulmJ jsulm

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

              F Offline
              F Offline
              fireghostea
              wrote on last edited by
              #5

              @jsulm yeeeeees you solve it tnx

              1 Reply Last reply
              0
              • yuvaramY Offline
                yuvaramY Offline
                yuvaram
                wrote on last edited by
                #6

                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.

                Yuvaram Aligeti
                Embedded Qt Developer
                : )

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved