Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

optimizing gui and parralel processing?



  • I would like to further improve my GUI based functionality.
    Right now i have working gui with various buttons and LineEdits. For example each button has its own functionality.

    void Widget::on_lineEdit_returnPressed()
    {
    
    
        QString value = "IP Address is :";
        QString value1 = ui->lineEdit->text();
        QString value2 = value + value1;
        ui->plainTextEdit->appendPlainText(value2);
    }
    
    void Widget::on_pushButton_pressed()
    {
    
    
        ui->plainTextEdit->appendHtml("<div style='color: green;'> Process Started with below Selected Parameters </div>");
        if(ui->lineEdit->text().isEmpty() || ui->lineEdit_2->text().isEmpty() || ui->lineEdit_3->text().isEmpty() || ui->lineEdit_4->text().isEmpty() )
        {
            ui->plainTextEdit->appendHtml("<div style='color: red;'> Process Failed due to some parameters missing. Please Check all the parameters needed Except in Check boxes and DropDown Menu </div>");
            ui->plainTextEdit->appendHtml("<div style='color: red;'> By Default CheckBoxes are disabled. Please Check the respective box if you want to enable </div>");
    
    
        }
        else
        {
            QString i1 = ui->lineEdit->text();
            QString i2 = ui->lineEdit_2->text();
            QString it1 = ui->lineEdit_3->text();
            QString it2 = ui->lineEdit_4->text();
            bool enaout = ui->checkBox->isChecked();
            QString res;
            if (enaout == true)
                res = "Output Enabled";
            else
                res = "Output Disabled";
    
            bool ra = ui->checkBox_2->isChecked();
            QString res1;
            if (ra == true)
                res1 = "RA is  Enabled";
            else
                res1 = "RA is Disabled";
    
            ui->plainTextEdit->appendHtml("i1:" + i1);
            ui->plainTextEdit->appendHtml("i2: " + i2);
            ui->plainTextEdit->appendHtml("it1: "+ it1);
            ui->plainTextEdit->appendHtml("it2: " + it2);
            ui->plainTextEdit->appendPlainText(res);
            ui->plainTextEdit->appendPlainText(res1);
    
    
            QString fileName = "Process.exe";
            if(QFileInfo(fileName).exists())
            {
                QStringList arguments;
                arguments << ui->lineEdit->text()<<ui->lineEdit_2->text();
    
                proc = new QProcess();
    
                proc->start(fileName,arguments);
                if(proc->state() != QProcess::NotRunning)
                {
                    proc->waitForFinished();
                    
                    QString result = proc->readAllStandardOutput();
                    ui->plainTextEdit->appendPlainText(result);
    
                }
                
                else{
                    ui->plainTextEdit->appendPlainText("Unable to find file with this name");
                }
                
    
            }
    
            else
                ui->plainTextEdit->appendHtml("<div style='color: red;'> Error While Opening Executable </div>");
    
            
        }
    }
    
    /*
    
    QProcess *proc was initialized in header and 
    proc = new Process is used in constructor.
    
    so in the above code i am directly using proc->somestuff
    
    
    */
    

    and similary for all other buttons/LineEdits.

    But however i realised that if i am using some heavy maths for example performing heavy calculations in any of the functionality it takes sometime to do that part. But my gui is unable do other tasks until output comes from one function.

    Is there a way to run all this process separately? For Example even though it takes time it should be like unable to respond install we can be able to perform other functionality by using some parrale processing/threads. I am not sure how to do this!!

    ![alt text](Capture1.PNG image url)

    This is extremely minimal version which is created now to show what I mean. For Example if i enter some information in lineEdit box then there is some corresponding function which needs to be executed when you hit enter. But until this function is completed my gui widget stays hanged, i mean i cannot enter information in 2nd lineEdit box!. SO what i would like here to do is have various parralel process, and most of time i dont have any dependecies with other boxes except with pushButton!

    FYI i checked forum but there are not many things related to this!

    I am not sure if my question title is prompt


  • Lifetime Qt Champion

    @aravmadd said in optimizing gui and parralel processing?:

    proc->waitForFinished();

    This will block your GUI until process finishes. Don't do this!
    Connect a slot to https://doc.qt.io/qt-5/qprocess.html#finished signal instead. And move this code

    QString result = proc->readAllStandardOutput();
    ui->plainTextEdit->appendPlainText(result);
    

    to that slot...



  • @aravmadd said in optimizing gui and parralel processing?:

    Is there a way to run all this process separately? For Example even though it takes time it should be like unable to respond install we can be able to perform other functionality by using some parrale processing/threads. I am not sure how to do this!

    proc->waitForFinished()

    The whole point is not to use QProcess::waitForFinished. That is what blocks the UI. Use QProcess signals (like finished) to update the UI. Then the processes run asynchronously to your application.



  • @jsulm said in optimizing gui and parralel processing?:

    QString result = proc->readAllStandardOutput();
    ui->plainTextEdit->appendPlainText(result);

    HI Thanks for replying and giving your valuable time and suggestions. However i didnt get/ understood to do what you have said .

    I understood like this
    connect(proc, SIGNAL(finished(int , QProcess::ExitStatus )),
    this, SLOT(i am not sure which slot sorry for my stupid questiom));


  • Lifetime Qt Champion

    @aravmadd said in optimizing gui and parralel processing?:

    i am not sure which slot sorry for my stupid questiom

    Well, your own slot inside Widget class. Simply add a slot in Widget with same parameters as the signal.



  • @jsulm Thanks i will give a try. Will update in forum if i can succefully implement!! Many Thanks :-)


  • Lifetime Qt Champion

    @aravmadd
    Hi
    Just so im sure i understand.
    You talk about doing heavy calc in app, calc, but you seem to show code running an extern process (another .exe)

    If you mean to run some other app, all is fine but i just have to ask :)



  • @mrjj HI! YOU are right i am also running other executable after taking all other inputs from gui. For this i am using process.

    I am also doing some heavy math calc for example using EIgen Library and other stuff to do some moderate math. But for doing this thing it is taking sometime, and during this time my gui gets blocked because of things which already mentioned by @jsulm and @JonB



  • @sm2770s
    Well what we and you have been talking about here is QProcess, which you will solve by using signals and slots. If you have heavy Eigen calculations you need to call in the Qt process then you will have to use separate threads if you need to keep the UI responsive.



  • @JonB said in optimizing gui and parralel processing?:

    which you will solve by using signals and slots. If you have heavy Eigen calculations you need to call in the Qt process then you will have to use separate threads if you need to keep the UI responsive.

    HI Jon! As of now i am planning to move my calculations into executable so it can be easy and my executable does that math stuff and i think that would be optimistic way.

    But what i would like to try for now is each button/checkbox or lineedit should run without making my gui unresponsive by using different process. For time being i am trying to ignore heavy calculations and give a try.

    Can you give some suggestion if this approach seems legit?



  • @sm2770s said in optimizing gui and parralel processing?:

    @JonB said in optimizing gui and parralel processing?:

    which you will solve by using signals and slots. If you have heavy Eigen calculations you need to call in the Qt process then you will have to use separate threads if you need to keep the UI responsive.

    HI Jon! As of now i am planning to move my calculations into executable so it can be easy and my executable does that math stuff and i think that would be optimistic way.

    But what i would like to try for now is each button/checkbox or lineedit should run without making my gui unresponsive by using different process. For time being i am trying to ignore heavy calculations and give a try.

    Can you give some suggestion if this approach seems legit?

    I may be reading this discussion wrong, but...

    If there is a time consuming task, it does not matter what approach you take - your GUI has to wait until the task returns valid data.
    Perhaps "blocking (GUI) " is incorrect term to use.
    Not sure if I agree to try to fool the user that the code is magically working "faster than a speeding bullet".
    My approach - KISS - I prefer QConnectivity whoops - wrong word - QtConcurrent and I use it to let the user know it is actually taking time to finish the task.
    Just my opinion.



  • @sm2770s
    So long as you do your computations out-of-process via QProcess::start() your UI will not become unresponsive, provided you use the signals/slots and not QProcess::waitFor...().



  • @AnneRanch I think blocking GUI is wrong word. I mean what you understood is right!



  • @JonB will give a look into what you mentioned . Thanks a lot again :-)



  • @AnneRanch Throw any time-consuming job into a thread or a qprocess and add a progress bar to show something is in progress. Keep GUI idle.


Log in to reply