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. How to make signal/slot in a nude main func
QtWS25 Last Chance

How to make signal/slot in a nude main func

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 3.3k Views
  • 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.
  • S Offline
    S Offline
    suclida
    wrote on last edited by suclida
    #1

    Thank you very much! I have sloved the problem. I can create a new thread by c++11 thread but QThread. In the entrance of the new thread, QCoreApplication must be called, and a.exec() could be at the end of the entrance function.

    NUDE MAIN FUNCTION, which means there is no QApplication a, no return a.exec() in the main function.
    However, I derive a class named Client from QThread, in the Client::run(), I write this:

    void Client::run() {
        QEventLoop el;
        //something for business
        el.exec();
    }
    

    I want use the signal and slot in the new thread Client, but the console warned:
    QEventLoop: Cannot be used without QApplication
    What I can do for using singal/slot in a nude main func?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Well, you need a QApplication or at least a QCoreApplication. You don't need to call exec from it but you still need at least one of them to have the internals of Qt setup properly.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      3
      • SGaistS SGaist

        Hi,

        Well, you need a QApplication or at least a QCoreApplication. You don't need to call exec from it but you still need at least one of them to have the internals of Qt setup properly.

        S Offline
        S Offline
        suclida
        wrote on last edited by VRonin
        #3

        @SGaist Thank you very much! But there is another question:I can't slot a function with emitting QTcpSocket::connected
        here is my code:

        class Client : public QThread {
        \\ ......
        }
        int main() {
        	qDebug() << QThread::currentThreadId() << QThread::currentThread() << __FUNCTION__ << endl;
        	int i = 1;
        	QCoreApplication a(i, nullptr);
        	Client *client = new Client;
        	client->start();
        	while (true) {
        		QThread::sleep(3600);
        	}
        }
        
        void Client::slot_connected() {
        	qDebug() << QThread::currentThreadId() << QThread::currentThread() << __FUNCTION__ << endl;
        }
        
        void Client::run() {
        	qDebug() << QThread::currentThreadId() << QThread::currentThread() << __FUNCTION__ << endl;
        	QTcpSocket *tcpsocket = new QTcpSocket;
        	connect(tcpsocket, static_cast<void(QAbstractSocket::*)(QAbstractSocket::SocketError)>(&QAbstractSocket::error), this, &Client::displayError, Qt::DirectConnection);
        	connect(tcpsocket, &QTcpSocket::connected, this, &Client::slot_connected);
        	tcpsocket->connectToHost("127.0.0.1", 7501);
        }
        

        It is cofirmed that the program server7501 was listenning and get the newConnection. But the function Client::slot_connected didn't happen

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Can you explain why you are using an infinite loop that makes your application sleep rather that Qt's event loop ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          S 1 Reply Last reply
          3
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            a call to QEventLoop::exec() (or QCoreApplication::exec()) is necessary to make slots across threads working

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            S 1 Reply Last reply
            2
            • SGaistS SGaist

              Can you explain why you are using an infinite loop that makes your application sleep rather that Qt's event loop ?

              S Offline
              S Offline
              suclida
              wrote on last edited by
              #6

              @SGaist There was a mfc application, as we known, which main function is not like QCoreApplication a, return a.exec(). I want to create a shared library for that mfc application. Furthermore, the shared library need tcpsocket and singal/slot.
              Actually, the code above is a daemon for the shared library.

              1 Reply Last reply
              0
              • VRoninV VRonin

                a call to QEventLoop::exec() (or QCoreApplication::exec()) is necessary to make slots across threads working

                S Offline
                S Offline
                suclida
                wrote on last edited by VRonin
                #7

                @VRonin Thank you very much! This is the code modified.

                void Client::run() {
                	qDebug() << QThread::currentThreadId() << QThread::currentThread() << __FUNCTION__ << endl;
                	QEventLoop el;
                	QTcpSocket *tcpsocket = new QTcpSocket;
                	connect(tcpsocket, static_cast<void(QAbstractSocket::*)(QAbstractSocket::SocketError)>(&QAbstractSocket::error), this, &Client::displayError, Qt::DirectConnection);
                	connect(tcpsocket, &QTcpSocket::connected, this, &Client::slot_connected);
                	tcpsocket->connectToHost("127.0.0.1", 7501);
                	el.exec();
                }
                

                But the slot function Client::slot_connected didn't happen either.
                Could you write some code for me?

                jsulmJ 1 Reply Last reply
                0
                • S suclida

                  @VRonin Thank you very much! This is the code modified.

                  void Client::run() {
                  	qDebug() << QThread::currentThreadId() << QThread::currentThread() << __FUNCTION__ << endl;
                  	QEventLoop el;
                  	QTcpSocket *tcpsocket = new QTcpSocket;
                  	connect(tcpsocket, static_cast<void(QAbstractSocket::*)(QAbstractSocket::SocketError)>(&QAbstractSocket::error), this, &Client::displayError, Qt::DirectConnection);
                  	connect(tcpsocket, &QTcpSocket::connected, this, &Client::slot_connected);
                  	tcpsocket->connectToHost("127.0.0.1", 7501);
                  	el.exec();
                  }
                  

                  But the slot function Client::slot_connected didn't happen either.
                  Could you write some code for me?

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

                  @suclida said in How to make signal/slot in a nude main func:

                  Could you write some code for me

                  That's not how it should be.

                  You should check the return value of connect() to be sure it was successful. Then check whether there was an error: you already connected error() signal.

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

                  S 1 Reply Last reply
                  1
                  • jsulmJ jsulm

                    @suclida said in How to make signal/slot in a nude main func:

                    Could you write some code for me

                    That's not how it should be.

                    You should check the return value of connect() to be sure it was successful. Then check whether there was an error: you already connected error() signal.

                    S Offline
                    S Offline
                    suclida
                    wrote on last edited by VRonin
                    #9

                    @jsulm Thank you for your suggestion.

                    QMetaObject::Connection connectRes = connect(tcpsocket, static_cast<void(QAbstractSocket::*)(QAbstractSocket::SocketError)>(&QAbstractSocket::error), this, &Client::displayError, Qt::DirectConnection);
                    	qDebug() << connectRes << endl;
                    	connectRes = connect(tcpsocket, &QTcpSocket::connected, this, &Client::slot_connected);
                    	qDebug() << connectRes << endl;
                    

                    The QMetaObject::Connection are true, both.

                    1 Reply Last reply
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #10

                      The problem is not in Client but in your main. while (true) will prevent all signals emitted from the main thread to reach other threads.

                      Both MFC and Qt require an event loop to work correctly. if you want a signal/slot system independent of the underlying event loop I suggest you take a look at Boost.Signals2

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      S 1 Reply Last reply
                      1
                      • VRoninV VRonin

                        The problem is not in Client but in your main. while (true) will prevent all signals emitted from the main thread to reach other threads.

                        Both MFC and Qt require an event loop to work correctly. if you want a signal/slot system independent of the underlying event loop I suggest you take a look at Boost.Signals2

                        S Offline
                        S Offline
                        suclida
                        wrote on last edited by suclida
                        #11

                        @VRonin Thank you!
                        You mean that the problem is in the main. But the main function couldn't call the event loop, because the main fuction has its loop and we can't cost too much time in the main thread. So I have to call the event loop in a new thread.
                        Further more, as you know , the tcpsokcet is born in the new thread(in the function Client::run()). Did you mean that the singal QTcpSocket::connected is emitted at the main thread although tcpsocket is born in a new thread?
                        And I have create the eventloop in the Client::run(), Why was not the Client::slot_connected called?

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          Why the QEventLoop ? QThread already has everything needed. Just call exec().

                          How do you know that it's not called ?
                          Do you have any explicit error ?
                          Are you sure the socket gets connected ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0

                          • Login

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