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. Qt Asynchronous Method To Synchronous Method

Qt Asynchronous Method To Synchronous Method

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 882 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.
  • A Offline
    A Offline
    Abhi_Varma
    wrote on 28 Apr 2021, 06:18 last edited by Abhi_Varma
    #1

    Hi, I am new to Qt and I am having trouble converting this following Asynchronous function to Synchronous function.

    There is an application which has importing a file functionality which then posts the file path to the server it will check if the file is in the supported format or not and it returns the path of the file as the response.

    Ex:
    file1.cpp

    void ImportScanFile( QString filePath)
    {
     classB *objB = new classB();
     classB->checkFileIsSupported(filePath);
    }
    

    file2.cpp

    void checkFileIsSupported(QString filePath)
    {
    QUrl url(QUrl::fromLocalFile(serveraddress + '/' + filepath);
    url.setScheme("http");
    url.setHost("127.0.01");
    url.setPort("8006");
    
    QNetworkRequest request(url);
    QNetworkAccessManagerObj->post(request, QByterArray());
    }
    
    //Following slot is invoked when post request are made
    void onRequestFinished(QNetworkReply* reply)
    {
     QNetworkRequest request = reply->request();
     QString requestPath = request.url().toString();
    
     if(reply->error() !=  QNetworkReply::NoError)
     {
       if( requestPath.contains(filePath))
        {
          show qMessage with txt "Failed to import".
        }
      }
     else if( reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 200)
     {
       QString filePath = reply->readAll().data()
       if( filePath.empty())
       {
            show qMessage with txt "Failed to import".
       }
       else
       {
       if( importScan(filePath)) //This function will print scan data on the model
       {
           show qMessage with txt "Successfully Imported".
       }
       else
      {
         show qMessage with txt "Failed to import".
       }
      }
     }
    }
    

    Above as you can see that after the checkFileIsSupported() post call the control will go back to ImportScanFile() and it exit the function and then onRequestFinished() slot is called and the msg box will appear after the execution of ImportScanFile() this is Asynchronous as the msg are coming after the original function checkFilesInSupported() finished execution . How can I send back the response from the server to the place from where the checkFileIsSupported() is getting called so i can handle everything there itself and make everything Synchronous.

    J 1 Reply Last reply 29 Apr 2021, 06:10
    0
    • A Abhi_Varma
      28 Apr 2021, 06:18

      Hi, I am new to Qt and I am having trouble converting this following Asynchronous function to Synchronous function.

      There is an application which has importing a file functionality which then posts the file path to the server it will check if the file is in the supported format or not and it returns the path of the file as the response.

      Ex:
      file1.cpp

      void ImportScanFile( QString filePath)
      {
       classB *objB = new classB();
       classB->checkFileIsSupported(filePath);
      }
      

      file2.cpp

      void checkFileIsSupported(QString filePath)
      {
      QUrl url(QUrl::fromLocalFile(serveraddress + '/' + filepath);
      url.setScheme("http");
      url.setHost("127.0.01");
      url.setPort("8006");
      
      QNetworkRequest request(url);
      QNetworkAccessManagerObj->post(request, QByterArray());
      }
      
      //Following slot is invoked when post request are made
      void onRequestFinished(QNetworkReply* reply)
      {
       QNetworkRequest request = reply->request();
       QString requestPath = request.url().toString();
      
       if(reply->error() !=  QNetworkReply::NoError)
       {
         if( requestPath.contains(filePath))
          {
            show qMessage with txt "Failed to import".
          }
        }
       else if( reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 200)
       {
         QString filePath = reply->readAll().data()
         if( filePath.empty())
         {
              show qMessage with txt "Failed to import".
         }
         else
         {
         if( importScan(filePath)) //This function will print scan data on the model
         {
             show qMessage with txt "Successfully Imported".
         }
         else
        {
           show qMessage with txt "Failed to import".
         }
        }
       }
      }
      

      Above as you can see that after the checkFileIsSupported() post call the control will go back to ImportScanFile() and it exit the function and then onRequestFinished() slot is called and the msg box will appear after the execution of ImportScanFile() this is Asynchronous as the msg are coming after the original function checkFilesInSupported() finished execution . How can I send back the response from the server to the place from where the checkFileIsSupported() is getting called so i can handle everything there itself and make everything Synchronous.

      J Offline
      J Offline
      JKSH
      Moderators
      wrote on 29 Apr 2021, 06:10 last edited by JKSH
      #2

      @Abhi_Varma said in Qt Asynchronous Method To Synchronous Method:

      How can I send back the response from the server to the place from where the checkFileIsSupported() is getting called so i can handle everything there itself and make everything Synchronous.

      First, it is important to know that if you make your code synchronous, your GUI will freeze until the import is finished. Are you sure you want this?

      Second, there is no need to write synchronous code. If you want to put all the slot's code inside checkFileIsSupported(), you can connect the QNetworkReply::finished() signal to a lambda function (don't connect QNetworkAccessManager::finished()):

      void checkFileIsSupported(QString filePath)
      {
          QUrl url(QUrl::fromLocalFile(serveraddress + '/' + filepath);
          url.setScheme("http");
          url.setHost("127.0.01");
          url.setPort("8006");
      
          QNetworkRequest request(url);
          QNetworkReply* reply = QNetworkAccessManagerObj->post(request, QByteArray());
      
          connect(reply, &QNetworkReply::finished, [=]
          {
              // QNetworkRequest request = reply->request(); // NOTE: This line is unnecessary; you can capture the QNetworkRequest in the lambda directly
              QString requestPath = request.url().toString();
      
              if(reply->error() !=  QNetworkReply::NoError)
              {
                  ...
              }
              else if( reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 200)
              {
                  ...
              }
      
              // IMPORTANT: Remember to delete your QNetworkReply, or else you have a memory leak
              reply->deleteLater();
          });
      }
      

      There are many tutorials online that show you how to use lambdas.

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

      1 Reply Last reply
      2

      1/2

      28 Apr 2021, 06:18

      • Login

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