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. Question about Qhttpserver and QtFuture

Question about Qhttpserver and QtFuture

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 643 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.
  • A Offline
    A Offline
    apaczenko1993
    wrote on last edited by
    #1

    Hello, I have writted code like this:

    server->route("/promise", [this] (const QHttpServerRequest & request){
    
             writeLog(request.remoteAddress().toString() + ":" + QString::number(request.remotePort()) + " <--- Otrzymano request '/promise'");
    
             auto * watcher = new QFutureWatcher<QHttpServerResponse>(this);
    
             connect(watcher, &QFutureWatcher<QHttpServerResponse>::finished, this, [watcher, this, &request]() {
                    writeLog(request.remoteAddress().toString() + ":" + QString::number(request.remotePort()) + " ---> Request '/promise' zakonczony, zwracam odpowiedź");
                    watcher->deleteLater();
             });
    
             QFuture<QHttpServerResponse> future = QtConcurrent::run([&] () {
                    qDebug() << "Long task";
                    QThread::msleep(1500);
                    qDebug() << "Generating JSON"
                    QJsonDocument jsonDoc2(QJsonObject{
                                               {
                                                   {"key1", "1"},
                                                   {"key2", "2"},
                                                   {"key3", "3"}
                                               }
                                           });
                    QByteArray jsonData = jsonDoc2.toJson(QJsonDocument::Compact);
    
                    QHttpServerResponse response(jsonData, QHttpServerResponse::StatusCode::Ok);
                    response.setHeader("Access-Control-Allow-Origin", "*");
                    response.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
                    response.setHeader("Access-Control-Allow-Headers", "Origin, Content-Type, Accept");
                    response.setHeader("Content-Type", "application/json");
                    return response;
                });
    
             watcher->setFuture(future);
             return future;
         });
    

    and as I understand this route will be in a separate thread, but how can I receive a log information from QtConcurrent::run functon to main thread to show log info in GUI like qDebug() << "Generating JSON"?

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

      Hi,

      I am not sure to follow you, do you mean you want to get all qDebug messages in a widget ?
      That can be done using qInstallMessageHandler.

      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
      1
      • A Offline
        A Offline
        apaczenko1993
        wrote on last edited by
        #3

        Hi, I want to show all log messages in a QPlainTextEdit and save it to log file

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

          This article shows a way to do it.

          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
          2
          • A apaczenko1993 has marked this topic as solved on

          • Login

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