简体   繁体   中英

Question about app.exec() and looping in Qt

Isn't app.exec() an infinite loop that returns main() ?

I want to loop the server client communication below but it get executed just ones and then the main function ends with app.exec()

I am new both to Qt and C++, how can I manage this looping?

int main(int argc, char *argv[])
{
    cout << "Waiting for the next request " << endl;
    QApplication app(argc, argv);

    //  Prepare our context and socket
    zmq::context_t context(1);
    zmq::socket_t socket(context, ZMQ_REP);
    socket.bind("tcp://*:2424");
    zmq::message_t request;


    QQmlApplicationEngine engine;

    VideoStreamer videoStreamer;
    imageProvider *liveOriginalImageProvider(new imageProvider);
    imageProvider *liveMaskedImageProvider(new imageProvider);

    //********SERVER CLIENT COMMUNICATION BEGINS******//

    // Wait for next request from client
    cout << "Waiting for the next request ." << endl;
    socket.recv(&request);
    cout << "Waiting for the next request.. " << endl;
    string replyMessage = string(static_cast<char *>(request.data()), request.size());

    // Print out received message
    cout << "Received from client (Python): " + replyMessage << endl;

    //  See the gradual sending/replying from client
    sleep(1);

    //  Send reply back to client
    string msgToClient("W");
    zmq::message_t reply(msgToClient.size());
    memcpy((void *) reply.data(), (msgToClient.c_str()), msgToClient.size());
    socket.send(reply);

    //*********SERVER CLIENT COMMUNICATION ENDS**********//

    engine.rootContext()->setContextProperty("VideoStreamer",&videoStreamer);
    engine.rootContext()->setContextProperty("liveOriginalImageProvider",liveOriginalImageProvider);
    engine.rootContext()->setContextProperty("liveMaskedImageProvider",liveMaskedImageProvider);

    engine.addImageProvider("liveOriginal",liveOriginalImageProvider);
    engine.addImageProvider("liveMasked",liveMaskedImageProvider);


    const QUrl url(QStringLiteral("qrc:/main.qml"));
    engine.load(url);

    QObject::connect(&videoStreamer,&VideoStreamer::originalImage,liveOriginalImageProvider,&imageProvider::updateImage);
    QObject::connect(&videoStreamer,&VideoStreamer::maskedImage,liveMaskedImageProvider,&imageProvider::updateImage);

    return app.exec();
}

One of the main projects I work on right now uses Qt and ZMQ - Your ZMQ sockets need to live inside a QObject that runs on the application's event loop, not inside the main() function of your application

Here's a very abridged view of how one of our ZMQ sockets communicates on the Qt event loop - the app has a ZMQ_SUB socket connected to a ZMQ_PUB socket on the other end publishing events from a hardware interface

int main(int argc, char* argv[])
{
   QApplication app(argc, argv);
   ...
   ConnectionManager connMgr; // Create connection class - is a QObject subclass
   connMgr.connect(target);

   MainWindow mainWin; // Create GUI classes

   return app.exec();
}
void ConnectionManager::connect(std::string target)
{
    context = zmq_ctx_new();

    zsocket = zmq_socket(context, ZMQ_SUB);
    zmq_connect(zsocket, (connection + ":" + REQUEST_PORT).c_str());
    ...

    QTimer pollTimer;
    pollTimer.callOnTimeout(this, &ConnectionManager::onPollTimer);
    pollTimer.start(100);
}

void ConnectionManager::onPollTimer()
{
    uint16_t length = 0;

    const size_t buffer_length = 1024;
    uint8_t* buffer = new uint8_t[buffer_length];

    do
    {
        int64_t more = 0;
        size_t more_size = sizeof more;
        auto len = zmq_recv(zsocket, buffer + length, buffer_length - length, ZMQ_NOBLOCK);
        if (len == -1)
        {
            return;
        }
        else if (len > 0)
        {
            length += len;
            auto rc = zmq_getsockopt(socket, ZMQ_RCVMORE, &more, &more_size);
        }
    } while (more);

    std::cout << "Received" << length << "bytes";
    HandleMessage(buffer, length); // Process the incoming message
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM