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. startDetached fails under 5.15.2
QtWS25 Last Chance

startDetached fails under 5.15.2

Scheduled Pinned Locked Moved Unsolved General and Desktop
startdetached
18 Posts 4 Posters 2.1k Views
  • 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
    Jedd
    wrote on 5 May 2022, 18:55 last edited by
    #9
       qDebug() << "Start Detached";
        QStringList arguments;
        arguments << "/k";  //<< "cpath.bat";
        QProcess *process = new QProcess();
        process->startDetached("C:/windows/system32/cmd.exe", arguments);
        qDebug() << "error: " << process->error();
    
    

    Start Detached
    error: QProcess::UnknownError

    process->start returns the same error.

    J 1 Reply Last reply 6 May 2022, 07:11
    0
    • J Offline
      J Offline
      Jedd
      wrote on 6 May 2022, 02:56 last edited by
      #10

      Also tried:

      void MainWindow::on_pushButton_clicked()
      {
      
          qDebug() << "Start Detached";
          QProcess process;
          process.setProgram("C:/windows/system32/cmd.exe");
          process.setArguments({"/k"});
          process.startDetached();
          qDebug() << "error: " << process.error();
      }
      

      Same result.

      1 Reply Last reply
      0
      • J Jedd
        5 May 2022, 18:55
           qDebug() << "Start Detached";
            QStringList arguments;
            arguments << "/k";  //<< "cpath.bat";
            QProcess *process = new QProcess();
            process->startDetached("C:/windows/system32/cmd.exe", arguments);
            qDebug() << "error: " << process->error();
        
        

        Start Detached
        error: QProcess::UnknownError

        process->start returns the same error.

        J Offline
        J Offline
        JonB
        wrote on 6 May 2022, 07:11 last edited by JonB 5 Jun 2022, 07:20
        #11

        @Jedd said in startDetached fails under 5.15.2:

        error: QProcess::UnknownError

        That is almost certainly because there is no error! QProcess::error() has to return something, https://doc.qt.io/qt-5/qprocess.html#ProcessError-enum:

        QProcess::UnknownError 5 An unknown error occurred. This is the default return value of error().

        Unless something reports an error, like QProcess::errorOccurred(), there is no point looking at QProcess::error(). And in your first code it's not going going to return anything anyway because you use the static method bool QProcess::startDetached(const QString &program, const QStringList &arguments).

        I told you that if you want to find out what's going on you need to:

        • Use the instance method process.startDetached(). That is per your second code.
        • Attach slots to the signals it raises.

        I suggest you do so.

        Meanwhile if you want to test whether startDetached() works at all, I suggest you try a command like e.g. cmd /c echo hello > C:\some\writable\folder\filename, so that you can tell whether it created the file and therefore ran.

        1 Reply Last reply
        2
        • J Offline
          J Offline
          Jedd
          wrote on 7 May 2022, 01:21 last edited by
          #12

          Thanks for your patience, this is new ground for me.

          
          void MainWindow::on_pushButton_clicked()
          {
          
              qDebug() << "Start Detached";
              QStringList arguments;
              arguments << "/k";  //<< "cpath.bat";
              QProcess *process = new QProcess();
              connect(&process, &QProcess::errorOccurred, this, MainWindow::onSdetacherror);
              process->setProgram("C:/windows/system32/cmd.exe");
              process->setArguments({"/k"});
              process->startDetached("C:/windows/system32/cmd.exe", arguments);
          
          }
          
          
          void MainWindow::onSdetacherror()
          {
          
              qDebug() << "error:";
          }
          
          ====
          
          // from mainwindow.h
          private slots:
              void onSdetacherror();
          
          

          No joy yet. the connect statement is in error and I'm not sure how to reference errorOccured in the slot.

          J 1 Reply Last reply 7 May 2022, 06:32
          0
          • J Offline
            J Offline
            Jedd
            wrote on 7 May 2022, 03:10 last edited by
            #13

            BTW, following your suggestion, the code below works, showing that the startDetached call is working ...

            qDebug() << "Start Detached";
                QStringList arguments;
                arguments << "/c" << "echo xxxxx > /Users/jedd/Desktop/xxx.txt";
                process->startDetached("C:/windows/system32/cmd.exe", arguments);
            
            
            J 1 Reply Last reply 8 May 2022, 07:43
            0
            • J Jedd
              7 May 2022, 01:21

              Thanks for your patience, this is new ground for me.

              
              void MainWindow::on_pushButton_clicked()
              {
              
                  qDebug() << "Start Detached";
                  QStringList arguments;
                  arguments << "/k";  //<< "cpath.bat";
                  QProcess *process = new QProcess();
                  connect(&process, &QProcess::errorOccurred, this, MainWindow::onSdetacherror);
                  process->setProgram("C:/windows/system32/cmd.exe");
                  process->setArguments({"/k"});
                  process->startDetached("C:/windows/system32/cmd.exe", arguments);
              
              }
              
              
              void MainWindow::onSdetacherror()
              {
              
                  qDebug() << "error:";
              }
              
              ====
              
              // from mainwindow.h
              private slots:
                  void onSdetacherror();
              
              

              No joy yet. the connect statement is in error and I'm not sure how to reference errorOccured in the slot.

              J Offline
              J Offline
              JonB
              wrote on 7 May 2022, 06:32 last edited by JonB 5 Jul 2022, 06:39
              #14

              @Jedd said in startDetached fails under 5.15.2:

              No joy yet. the connect statement is in error and I'm not sure how to reference errorOccured in the slot.

              Please copy & paste any error messages you get rather than just stating there was an error! Saves me having to guess....

                  QProcess *process = new QProcess();
                  connect(&process, &QProcess::errorOccurred, this, MainWindow::onSdetacherror);
              

              You already have a QProcess *process so you do not want to take its address with &process. And both the signal and slot PointerToMemberFunctions require a leading &:

              connect(process, &QProcess::errorOccurred, this, &MainWindow::onSdetacherror);
              

              And the QProcess::errorOccurred(QProcess::ProcessError error) signal passes a parameter to the slot:

              void MainWindow::onSdetacherror(QProcess::ProcessError error)
              {
              
                  qDebug() << "error:" << error;
              }
              

              As for:

                  process->setProgram("C:/windows/system32/cmd.exe");
                  process->setArguments({"/k"});
                  process->startDetached("C:/windows/system32/cmd.exe", arguments);
              

              I'm going to say this again: please read what I wrote earlier about which QProcess::startDetached() method you must use if you are attaching slots to signals. The one which has string and string list arguments is bool QProcess::startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory = QString(), qint64 *pid = nullptr). That is a static method. Unfortunately C++ allows you to call static methods on an instance of the class (process->startDetached("...")), which makes it look like it is using your process, but in fact it is not. It's really QProcess::startDetached("..."), you might as well call it that way, and that shows it does not use your QProcess *. It creates its own internally. And so any slots you have connect()ed to your instance will not be called. Here you must use bool QProcess::startDetached(qint64 *pid = nullptr), which is not static, does use your instance and will activate your slots:

                  process->setProgram("C:/windows/system32/cmd.exe");
                  process->setArguments({"/k"});
                  process->startDetached();
              

              Please do not make this mistake again!

              While you're at it, you might as well add another signal slot handler, in case it tells us something:

              connect(process, &QProcess::stateChanged, this, &MainWindow::onProcessStateChanged);
              
              void MainWindow::onProcessStateChanged(QProcess::ProcessState newState)
              {
                  qDebug() << "newState:" << newState;
              }
              

              BTW, following your suggestion, the code below works, showing that the startDetached call is working ...

              Well that's a start! So we know startDetached() itself does work, as it should.

              I do not know at this point why your code which works at 5.6 should not work at 5.15. Let's hope that errorOccurred /stateChanged slots reveal something.

              1 Reply Last reply
              1
              • J Jedd
                7 May 2022, 03:10

                BTW, following your suggestion, the code below works, showing that the startDetached call is working ...

                qDebug() << "Start Detached";
                    QStringList arguments;
                    arguments << "/c" << "echo xxxxx > /Users/jedd/Desktop/xxx.txt";
                    process->startDetached("C:/windows/system32/cmd.exe", arguments);
                
                
                J Offline
                J Offline
                JonB
                wrote on 8 May 2022, 07:43 last edited by
                #15

                @Jedd
                I have a possible theory as to what might be going on, if start() does what you want but startDetached() does not.

                Please don't let this reply stop you reading my (long) previous one, and trying the things I ask you to there.

                I'd like to summarize which things do work and which do not. I have 4 things for you to try:

                QProcess *process = new QProcess(); followed by:

                1. process->start("C:/windows/system32/cmd.exe", { "/k" });
                2. process->startDetached("C:/windows/system32/cmd.exe", { "/k" });
                3. process->start("C:/windows/system32/cmd.exe", { "/k", "echo xxxxx > /Users/jedd/Desktop/xxx.txt" });
                4. process->startDetached("C:/windows/system32/cmd.exe", { "/k", "echo yyyyy > /Users/jedd/Desktop/yyy.txt" });

                For 1 & 2, in which one do you see a visible console remain?
                For 3 & 4, do you see a visible console, but more significantly do they both create their respective files even if you don't see a console?

                Depending on results, this may all be to do with the discussions in https://forum.qt.io/topic/135952/executing-cmd-exe-with-some-console-application.

                The strange thing from that is that as per @VRonin's post https://forum.qt.io/topic/135952/executing-cmd-exe-with-some-console-application/13 and my reply to that the implication is that it is start() which should fail to open the console window while startDetached() should allow it, which seems to be the opposite of what you are reporting?

                Anyway, let me have clear & simple answers as to how 1--4 above behave wrt my questions about them, and we'll take it from there.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  Jedd
                  wrote on 8 May 2022, 21:00 last edited by
                  #16
                  1. process->start("C:/windows/system32/cmd.exe", { "/k" });
                  2. process->startDetached("C:/windows/system32/cmd.exe", { "/k" });
                  3. process->start("C:/windows/system32/cmd.exe", { "/k", "echo xxxxx > /Users/jedd/Desktop/xxx.txt" });
                  4. process->startDetached("C:/windows/system32/cmd.exe", { "/k", "echo yyyyy > /Users/jedd/Desktop/yyy.txt" });
                  

                  For 1 and 2 there is no visible console at any time

                  For 3 and 4 there is no visible console, but for both start and startDetached the respective text file is produced

                  Using the code below:
                  with startDetached, neither NewState or error are triggered

                  with start I get:

                  newState: QProcess::Starting
                  newState: QProcess::Running

                  void MainWindow::on_pushButton_clicked()
                  {
                  
                      qDebug() << "Start Detached";
                      QStringList arguments;
                      arguments << "/k";  
                      QProcess *process = new QProcess();
                      connect(process, &QProcess::errorOccurred, this, &MainWindow::onSdetacherror);
                      connect(process, &QProcess::stateChanged, this, &MainWindow::onProcessStateChanged);
                      process->setProgram("C:/windows/system32/cmd.exe");
                      process->setArguments(arguments);
                      process->startDetached("C:/windows/system32/cmd.exe", arguments);
                  
                  }
                  
                  
                  
                  void MainWindow::onProcessStateChanged(QProcess::ProcessState newState)
                  {
                      qDebug() << "newState:" << newState;
                  }
                  
                  void MainWindow::onSdetacherror(QProcess::ProcessError error)
                  {
                  
                      qDebug() << "error:" << error;
                  }
                  
                  
                  J 1 Reply Last reply 9 May 2022, 07:56
                  0
                  • J Jedd
                    8 May 2022, 21:00
                    1. process->start("C:/windows/system32/cmd.exe", { "/k" });
                    2. process->startDetached("C:/windows/system32/cmd.exe", { "/k" });
                    3. process->start("C:/windows/system32/cmd.exe", { "/k", "echo xxxxx > /Users/jedd/Desktop/xxx.txt" });
                    4. process->startDetached("C:/windows/system32/cmd.exe", { "/k", "echo yyyyy > /Users/jedd/Desktop/yyy.txt" });
                    

                    For 1 and 2 there is no visible console at any time

                    For 3 and 4 there is no visible console, but for both start and startDetached the respective text file is produced

                    Using the code below:
                    with startDetached, neither NewState or error are triggered

                    with start I get:

                    newState: QProcess::Starting
                    newState: QProcess::Running

                    void MainWindow::on_pushButton_clicked()
                    {
                    
                        qDebug() << "Start Detached";
                        QStringList arguments;
                        arguments << "/k";  
                        QProcess *process = new QProcess();
                        connect(process, &QProcess::errorOccurred, this, &MainWindow::onSdetacherror);
                        connect(process, &QProcess::stateChanged, this, &MainWindow::onProcessStateChanged);
                        process->setProgram("C:/windows/system32/cmd.exe");
                        process->setArguments(arguments);
                        process->startDetached("C:/windows/system32/cmd.exe", arguments);
                    
                    }
                    
                    
                    
                    void MainWindow::onProcessStateChanged(QProcess::ProcessState newState)
                    {
                        qDebug() << "newState:" << newState;
                    }
                    
                    void MainWindow::onSdetacherror(QProcess::ProcessError error)
                    {
                    
                        qDebug() << "error:" << error;
                    }
                    
                    
                    J Offline
                    J Offline
                    JonB
                    wrote on 9 May 2022, 07:56 last edited by JonB 5 Sept 2022, 07:57
                    #17

                    @Jedd said in startDetached fails under 5.15.2:

                    with startDetached, neither NewState or error are triggered

                    process->startDetached("C:/windows/system32/cmd.exe", { "/k", "echo yyyyy > /Users/jedd/Desktop/yyy.txt" });

                    I wrote you a whole, long diatribe on why process->startDetached("...") will not trigger any signals. I explained this not once but twice:

                    I told you that if you want to find out what's going on you need to:

                    • Use the instance method process.startDetached(). That is per your second code.

                    I'm going to say this again: please read what I wrote earlier about which QProcess::startDetached() method you must use if you are attaching slots to signals.

                    You really should be able to get this right by now. I cannot/will not keep typing the same in over & over.

                    I do not know why neither start() nor startDetached() show you a console. It does look like they are both running successfully.

                    I suggest you read carefully through https://forum.qt.io/topic/135952/executing-cmd-exe-with-some-console-application. Among other posts there you will see:

                    You are great! Thanks a lot!

                    process.setProgram("C:\\Windows\\System32\\cmd.exe");
                    process.setArguments({"/k", "pause"});
                    qint64 pid;
                    process.startDetached(&pid);
                    

                    displays console with pause text.

                    You see that user reports it "displays console" I do not know why his does and yours does not. I would expect your start() not to display console but startDetached() to display console.

                    Anyway, all I can sugest is that you try the post I made there at https://forum.qt.io/topic/135952/executing-cmd-exe-with-some-console-application/15

                    Not tested, but can't you change this in the same way in your setCreateProcessArgumentsModifier() anyway, just add the first line to whatever you want:

                    args->flags &= ~CREATE_NO_WINDOW;  // switch that *off*
                    args->flags |= CREATE_NEW_CONSOLE; 
                    ...
                    

                    Try this with both start() & startDetached(). For the final time: do not use this with [static] startDetached("...") method, only with startDetached() [non-static] one.

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      Jedd
                      wrote on 9 May 2022, 13:59 last edited by
                      #18

                      Thanks for your help.

                      1 Reply Last reply
                      0

                      18/18

                      9 May 2022, 13:59

                      • Login

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