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. QWebSocketServer without QApplication

QWebSocketServer without QApplication

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 2.3k Views 3 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.
  • A Offline
    A Offline
    Amazonasmann
    wrote on last edited by
    #1

    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){}

    }

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Amazonasmann
        wrote on last edited by
        #3

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

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

        ?

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Amazonasmann
          wrote on last edited by
          #4

          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?

          JKSHJ 1 Reply Last reply
          0
          • jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

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

            1 Reply Last reply
            0
            • A Amazonasmann

              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?

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

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

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              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