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. Error on QEventLoop::exec() under windows (works well under linux)
Forum Updated to NodeBB v4.3 + New Features

Error on QEventLoop::exec() under windows (works well under linux)

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 4 Posters 3.9k 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.
  • B Offline
    B Offline
    bludger
    wrote on last edited by
    #6

    Hi,

    You could also try it with the following code snippet:

    connect (m_pNetManager, &QNetworkAccessManager::finished, &loop, [&](QNetworkReply* reply) {
        loop.quit();
        qDebug() << reply->error();
    });
    
    loop.exec();
    

    So instead of connecting to your QNetworkReply object, connect to the QNetworkManager instead. I'm using the above snippet in my software and it works well (Qt 5.7.0 & Qt 5.8.0 both MSVC14 and OS x Clang 64).

    Also note that you need to disconnect the slot in my proposed solution after you got the reply. You can use an RAII construction for this as the connect () function will return a QMetaObject::Connection instance.

    Please let me know if this works for you.

    1 Reply Last reply
    0
    • S stephane78

      @VRonin Thanks for your response.
      first I don't delete reponse and I have several part of code like this so it is perhaps the problem.(you have put a deletelater so it is better)
      second I have a question about your code: I have about ten lines following reponse->readAll() and above local variables so should I put these lines in the lambda function and pass the variables as parameters to the lambda like reponse ?

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #7

      so should I put these lines in the lambda function and pass the variables as parameters to the lambda like reponse
      It depends a lot on:

      • what those variables are?
      • what is their life-cycle?
      • what do you do with them?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      S 1 Reply Last reply
      0
      • VRoninV VRonin

        so should I put these lines in the lambda function and pass the variables as parameters to the lambda like reponse
        It depends a lot on:

        • what those variables are?
        • what is their life-cycle?
        • what do you do with them?
        S Offline
        S Offline
        stephane78
        wrote on last edited by stephane78
        #8

        @VRonin I have local variables so it is not the problem but I have too two member variables and one that is an object on an sqlite database and I insert /update with it. On another project where I have used lambda functions (but a microsoft project with visual studio 2013) I had problems with such complex member variables in lambda and I must pass &this in the arguments of the lambda but perhaps lambda work better with mingw-w64 4.9.2, I will try.
        another question : don't you use QEventLoop and loop.exec() because of the lambda function ? so it will solve my problem with loop.exec , so I try now to implement your solution....I come back to let you know if it works well and if it solves my problem....

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #9

          QEventLoop::exec forces an asynchronous process as QNetworkAccessManager::post to be synchronous. Especially with a large number of posts this is forcing an unnatural behaviour

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          S 1 Reply Last reply
          2
          • VRoninV VRonin

            QEventLoop::exec forces an asynchronous process as QNetworkAccessManager::post to be synchronous. Especially with a large number of posts this is forcing an unnatural behaviour

            S Offline
            S Offline
            stephane78
            wrote on last edited by
            #10

            @VRonin thanks a lot. I have made two tests and it seems to work well.
            I use "this" as parameter in the lambda function to acces my member database variable and another variable.
            I will replace everywhere in the software the loop.exec() by a lambda function because I feel the software is faster now!!!! (and I will replace in another software where there are such loop.exec()).

            S 1 Reply Last reply
            1
            • S stephane78

              @VRonin thanks a lot. I have made two tests and it seems to work well.
              I use "this" as parameter in the lambda function to acces my member database variable and another variable.
              I will replace everywhere in the software the loop.exec() by a lambda function because I feel the software is faster now!!!! (and I will replace in another software where there are such loop.exec()).

              S Offline
              S Offline
              stephane78
              wrote on last edited by
              #11

              @VRonin, it works with mingw 4.9.2 under windows but is it ok too on mac os with clang and with g++ on linux ?

              jsulmJ 1 Reply Last reply
              0
              • S stephane78

                @VRonin, it works with mingw 4.9.2 under windows but is it ok too on mac os with clang and with g++ on linux ?

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #12

                @stephane78 Yes, it should. Did you test?

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

                S 1 Reply Last reply
                1
                • jsulmJ jsulm

                  @stephane78 Yes, it should. Did you test?

                  S Offline
                  S Offline
                  stephane78
                  wrote on last edited by
                  #13

                  @jsulm I am testing on windows (with mingw 4.9.2,Qt5.6.2) and I will replace evrywhere in the software the loop.exec() by a lambda and test again. if it works well I will replace them too in another existing software.
                  I have not yet tested on mac os and on linux. I suppose the clang and g++ version must support c++11

                  jsulmJ 1 Reply Last reply
                  0
                  • S stephane78

                    @jsulm I am testing on windows (with mingw 4.9.2,Qt5.6.2) and I will replace evrywhere in the software the loop.exec() by a lambda and test again. if it works well I will replace them too in another existing software.
                    I have not yet tested on mac os and on linux. I suppose the clang and g++ version must support c++11

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #14

                    @stephane78 said in Error on QEventLoop::exec() under windows (works well under linux):

                    clang and g++ version must support c++11

                    yes they do

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

                    S 1 Reply Last reply
                    2
                    • jsulmJ jsulm

                      @stephane78 said in Error on QEventLoop::exec() under windows (works well under linux):

                      clang and g++ version must support c++11

                      yes they do

                      S Offline
                      S Offline
                      stephane78
                      wrote on last edited by
                      #15

                      @VSRonin, @jsulm I have done the change in the two part of my function where I have an event loop and now it doesn't crash anymore (even with a lot of requests) and it works.

                      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