QWebSocketServer without QApplication



  • Hello, I am experimenting with websockets and have a problem with QWebSocketServer. In the example here, the server does not run on its own thread. If you use QApplication, then it is fine. What if I dont want to use QApplication? The QWebSocketServer::onConnection() slot is never called when a client connects, but the application is blocking the port which means the server should be running.

    Note: If I use QApplication it is fine. But if i do something like the following, it does not work:

    int main()
    {
    EchoServer *server = new EchoServer(port, debug);

    while(true){}

    }


  • Moderators

    That's by design. You need QApplication object in every Qt app and you need an event loop if you want to use signals and slots.

    So if you don't want to use QApplication you can't expect any class of Qt to work properly and if you don't want Qt's event loop no signals will be delivered and no slots will be called.



  • I dont want the a.exec() to block my application.
    Can i do something like

    while(true)
    {
        a.processEvents(QEventLoop::AllEvents);
    }
    

    ?



  • ok, problem solved.

    int argc = 0;
    char** argv = 0;
    QCoreApplication a(argc,argv);
    QtServer server2(8080);
    while(true)
    {
        a.processEvents(QEventLoop::AllEvents);
    }
    

    Will do what i want. Maybe the trick is, to first create the QCoreApplication and all QObjects after that?


  • Moderators

    "I dont want the a.exec() to block my application" - now you have your own blocking loop. So, why not use QApplication and a.exec()? And actually a.exec() does not block your application, it starts the event loop. You need a running event loop to be able to send/receive events/signals. It is not clear why you don't want to use QApplication.


  • Moderators

    @Amazonasmann said:

    while(true)
    {
        a.processEvents(QEventLoop::AllEvents);
    }
    

    You are making your CPU core run at 100%. That shortens your CPU's life, makes that CPU core unavailable to other programs, and wastes electricity.

    Will do what i want. Maybe the trick is, to first create the QCoreApplication and all QObjects after that?

    First, can you describe what you want? Why do you want your own infinite loop?

    Remember that Qt is an event-driven framework, so I recommend writing event-driven code.


Log in to reply
 

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