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. Run command using QProcess
Qt 6.11 is out! See what's new in the release blog

Run command using QProcess

Scheduled Pinned Locked Moved General and Desktop
12 Posts 2 Posters 6.1k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello,
    I am developing application for windows which can shows the corrupt files in the directory with the use of QProcess. I have opened the file using QProcess but i want to know the exit status of the command means whether the is correctly opened or not using QProcess.
    Please help me out. How can i get the finished signal status using QProcess.

    Regards
    Abhinay
    S/w engg
    Virtual employee pvt ltd.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi, welcome to devnet

      You can wait till the process is finished with "waitForFinished()":http://qt-project.org/doc/qt-5/qprocess.html#waitForFinished and then check the return code with "exitCode()":http://qt-project.org/doc/qt-5/qprocess.html#exitCode

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Hello Chrls,
        I have done this but every time when i am checking the exit status, the exit status is always normal whether the file is corrupted or not. I want If the file is corrupted and if the file is not properly opened then my code should return false.
        I am just sending you the code as i have written.

        ///////////////////////////////////////////////////////////////////////////////////////
        QString StrValue = m_AllFileList.at(i);

        QProcess *p = new QProcess(this);

        p->start((const char*) StrValue.toAscii());

        p->waitForFinished();

        p->start("cmd.exe", QStringList() << "/C" << (const char*)
        StrValue.toAscii());

                    QProcess::ExitStatus Status = p->exitStatus();
        
        
                    if (Status == 0)
        

        {
        cout<<" File is corrupted " ;
        }

                    else 
                    {
                             cout<<" File is OK " 
                    }
        
        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          QString StrValue = m_AllFileList.at(i);

          QProcess *p = new QProcess(this);

          p->start((const char*) StrValue.toAscii());

          p->waitForFinished();

          p->start(“cmd.exe”, QStringList() << “/C” << (const char*) StrValue.toAscii());

          QProcess::ExitStatus Status = p->exitStatus();

          if (Status == 0) { cout<<” File is corrupted “ ; }

          else { cout<<” File is OK “ }

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Please use code tags when posting code (first button to the right).

            This doesn't look right. What is the actual process you start and does it have meaningful return codes? Why are you starting the process twice? One directly and second via cmd shell? The second time you don't even wait till it's finished before checking the status.

            The exitStatus() only reports if the process succeeded or crashed. You shouldn't compare it to 0. It can have two possible values: QProcess::NormalExit(which happens to be 0 now but is not guaranteed to stay so in the future) and QProcess::CrashExit.

            If you want to get the return code from the process use exitCode(), not exitStatus().

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              @QProcess *p = new QProcess(this);

              p->start("cmd.exe", QStringList() << "/C" << (const char*) StrValue.toAscii());

              p->waitForFinished();

              QProcess::ExitStatus Status = p->exitStatus();

              if (Status == 0) { cout<<” File is corrupted “ ; }

              else { cout<<” File is OK “; } @

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                Sorry for the previous one.
                The last code is the actual code.

                Thanks
                Abhinay Chauhan

                1 Reply Last reply
                0
                • Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Again, exitStatus() returns an enum with two values, not an int (0).
                  The value of QProcess::NormalExit(what you get) means only that the process finished without crashing. Depending on what your process is it will never crash (or at least shouldn't if you did it right).

                  If your process returns some sort of code like 0 for ok and 1 for corrupted files then check exitCode(), not exitStatus().

                  Btw. don't convert the param to const char*. You are putting it into a QStringList so it will be converted back to QString anyway.

                  1 Reply Last reply
                  0
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #9

                    I have updated the code but every time the (p->exitCode() == 0) whether the file is corrupted or the file is correct.

                    @QString StrValue = m_AllFileList.at(i);

                    QProcess *p = new QProcess(this);
                    p->start("cmd.exe", QStringList() << "/C" << StrValue.toAscii());
                    p->waitForFinished();

                    if (p->exitCode() == 0)
                    {
                    int temp = 0;
                    }

                    else
                    {
                    int temp = 0;
                    }@

                    1 Reply Last reply
                    0
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #10

                      @for (int i = 0; i < m_AllFileList.size(); ++i)
                      {
                      QString StrValue = m_AllFileList.at(i);

                      QProcess *p = new QProcess(this);
                      p->start("cmd.exe", QStringList() << "/C" << StrValue.toAscii());
                      p->waitForFinished();

                      if (p->exitCode() == 0)
                      {
                      int temp = 0;
                      }

                      else
                      {
                      int temp = 0;
                      }
                      }
                      @

                      1 Reply Last reply
                      0
                      • Chris KawaC Offline
                        Chris KawaC Offline
                        Chris Kawa
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        What exactly does m_AllFileList contain?
                        I hope you're not doing something like "cmd /C file.txt" because this would make no sense at all.

                        1 Reply Last reply
                        0
                        • ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by
                          #12

                          m_AllFileList is a list that contains the Path of the Audio, video and image files in the directory.

                          so i want to run the command on the files through QProcess to get the corrupt files in the directory.

                          for example - If m_AllFileList contains a image file path and we are going to open the same image file but there is not preview showing and the window photo viewer shows the dialog "Windows Photo viewer can't open this picture because ...."

                          so i want to get these files which are not opened by the Windows photo viewer, Windows Media Player for image and Audio video files respectively.

                          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