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. C++ loop with GUI
QtWS25 Last Chance

C++ loop with GUI

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 5 Posters 4.2k 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
    alecs26
    wrote on last edited by
    #1

    Hello,

    I have a Qt 5 application with a GUI (qml). In addition to the GUI, I have computations in C++ that must run in continuous in background. However, I can't get both at the same time: if I launch my C++ loop, the GUI freezes. Here is what I done:

    **main.cpp**
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        QQmlComponent componentmain(&engine,QUrl(QStringLiteral("qrc:/main.qml")));
        otherfunctions.objectmain=componentmain.create();
    
        while(1)
        {
            // Computations here
             Sleep(30);
        }
    
        return app.exec();
    }
    
    

    If I remove the "while(1)", my GUI works fine. However, with the loop, the GUI freezes.

    I thought to launch my while loop in a separate thread but it need to be in the main thread to show image from opencv.

    What could I do ?

    Thank you very much,

    Alex

    kshegunovK 1 Reply Last reply
    0
    • M Offline
      M Offline
      mostefa
      wrote on last edited by mostefa
      #2

      Hi @alecs26

      what is the utility of this loop:

      while(1)
          {
              // Computations here
               Sleep(30);
          }
      

      Maybe you can replace this with a slot using QTimer ? could you tell us more about this loop ?

      1 Reply Last reply
      1
      • A alecs26

        Hello,

        I have a Qt 5 application with a GUI (qml). In addition to the GUI, I have computations in C++ that must run in continuous in background. However, I can't get both at the same time: if I launch my C++ loop, the GUI freezes. Here is what I done:

        **main.cpp**
        int main(int argc, char *argv[])
        {
            QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
            QApplication app(argc, argv);
        
            QQmlApplicationEngine engine;
            QQmlComponent componentmain(&engine,QUrl(QStringLiteral("qrc:/main.qml")));
            otherfunctions.objectmain=componentmain.create();
        
            while(1)
            {
                // Computations here
                 Sleep(30);
            }
        
            return app.exec();
        }
        
        

        If I remove the "while(1)", my GUI works fine. However, with the loop, the GUI freezes.

        I thought to launch my while loop in a separate thread but it need to be in the main thread to show image from opencv.

        What could I do ?

        Thank you very much,

        Alex

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        Probably the best solution is to what @mostefa suggested - using a 0 second timer that will generate events for you and execute the body of the loop in a slot. Aside from that you could also use QCoreApplication::processEvents to do the event processing, although this isn't the best approach usually.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        3
        • A Offline
          A Offline
          alecs26
          wrote on last edited by
          #4

          @mostefa The utility of the loop is to run computation in background of the GUI, it must run in continuous. It analyzes the cursor position and clicks to see the user behavior. The code would replace the comment //Computations here.

          I know how to use a QTimer in a QML GUI but I am not sure about slots (I don't know anything about this). From where would I launch the slot ? How does it work ?

          Thank you so much,

          Alex

          VRoninV 1 Reply Last reply
          0
          • A alecs26

            @mostefa The utility of the loop is to run computation in background of the GUI, it must run in continuous. It analyzes the cursor position and clicks to see the user behavior. The code would replace the comment //Computations here.

            I know how to use a QTimer in a QML GUI but I am not sure about slots (I don't know anything about this). From where would I launch the slot ? How does it work ?

            Thank you so much,

            Alex

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

            @alecs26 said in C++ loop with GUI:

            I am not sure about slots (I don't know anything about this)

            http://doc.qt.io/qt-5/signalsandslots.html

            It analyzes the cursor position and clicks to see the user behavior.

            it sounds like either poor design or on the verge of malicious software. I still hope it's the first

            "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

            1 Reply Last reply
            3
            • A Offline
              A Offline
              alecs26
              wrote on last edited by
              #6

              @VRonin it's not malicious, its to analyze the mouse motion of people living with disabilities to help them better controlling their computer.

              For you "poor design" comment, what do you suggest instead ?

              Thanks,

              Alex

              6thC6 1 Reply Last reply
              0
              • A alecs26

                @VRonin it's not malicious, its to analyze the mouse motion of people living with disabilities to help them better controlling their computer.

                For you "poor design" comment, what do you suggest instead ?

                Thanks,

                Alex

                6thC6 Offline
                6thC6 Offline
                6thC
                wrote on last edited by
                #7

                @alecs26 For you "poor design" comment, what do you suggest instead ?

                What they said: Read up on http://doc.qt.io/qt-5/signalsandslots.html - you don't need to sleep your GUI thread and still do post (or subsequent) app.exec() C++ processing.

                You can then plug your QML (whatever) slots from the signals your c++ classes emit. The gui (qml/whatever) can also send signals to your c++ class slots and there's rainbows and unicorns and the world is at peace and stuff...

                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