Home > Blockchain >  QProcess start Notepad with Windows "start" program
QProcess start Notepad with Windows "start" program

Time:01-12

I'm trying to open a file with Notepad by using QProcess.
This is the command I'm trying to start:

start notepad   file.txt

If I run this command on the terminal, everything works fine, but if I use QProcess, the signal errorOccured() is emitted with error FailedToStart.

This is my code:

  QProcess* process = new QProcess;
  QObject::connect(process, &QProcess::errorOccurred, this, [process](QProcess::ProcessError e)
  {
    qDebug() << process->errorString();
    qDebug() << e;
  });
  QObject::connect(process, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), this, [process](int exitcode, QProcess::ExitStatus)
  {
    if(exitcode != 0)
      qDebug() << process->readAllStandardError();

    process->deleteLater();
  });

  process->start("start", { "notepad  ", "file.txt" });

What am I doing wrong?

CodePudding user response:

There is no application named start.exe, it only exists as an internal command inside cmd.exe.

To emulate start, use ShellExecute(0, 0, TEXT("Notepad "), TEXT("c:\myfile.ext"), 0, SW_SHOW);.

If you absolutely need to use QProcess you would have to execute cmd.exe /C start "Notepad " "c:\myfile.ext".

  • Related