QT GUI launches after a long time due to a long function in the constructor



  • I wrote an application that pings an IP (using QProcess) and update the info accordingly in the GUI. When the ping takes time to respond, the GUI was still launching and only after the ping function is done, it launches the application.
    Initially I called the function in the constructor but after googling I got some info and I tried overriding ShowEvent(). Also in ShowEvent() I tried QTimer::singleShot and QtConcurrent::run but no luck. Still takes its own time and only the function is completed the GUI is displayed.
    Any idea how I can make the GUI to display first and then update the info in GUI once the function is completed?

    Below is my function:
    bool pingIP(QString strIP)
    {
    QProcess *connected = new QProcess(0);
    QString exec="ping";
    QStringList params;
    params << "-n" << "1" << strIP;
    connected->start(exec,params, QIODevice::ReadOnly);

    if(!connected->waitForFinished())
    {
        return false;
    }
    
    QString strOut = connected->readAllStandardOutput();
    if(connected->exitCode() == 0)
    {
        if(strOut.contains("unreachable"))
        {
            return false;
        }
        return true;
    }
    return false;
    

    }


  • Moderators

    Please wrap you code in code tags (put '@' before and after your code), split it into multiple lines, and use indentation. It's hard to read!

    Anyway, the simple solution is to take the long function out of the GUI constructor. Create and display your widget/window first, and then run the long function. After the function returns, update your widget/window.



  • I wrote an application that pings an IP (using QProcess) and update the info accordingly in the GUI. When the ping takes time to respond, the GUI was still launching and only after the ping function is done, it launches the application.
    Initially I called the function in the constructor but after googling I got some info and I tried overriding ShowEvent(). Also in ShowEvent() I tried QTimer::singleShot and QtConcurrent::run but no luck. Still takes its own time and only the function is completed the GUI is displayed.
    Any idea how I can make the GUI to display first and then update the info in GUI once the function is completed?

    Below is my function:
    @
    bool pingIP(QString strIP)
    {
    QProcess *connected = new QProcess(0);
    QString exec=“ping”; QStringList params; params << “-n” << “1” << strIP;
    connected->start(exec,params, QIODevice::ReadOnly);

    if(!connected->waitForFinished())
    {
    return false;
    }

    QString strOut = connected->readAllStandardOutput();

    if(connected->exitCode() == 0)
    {
    if(strOut.contains(“unreachable”))
    {
    return false;
    }

    return true; 
    

    }

    return false;
    }
    @



  • The problem is, I need to call the function when it launches. And the GUI is coming only after the function returns. I just need a mechanism to show the GUI and the call should run on the background so that I can update the GUI whenever the function returns.

    I tried, QTimer::singleShot and QtConcurrent::run but no luck


  • Moderators

    My original answer still stands: You can call the function after you finish constructing your GUI.

    @
    // 1. Construct and show the GUI first...
    MyWidget w;
    w.show();

    // 2. Then call the function...
    bool result = pingIP("127.0.0.1");

    // 3. Then update your GUI
    w.updateGui(result);
    @

    If you can't use that, post the code that creates the GUI and calls pingIP()



  • Actually, I'm using QMainWindow. Is there any equvialent functionality available for QMainWindow?


  • Moderators

    Subclass it and add your own functions :)


Log in to reply
 

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