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. Always returns 0
Qt 6.11 is out! See what's new in the release blog

Always returns 0

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 5 Posters 2.1k Views 3 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi and welcome to devnet,

    What is mountpoint ?
    Does it return any error code ?

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    0
    • PothosP Pothos

      I'm trying this simple program for learning and protoing purposes.

      In this example result is always 0 regardless of what I type into the input field. What am I doing wrong here?

      1f15fe13-c2bf-47f7-baab-d22bb3e19130-image.png

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #3

      @Pothos
      Please paste code I can copy, not a picture.

      QProcess::execute() is static. You're not getting the exit code you think you are into your code.

      BTW, conceptually you don't want a QProcess::waitForStarted()before you have given it a process to start.

      @SGaist
      It's https://www.man7.org/linux/man-pages/man1/mountpoint.1.html. Its exit code should vary according to what path is passed to it, that's what the OP is getting at.

      SGaistS 1 Reply Last reply
      2
      • JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by JoeCFD
        #4

        testProcess->readAllStandardOutput() to match the output of command line run

        1 Reply Last reply
        0
        • JonBJ JonB

          @Pothos
          Please paste code I can copy, not a picture.

          QProcess::execute() is static. You're not getting the exit code you think you are into your code.

          BTW, conceptually you don't want a QProcess::waitForStarted()before you have given it a process to start.

          @SGaist
          It's https://www.man7.org/linux/man-pages/man1/mountpoint.1.html. Its exit code should vary according to what path is passed to it, that's what the OP is getting at.

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #5

          @JonB Good catch !

          I was checking for mountpoint because of the unusual path it's located in. It might be a typo from @Pothos though.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • PothosP Offline
            PothosP Offline
            Pothos
            wrote on last edited by Pothos
            #6

            @JonB
            Here is the code copied:

            void MainWindow::on_testButton_clicked()
            {
                QString path = ui->testPlainTextEdit->toPlainText();
                QProcess *testProcess = new QProcess(this);
            
                testProcess->waitForStarted();
            
                testProcess->execute("/usr/bin/mountpoint", {"-q", path});
                testProcess->waitForFinished();
                int result = testProcess->exitCode();
            
                qDebug()<< result;
                if (result == 0) {
                    ui->testLabel->setText("Mounted.");
                } else {
                    ui->testLabel->setText("Not mounted.");
                }
            
                ui->testPlainTextEdit->clear();
                ui->testPlainTextEdit->setFocus();
            
                // Close process and delete pointer variable
                testProcess->close();
                delete testProcess;
            }
            

            @JoeCFD how to use?

            @SGaist yes it was a typo. Still doesn't work though.

            1 Reply Last reply
            0
            • PothosP Pothos

              I'm trying this simple program for learning and protoing purposes.

              In this example result is always 0 regardless of what I type into the input field. What am I doing wrong here?

              1f15fe13-c2bf-47f7-baab-d22bb3e19130-image.png

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by JonB
              #7

              @Pothos
              You are mixing two possibilities for running your command.

              If you want to use wattForStarted/Finished() you must run your command via start().

              Here for simplicity you could use QProcess::execute(), in which case its return result is the exit code you are looking for.

              Please go look at the doc page for QProcess::execute() and read what it says. That is where your problem is.

              PothosP 1 Reply Last reply
              3
              • JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by
                #8

                QByteArray QProcess::readAllStandardOutput(). You convert QByteArray to what you expect.

                1 Reply Last reply
                0
                • JonBJ JonB

                  @Pothos
                  You are mixing two possibilities for running your command.

                  If you want to use wattForStarted/Finished() you must run your command via start().

                  Here for simplicity you could use QProcess::execute(), in which case its return result is the exit code you are looking for.

                  Please go look at the doc page for QProcess::execute() and read what it says. That is where your problem is.

                  PothosP Offline
                  PothosP Offline
                  Pothos
                  wrote on last edited by
                  #9

                  @JonB
                  Okay so QProcess::execute() is a static method. I managed to get my program work simply with int result = QProcess::execute("/usr/bin/mountpoint", {"-q", path}). Thank you for the help.

                  I'm still wondering: Can one say that either execute() or start() is better in this particular case or is it just a matter of preference?

                  JonBJ 1 Reply Last reply
                  0
                  • Kent-DorfmanK Offline
                    Kent-DorfmanK Offline
                    Kent-Dorfman
                    wrote on last edited by
                    #10

                    Do you want concurrency or do you want to block until the process is completed?

                    The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

                    1 Reply Last reply
                    0
                    • PothosP Pothos

                      @JonB
                      Okay so QProcess::execute() is a static method. I managed to get my program work simply with int result = QProcess::execute("/usr/bin/mountpoint", {"-q", path}). Thank you for the help.

                      I'm still wondering: Can one say that either execute() or start() is better in this particular case or is it just a matter of preference?

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by JonB
                      #11

                      @Pothos
                      execute() "worked" here because it was the quickest way to show you the return result you thought was wrong.

                      Because this command is so quick to execute you "get away" without noticing that the UI is blocked while that slot runs.

                      That is what @Kent-Dorfman is drawing to your attention.

                      If you really want to it right: use start(), but do not use those waitFor...() methods. Use the QProcess signals like finished() to get the result back. Then do whatever you want to do with the result in your slot for finished(). Connect the other signals too, like errorOccurred. If you do all this you'll understand a lot about how Qt programming with signals & slots works!

                      PothosP 1 Reply Last reply
                      2
                      • JonBJ JonB

                        @Pothos
                        execute() "worked" here because it was the quickest way to show you the return result you thought was wrong.

                        Because this command is so quick to execute you "get away" without noticing that the UI is blocked while that slot runs.

                        That is what @Kent-Dorfman is drawing to your attention.

                        If you really want to it right: use start(), but do not use those waitFor...() methods. Use the QProcess signals like finished() to get the result back. Then do whatever you want to do with the result in your slot for finished(). Connect the other signals too, like errorOccurred. If you do all this you'll understand a lot about how Qt programming with signals & slots works!

                        PothosP Offline
                        PothosP Offline
                        Pothos
                        wrote on last edited by
                        #12

                        @Kent-Dorfman in this particular case I think blocking the event loop is acceptable because, as JonB said, the command was so quick to execute, which means the user won't notice any freeze-up.

                        @JonB I'm going to try the start()-version next as I will be trying to run an rsync-command next using this Qt-app.

                        JonBJ 1 Reply Last reply
                        0
                        • PothosP Pothos

                          @Kent-Dorfman in this particular case I think blocking the event loop is acceptable because, as JonB said, the command was so quick to execute, which means the user won't notice any freeze-up.

                          @JonB I'm going to try the start()-version next as I will be trying to run an rsync-command next using this Qt-app.

                          JonBJ Online
                          JonBJ Online
                          JonB
                          wrote on last edited by
                          #13

                          @Pothos said in Always returns 0:

                          @JonB I'm going to try the start()-version next as I will be trying to run an rsync-command next using this Qt-app.

                          Yes, that will be an example of a "long-running" command. You will presumably not want your UI to be frozen/blocked while it executes, which it would be if you were to use execute(). You will indeed want to use start(), and the various signals, so that it runs asynchronously.

                          1 Reply Last reply
                          1

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved