C++ loop with GUI



  • 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



  • 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 ?


  • Qt Champions 2016

    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.



  • @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



  • @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



  • @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



  • @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...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.