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. QProcess not working.
Forum Updated to NodeBB v4.3 + New Features

QProcess not working.

Scheduled Pinned Locked Moved Solved General and Desktop
29 Posts 5 Posters 3.7k 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.
  • SPlattenS SPlatten

    @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.

    KroMignonK Offline
    KroMignonK Offline
    KroMignon
    wrote on last edited by KroMignon
    #13

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

    SPlattenS 2 Replies Last reply
    1
    • JonBJ JonB

      @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 :)

      SPlattenS Offline
      SPlattenS Offline
      SPlatten
      wrote on last edited by
      #14

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

      Kind Regards,
      Sy

      JonBJ 1 Reply Last reply
      0
      • SPlattenS SPlatten

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

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #15

        @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.

        SPlattenS 1 Reply Last reply
        0
        • KroMignonK 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=

          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #16

          @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

          jsulmJ KroMignonK 2 Replies Last reply
          0
          • SPlattenS SPlatten

            @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;
            });
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #17

            @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

            SPlattenS 1 Reply Last reply
            0
            • SPlattenS SPlatten

              @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;
              });
              
              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by KroMignon
              #18

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

              SPlattenS 1 Reply Last reply
              1
              • jsulmJ jsulm

                @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?

                SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on last edited by
                #19

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

                Kind Regards,
                Sy

                jsulmJ 1 Reply Last reply
                0
                • SPlattenS SPlatten

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

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #20

                  @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
                  0
                  • KroMignonK 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?
                    SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #21

                    @KroMignon , as I said earlier I've tried numerous things, thats just one of variant.

                    Kind Regards,
                    Sy

                    jsulmJ 1 Reply Last reply
                    0
                    • SPlattenS SPlatten

                      @KroMignon , as I said earlier I've tried numerous things, thats just one of variant.

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by jsulm
                      #22

                      @SPlatten Also, as @JonB already wrote: you can't use redirection without a shell. Redirection is a feature of a shell (cmd.exe on Windows). So, you will need to execute cmd.exe as executable and pass everything else as parameters.

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

                      1 Reply Last reply
                      0
                      • JonBJ 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.

                        SPlattenS Offline
                        SPlattenS Offline
                        SPlatten
                        wrote on last edited by
                        #23

                        @JonB , just tried:

                        QProcess::execute(strApp);
                        

                        strApp contains:

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

                        Still not working.

                        Kind Regards,
                        Sy

                        jsulmJ JonBJ 2 Replies Last reply
                        0
                        • KroMignonK 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=

                          SPlattenS Offline
                          SPlattenS Offline
                          SPlatten
                          wrote on last edited by
                          #24

                          @KroMignon , yes it does, I'll try without the application which is where I started.

                          Kind Regards,
                          Sy

                          1 Reply Last reply
                          0
                          • SPlattenS SPlatten

                            @JonB , just tried:

                            QProcess::execute(strApp);
                            

                            strApp contains:

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

                            Still not working.

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by jsulm
                            #25

                            @SPlatten said in QProcess not working.:

                            QProcess::execute(strApp);

                            First parameter is the executable ("cmd.exe" in your case), second is a list of parameters...

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

                            1 Reply Last reply
                            0
                            • SPlattenS SPlatten

                              @JonB , just tried:

                              QProcess::execute(strApp);
                              

                              strApp contains:

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

                              Still not working.

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #26

                              @SPlatten
                              No, that is not what I said at all. Look at the example. The program needs to be cmd.exe. The arguments need to be first /c and second your whole command line (as a single argument; for cmd it doesn't seem to matter if you pass it as one string as I showed, or I think it will accept each argument separately). I showed you earlier an example of what you need.

                              SPlattenS 1 Reply Last reply
                              0
                              • JonBJ JonB

                                @SPlatten
                                No, that is not what I said at all. Look at the example. The program needs to be cmd.exe. The arguments need to be first /c and second your whole command line (as a single argument; for cmd it doesn't seem to matter if you pass it as one string as I showed, or I think it will accept each argument separately). I showed you earlier an example of what you need.

                                SPlattenS Offline
                                SPlattenS Offline
                                SPlatten
                                wrote on last edited by SPlatten
                                #27

                                @JonB , ok, having prefixed with cmd.exe it now works. Thank you, thank you everyone.

                                Kind Regards,
                                Sy

                                JonBJ 1 Reply Last reply
                                1
                                • SPlattenS SPlatten

                                  @JonB , ok, having prefixed with cmd.exe it now works. Thank you, thank you everyone.

                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on last edited by JonB
                                  #28

                                  @SPlatten
                                  Having said that. If the only purpose of having to go via cmd /c is to handle the redirection of output via >, I would handle that back in QProcess (I think others have suggested this) and then you won't need to go via cmd, which has its disadvantages.

                                  SPlattenS 1 Reply Last reply
                                  1
                                  • JonBJ JonB

                                    @SPlatten
                                    Having said that. If the only purpose of having to go via cmd /c is to handle the redirection of output via >, I would handle that back in QProcess (I think others have suggested this) and then you won't need to go via cmd, which has its disadvantages.

                                    SPlattenS Offline
                                    SPlattenS Offline
                                    SPlatten
                                    wrote on last edited by
                                    #29

                                    @JonB I will investigate now.

                                    Kind Regards,
                                    Sy

                                    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