Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved QProcess not working.

    General and Desktop
    5
    29
    906
    Loading More Posts
    • 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.
    • SPlatten
      SPlatten last edited by

      I am using Qt 5.9.2 with MSVC2015 64bit. I am trying to execute mysqldump using QProcess. I've tried several different things, below is the latest code, none of these work and in the Event View I see:

      Log Name:      Application
      Source:        MariaDB
      Date:          26/07/2021 08:18:28
      Event ID:      100
      Task Category: None
      Level:         Warning
      Keywords:      Classic
      User:          N/A
      Computer:      LHR1-EUD-L20381
      Description:
      Aborted connection 84 to db: 'training' user: 'trainer' host: 'localhost' (Got an error reading communication packets)
       
      Event Xml:
      <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
        <System>
          <Provider Name="MariaDB" />
          <EventID Qualifiers="49152">100</EventID>
          <Version>0</Version>
          <Level>3</Level>
          <Task>0</Task>
          <Opcode>0</Opcode>
          <Keywords>0x80000000000000</Keywords>
          <TimeCreated SystemTime="2021-07-26T07:18:28.6591139Z" />
          <EventRecordID>149973</EventRecordID>
          <Correlation />
          <Execution ProcessID="0" ThreadID="0" />
          <Channel>Application</Channel>
          <Computer>LHR1-EUD-L20381</Computer>
          <Security />
        </System>
        <EventData>
          <Data>Aborted connection 84 to db: 'training' user: 'trainer' host: 'localhost' (Got an error reading communication packets)
       
      </Data>
        </EventData>
      </Event>
      

      My code:

      process* pobjProcess(new process(this));
                  if ( pobjProcess != nullptr )
                  {
                      QString strApp(DataSets::mscszMariaDBdumpApp);
                      QStringList slstArguments;
                      slstArguments << ("\"" + strDBinstFolder + strApp + "\"")
                                    << (QString(DataSets::mscszOptionHost) + Trainer::strHost())
                                    << (QString(DataSets::mscszOptionUser) + Trainer::strUser())
                                    << (QString(DataSets::mscszOptionPassword) + Trainer::strPassword())
                                    << (Trainer::strDatabase())
                                    << (QString(">") + strFilename);
                      strApp = "";
                      foreach( QString strArg, slstArguments )
                      {
                          if ( strApp.isEmpty() != true )
                          {
                              strApp += " ";
                          }
                          strApp += strArg;
                      }
      qdbg() << strApp;
                      pobjProcess->setWorkingDirectory(strDBinstFolder);
                      pobjProcess->setArguments(slstArguments);
                      pobjProcess->setProgram(strApp);
                      pobjProcess->start();
                      QObject::connect(pobjProcess, &QProcess::stateChanged,
                          [pobjProcess](QProcess::ProcessState newState)
                          {
                              const qint64 cint64PID(pobjProcess->processId());
                              if ( newState == QProcess::NotRunning )
                              {
       
                              }
                              else if ( newState == QProcess::Running && cint64PID > 0 )
                              {
       
                              }
      qdbg() << "PID: " << cint64PID << ", newState: " << newState;
                          }
                      );
                  }
      

      In the console I am seeing:

      PID: 4968, newState:  QProcess::ProcessState(NotRunning)
      

      The actual command line is:

      "C:/Program Files (x86)/MariaDB 10.5/bin/mysqldump" --host=localhost --user=trainer --password=CobhamAC training >C:/Users/simon.platten/Documents/TrainerS1D5.sql
      

      If I copy the above and paste into a command prompt, it works. But not from the application.

      Kind Regards,
      Sy

      J.Hilk KroMignon jsulm JonB 4 Replies Last reply Reply Quote 0
      • JonB
        JonB @SPlatten last edited by

        @SPlatten said in QProcess not working.:

        @JonB , where are you getting process and exec from?

        It's an example of QProcess::exec(). Actually that's static. Exactly the same principle if you want to use pobjProcess->start();. It's the argument passing I'm trying to tell you about.

        if I copy the command line and arguments to a command prompt it works,

        Yes, as I said this will (only) work from cmd.

                                  << QString(">")
        
                                  << strFilename;
        

        As I said, this won't work for mysqldump run as an executable. It will only work via cmd /c .... Up to you whether you believe me :)

        SPlatten 1 Reply Last reply Reply Quote 0
        • J.Hilk
          J.Hilk Moderators @SPlatten last edited by

          @SPlatten said in QProcess not working.:

          pobjProcess->setArguments(slstArguments);
          pobjProcess->setProgram(strApp);

          hi,

          you do realise, that at this point strApp also has the arguments appended to it?

          it should be only the program, and the arguments as string list separately

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

          Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          SPlatten 1 Reply Last reply Reply Quote 2
          • KroMignon
            KroMignon @SPlatten last edited by

            @SPlatten said in QProcess not working.:

            If I copy the above and paste into a command prompt, it works. But not from the application.

            I have 2 remarks:

            1. I am always surprise about your coding style, it seems that you always have to overload every Qt base class. If I have right understood your code, process is a sub-class of QProcess.
            2. It looks to me as the application is also part of the arguments, why?

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            1 Reply Last reply Reply Quote 1
            • jsulm
              jsulm Lifetime Qt Champion @SPlatten last edited by

              @SPlatten Why don't you connect a slot to https://doc.qt.io/qt-5/qprocess.html#errorOccurred and check what https://doc.qt.io/qt-5/qiodevice.html#errorString returns?

              I would split "<< (QString(">") + strFilename);" into two parameters.

              process* pobjProcess(new process(this));
              if ( pobjProcess != nullptr )
              

              Why do you check pobjProcess pointer after alloocating memory? This is useless as in case of out of memory your app will be terminated anyway.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              J.Hilk 1 Reply Last reply Reply Quote 1
              • J.Hilk
                J.Hilk Moderators @jsulm last edited by

                @jsulm said in QProcess not working.:

                This is useless as in case of out of memory your app will be terminated anyway.

                not necessarily
                I see 2 cases where one would do such a thing

                • if the OP compiles with -fno-exceptions than it will be indeed a null pointer returned instead of an exception
                • If the OP overloaded new and explicitly returns nullptr if some condition is met/not met

                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

                Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply Reply Quote 1
                • JonB
                  JonB @SPlatten last edited by

                  @SPlatten said in QProcess not working.:

                  "C:/Program Files (x86)/MariaDB 10.5/bin/mysqldump" --host=localhost --user=trainer --password=CobhamAC training >C:/Users/simon.platten/Documents/TrainerS1D5.sql

                  You have chosen to use a DOS/Windows > redirection symbol on your command line. This cannot be executed directly, i.e. executing mysqldump and passing >... to it.

                  If you do it this way you must execute via

                  QStringList args { "/c", "\"C:\\Program Files (x86)\\MariaDB 10.5\\bin\\mysqldump\" --host=localhost --user=trainer --password=CobhamAC training >C:\\Users\\simon.platten\\Documents\\TrainerS1D5.sql"  };
                  process->exec("cmd", args);
                  

                  Or handle the output redirection yourself in the caller, or via an "output-to" argument to mysqldump if it supports that.

                  SPlatten 1 Reply Last reply Reply Quote 1
                  • SPlatten
                    SPlatten @J.Hilk last edited by

                    @J-Hilk , yes, it didn't originally, I add those later just to see if it made any difference.

                    Kind Regards,
                    Sy

                    KroMignon 1 Reply Last reply Reply Quote 0
                    • SPlatten
                      SPlatten @JonB last edited by

                      @JonB , where are you getting process and exec from?

                      Kind Regards,
                      Sy

                      JonB 1 Reply Last reply Reply Quote 0
                      • SPlatten
                        SPlatten last edited by SPlatten

                        I've changed the code back to:

                        process* pobjProcess(new process(this));
                                    if ( pobjProcess != nullptr )
                                    {
                                        QString strApp(DataSets::mscszMariaDBdumpApp);
                                        QStringList slstArguments;
                                        slstArguments << ("\"" + strDBinstFolder + strApp + "\"")
                                                      << (QString(DataSets::mscszOptionHost) + Trainer::strHost())
                                                      << (QString(DataSets::mscszOptionUser) + Trainer::strUser())
                                                      << (QString(DataSets::mscszOptionPassword) + Trainer::strPassword())
                                                      << (Trainer::strDatabase())
                                                      << QString(">")
                                                      << strFilename;
                        qdbg() << slstArguments;
                        qdbg() << strApp;
                                        pobjProcess->setWorkingDirectory(strDBinstFolder);
                                        pobjProcess->setArguments(slstArguments);
                                        pobjProcess->setProgram(strApp);
                                        pobjProcess->start();
                                        QObject::connect(pobjProcess, &QProcess::errorOccurred,
                                             [this](QProcess::ProcessError err)
                                            {
                        qdbg() << "Error: " << err;
                                            }
                                        );
                                        QObject::connect(pobjProcess, &QProcess::stateChanged,
                                            [pobjProcess](QProcess::ProcessState newState)
                                            {
                                                const qint64 cint64PID(pobjProcess->processId());
                                                if ( newState == QProcess::NotRunning )
                                                {
                         
                                                }
                                                else if ( newState == QProcess::Running && cint64PID > 0 )
                                                {
                         
                                                }
                        qdbg() << "PID: " << cint64PID << ", newState: " << newState;
                                            }
                                        );
                                    }
                        

                        Still doesn't work and I'm not seeing anything errorOccurred

                        Kind Regards,
                        Sy

                        1 Reply Last reply Reply Quote 0
                        • KroMignon
                          KroMignon @SPlatten last edited by KroMignon

                          @SPlatten said in QProcess not working.:

                          yes, it didn't originally, I add those later just to see if it made any difference.

                          I don't really know what is the purpose of this, I guess you have to get SQL statements from you DB.

                          My way to achieve it would be:

                          • start mysqldump with QProcess
                          • wait until QProcess / mysqldump finished
                          • read QProcess output (cf. QProcess::readAll()) ==> no need of redirection at all

                          And last remark:
                          you have to do connection before starting process, to be sure not to loose state changes ;)

                          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                          SPlatten 1 Reply Last reply Reply Quote 1
                          • SPlatten
                            SPlatten @KroMignon last edited by

                            @KroMignon I don't follow you, if I copy the command line and arguments to a command prompt it works, the arguments are everything it needs for mysqldump to connect and export the database.

                            Kind Regards,
                            Sy

                            KroMignon 1 Reply Last reply Reply Quote 0
                            • JonB
                              JonB @SPlatten last edited by

                              @SPlatten said in QProcess not working.:

                              @JonB , where are you getting process and exec from?

                              It's an example of QProcess::exec(). Actually that's static. Exactly the same principle if you want to use pobjProcess->start();. It's the argument passing I'm trying to tell you about.

                              if I copy the command line and arguments to a command prompt it works,

                              Yes, as I said this will (only) work from cmd.

                                                        << QString(">")
                              
                                                        << strFilename;
                              

                              As I said, this won't work for mysqldump run as an executable. It will only work via cmd /c .... Up to you whether you believe me :)

                              SPlatten 1 Reply Last reply Reply Quote 0
                              • KroMignon
                                KroMignon @SPlatten last edited by KroMignon

                                @SPlatten said in QProcess not working.:

                                I don't follow you, if I copy the command line and arguments to a command prompt it works, the arguments are everything it needs for mysqldump to connect and export the database.

                                With QProcess, you don't start the process from a shell / command prompt, that is the difference here.
                                Try this:

                                auto proc = new QProcess;
                                QObject::connect(proc, &QProcess::stateChanged,
                                                    [proc](QProcess::ProcessState newState)
                                {
                                    qDebug() << "new state is " <<  newState;
                                    if(newState == QProcess::NotRunning)
                                    {
                                        QString output(proc->readAll());
                                        qDebug() << "Output is" << output;
                                        proc->delateLater();
                                    }
                                });
                                proc->setWorkingDirectory(strDBinstFolder);
                                proc->start(strApp,slstArguments );
                                

                                EDIT: do NOT add redirection in slstArguments !!!

                                EDIT BIS:

                                QStringList slstArguments;
                                slstArguments << (QString(DataSets::mscszOptionHost) + Trainer::strHost())
                                                              << (QString(DataSets::mscszOptionUser) + Trainer::strUser())
                                                              << (QString(DataSets::mscszOptionPassword) + Trainer::strPassword())
                                                              << (Trainer::strDatabase());
                                

                                ==> I suppose that DataSets::mscszOptionHost contains --host=

                                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                SPlatten 2 Replies Last reply Reply Quote 1
                                • SPlatten
                                  SPlatten @JonB last edited by

                                  @JonB , not in Qt 5.9.2 it ins't, execute ?

                                  Kind Regards,
                                  Sy

                                  JonB 1 Reply Last reply Reply Quote 0
                                  • JonB
                                    JonB @SPlatten last edited by JonB

                                    @SPlatten
                                    int QProcess::execute(const QString &program, const QStringList &arguments) [static]. So it's execute. It was introduced into some Qt. I don't know about 5.9.2.

                                    As I said, forget I ever wrote exec or execute. The same argument processing applies to QProcess::start. You keep passing > argument, and I've explained this won't work.

                                    SPlatten 1 Reply Last reply Reply Quote 0
                                    • SPlatten
                                      SPlatten @KroMignon last edited by

                                      @KroMignon , I've tried this:

                                      QString strApp(DataSets::mscszMariaDBdumpApp);
                                      QStringList slstArguments;
                                      slstArguments << ("\"" + strDBinstFolder + strApp + "\"")
                                                    << (QString(DataSets::mscszOptionHost) + Trainer::strHost())
                                                    << (QString(DataSets::mscszOptionUser) + Trainer::strUser())
                                                    << (QString(DataSets::mscszOptionPassword) + Trainer::strPassword())
                                                    << (Trainer::strDatabase())
                                                    << QString(">")
                                                    << strFilename;
                                      pobjProcess->setWorkingDirectory(strDBinstFolder);
                                      pobjProcess->start(strApp, slstArguments);
                                      QObject::connect(pobjProcess, &QProcess::errorOccurred,
                                          [this](QProcess::ProcessError err) {
                                      qDebug() << "Error: " << err;
                                      });
                                      QObject::connect(pobjProcess, &QProcess::stateChanged,
                                          [pobjProcess](QProcess::ProcessState newState)  {
                                              const qint64 cint64PID(pobjProcess->processId());
                                              if ( newState == QProcess::NotRunning )  {
                                              }  else if ( newState == QProcess::Running && cint64PID > 0 )  {
                                              }
                                      qDebug() << "PID: " << cint64PID << ", newState: " << newState;
                                      });
                                      

                                      Kind Regards,
                                      Sy

                                      jsulm KroMignon 2 Replies Last reply Reply Quote 0
                                      • jsulm
                                        jsulm Lifetime Qt Champion @SPlatten last edited by

                                        @SPlatten Did you notice "EDIT: do NOT add redirection in slstArguments !!!" in @KroMignon post?
                                        Why do you need redirection to a file if you can simply read the stdout from the process as @KroMignon already suggested?

                                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        SPlatten 1 Reply Last reply Reply Quote 0
                                        • KroMignon
                                          KroMignon @SPlatten last edited by KroMignon

                                          @SPlatten This don't make sense to me:

                                          • Why do you add application into argument list?
                                          • Why to you redirect it to a file and not directly read it for QProcess stream?

                                          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                          SPlatten 1 Reply Last reply Reply Quote 1
                                          • SPlatten
                                            SPlatten @jsulm last edited by

                                            @jsulm The purpose of this is to provide an export / backup facility of the database to an SQL file.

                                            Kind Regards,
                                            Sy

                                            jsulm 1 Reply Last reply Reply Quote 0
                                            • jsulm
                                              jsulm Lifetime Qt Champion @SPlatten last edited by

                                              @SPlatten You can still write the stdout from the process to a file...

                                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                                              1 Reply Last reply Reply Quote 0
                                              • First post
                                                Last post