skdadle
skdadle

Reputation: 165

showing cmd terminal in qt widgets application

I'm trying to pass some cmd commands using system() and I would like to be able to "communicate" with cmd, say I code in system("dir") in my mainwindow.cpp under my clicked function

this is what it looks like for example

void MainWindow::on_pushButton_login_clicked()
{
  std::string platform_server_ip  = ui->lineEdit_platform_server_ip->text().toStdString();   

  if (platform_server_ip == "dir"

    {
      QMessageBox::information(this,"Login", "all required log in details are correct");

     close();

     const char* c = platform_server_ip.c_str();
     system(c);
     system("ipconfig");


    }

I would like to know why it behaves like this and if that's normal. I've included CONFIG += console in my project file, and checked "run in terminal" (tried it also without) but it never shows me my desired outcome.

Instead what I get, is a blank terminal that pops up along side my GUI, and then when I enter "dir" in my GUI and hit enter, a cmd window pops up really fast and in less than a second, its gone. I've even tried it with system("ipconfig")andsystem ("pause") as well as with one system command like this system("ipconfig" "&pause")

desired outcome: is just a normal execution of system("ipconfig"), followed by other system commands, that display the same result as typing them in cmd itself.

I've also tried all this in "qt Console application" and I either get the same result, or the output (what would normally be as output on cmd) is then found in "application output" of qt creator.

Is there another better way I can achieve what I want? I'm truly a noob and would really appreciate some guidance.

Upvotes: 1

Views: 2972

Answers (2)

p-a-o-l-o
p-a-o-l-o

Reputation: 10079

You can try

system("cmd /k ipconfig");

This will open another terminal window which will stay open (k stands for keep) at the end of the command execution.

I think you don't need the CONFIG += console project setting, to achieve this. Calling system will start another process, which isn't related at all with the calling application.

If you want to start external programs from within a Qt application, you can use QProcess class, which lets you somehow interact with the started processes through standard in/out. For a very simple example, have a form with a push button and a text edit called textEdit; in the push button clicked slot:

QProcess process;
process.start("ipconfig");
process.waitForReadyRead();
ui->textEdit->setText(process.readAll());
process.waitForFinished();

This way, you won't see additional console windows, and the command output will be shown directly in your text edit.

This can be generalized in a function like this:

bool exec(QString command)
{
  QProcess process;
  process.start(command);
  if(!process.waitForStarted())
  {
    return false; //the process failed to start
  }

  //etc...

  return true;
}

Upvotes: 1

Moeren
Moeren

Reputation: 179

Depending on whether this is not just a quick hack/tool, you can look at QProcess for more indepth control over your process so that you can read / write the child process pipes.

Upvotes: 0

Related Questions