0

I'm trying to do a qt widgets application and I've got a batch file: containing

@echo off

echo Hello Everyone!
echo ----------------
echo 1 - Exit Program
echo ----------------
echo 2 - Say Hi 5 times
echo ----------------
set /p QUESTION=What would you like to do today?:
echo:
IF %QUESTION%==1 GOTO :1
IF %QUESTION%==2 GOTO :2

:1
exit
:2
cls
echo HI
echo HI
echo HI
echo HI
echo HI

pause

and I would like to pass "2" as an argument to it when its launched using QProcess, like simultaneous with execution

this is my mainwindow.cpp file

MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
{
  ui->setupUi(this);

  auto proc = new QProcess();
  QString program = QString("\"%1%2\"").arg("C:/Users/firstname secondname/desktop/").arg("mybatchfile.bat");
  QStringList arguments;
      arguments << "2";

  proc->setWorkingDirectory("C:/Users/firstname secondname/desktop/");
  proc->start( program, arguments );
}

MainWindow::~MainWindow()
{
  delete ui;
}

what I expected is that it runs my batch and shows me hi 5 times but what I get is nothing, I don't get any errors but I don't get desired output

can someone please tell me why that is and how can I fix? I'm really a noob and any feedback would help

skdadle
  • 155
  • 1
  • 2
  • 17

2 Answers2

3

In case you want to start a program like notepad.exe, you should wait for the termination of the command with proc->waitForFinished();, otherwise your MainWindow will tear down your QProcess immediately after starting it.

Good practice would also be implementing error catching functions, e.g. readyReadStandardError().

Starting a batch file is not possible directly, as it is not an executable. It needs to be started via cmd.exe

cmd.exe is somewhat more tricky: cmd.exe can be started with or without console and as your mainwindow already has a ui, any slave cmd.exe started will have no console window.

Thus you have to start cmd.exe as independant process with proc->startDetached();.

So the complete thing has to look like this:

QProcess* proc = new QProcess();
QString program = "cmd.exe";
QStringList arguments;
arguments << "/c C:/Users/firstname secondname/desktop/mybatchfile.bat 2";

proc->startDetached( program, arguments );
delete proc;

Use a simplified batch to make sure you get an output:

 @echo %1%
 pause
Jens
  • 6,173
  • 2
  • 24
  • 43
  • thanks! I added this to my code!, the only thing it did was it just took a little longer to even show my mainwindow. is this expected behavior? my main problem is its not executing anything :( – skdadle Aug 06 '18 at 14:29
  • Edited my answer – Jens Aug 10 '18 at 18:52
  • thank you for the help! though i just used the code you provided and also no output.. no errors but also not the expected output. i just get my empty main window :/ also could you maybe tell me what the /c does? again thanks a lot! – skdadle Aug 13 '18 at 11:32
  • Can you just use a simplified batch file as suggested above? – Jens Aug 16 '18 at 11:20
  • Hi, i tried using that simplified batch file, and unfortunately its still the same, the only thing i get in application output is I:\build-path-Desktop_Qt_5_9_3_MinGW_32bit-Debug\debug\myapp.exe exited with code 0... do i need to have anything checked/turned on for it to work?, also could you please clarify what this: QString("%1").arg("cmd.exe"); mean in your code? Thanks a lot for the answer! – skdadle Aug 17 '18 at 07:55
  • Sorry - QString("%1").arg("cmd.exe"); can just be replaced by "cmd.exe", copied from your original code. Will edit my answer! – Jens Aug 17 '18 at 07:57
  • thanks for the edit and your help, however it still does not work for me unfortunately :c does it work for you? i'm gonna keep trying. thanks a lot! – skdadle Aug 17 '18 at 08:53
  • It does work for me, though my paths to the batch file are different, can you put the batch into something like c:/tmp to make sure, the batch gets actually called? You would need to modify the code, too. – Jens Aug 17 '18 at 11:54
0

You are actually setting the variable %1 if you pass the argument "2", so you need to check for that and otherwise as the question with set /p

set QUESTION=%1
GOTO check:
ask:
set /p QUESTION=What would you like to do today?:
check:
IF "%QUESTION%"=="" GOTO :ask

echo:
IF %QUESTION%==1 GOTO :1
IF %QUESTION%==2 GOTO :2

...

Or you could send some data to the process using stdin

proc->write("2\n");
Amfasis
  • 3,932
  • 2
  • 18
  • 27
  • thanks for taking the time, but I did not quite get what you mean.. if i was changing my question or making it like, conditional (if you know what i mean, my english isnt that great) then that was unintentional.. i want the question to be asked normally, should i add proc->write("2\n"); to my code or replace a line with it? again thanks a lot for replying – skdadle Aug 06 '18 at 13:19
  • thanks for making it a lot more understandable! but unfortunately it still did not execute like i want for me, i just get the empty main window and that's it :/ shouldnt the code launch cmd with hi 5 times? really appreciate the edit tho! thank you :D – skdadle Aug 06 '18 at 13:27