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. QT GUI launches after a long time due to a long function in the constructor
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 2.2k Views 1 Watching
  • 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.
  • J Offline
    J Offline
    johncharlesbgmail.com
    wrote on last edited by
    #1

    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;
    

    }

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      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.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • J Offline
        J Offline
        johncharlesbgmail.com
        wrote on last edited by
        #3

        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;
        }
        @

        1 Reply Last reply
        0
        • J Offline
          J Offline
          johncharlesbgmail.com
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #5

            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()

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            0
            • J Offline
              J Offline
              johncharlesbgmail.com
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #7

                Subclass it and add your own functions :)

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                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