0

I am making an application that adds a new user account on Windows, in Qt Creator. The application asks for a username and password, then opens the cmd and enters the command net user username password /all. I want to be able to make a label that is always equal to what the cmd returns after the command has been input, for example "This account already exists". My code is below.

#include <QDebug>  
#include "mainwindow.h"  
#include "ui_mainwindow.h"  
#include "QProcess"  
#include "QTextStream"  
#include <iostream>  

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

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

void MainWindow::on_pushButton_clicked()  
{  
   QProcess process1;  
   QStringList arguments1;  

   arguments1 << "net user" << ui->lineEdit->text() << ui->lineEdit_2->text() << "/add";  
   QString path = "C:/Windows/system32/WindowsPowerShell/v1.0/powershell.exe";

   process1.execute(path, arguments1);  

   QString output(process1.readAllStandardOutput());  

   ui->label_4->setText(output);  
}
  • 1
    QProcess has members to get the output and pass the input. – drescherjm Nov 30 '17 at 15:34
  • So what would that look like in code? I'm new to Qt so I don't fully understand – HamishDewar Nov 30 '17 at 15:42
  • 1
    See QProcess::readAllStandardOutput() in the documentation. http://doc.qt.io/qt-5/qprocess.html#readAllStandardOutput – drescherjm Nov 30 '17 at 15:53
  • I edited my code as you suggested, and I received this in error the application output "QIODevice::read (QProcess): device not open" – HamishDewar Nov 30 '17 at 17:20
  • There is another sample I provided for [SO: Creating QT Application as GUI for existing console-based application on windows](https://stackoverflow.com/a/45209503/7478597). It might be of interest. – Scheff's Cat Nov 30 '17 at 18:04
  • I'm not quite sure but IMHO the reason why the `process1.readAllStandardOutput()` does not work is: You call the program with [`QProcess::execute()`](http://doc.qt.io/qt-5/qprocess.html#execute). It's a **`static`** method and ignores the `process1` completely. (You could've called `QProcess::execute(path, arguments1);` as well with exactly same behavior.) In my sample, I use `QProcess::start()` instead. This allows _concurrent_ execution of child process and your Qt process and standard in/out/error are redirected from/to the `QProcess` instance for which `start()` is called. – Scheff's Cat Nov 30 '17 at 18:12
  • I changed the process1.execute(path, arguments1); to process1.start(path, arguments1); and now when pressing the button I receive the error "QProcess: Destroyed while process ("C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe") is still running." – HamishDewar Nov 30 '17 at 18:49
  • Try adding: process1.QProcess::waitForFinished(-1); before you read the output. – drescherjm Nov 30 '17 at 19:29
  • https://stackoverflow.com/questions/17344807/read-qprocess-output-to-string – drescherjm Nov 30 '17 at 19:30
  • Oh my god, after putting in process1.QProcess::waitForFinished(-1); it works! Thanks so much everyone for the help – HamishDewar Nov 30 '17 at 19:38
  • Possible duplicate of [read QProcess output to string](https://stackoverflow.com/questions/17344807/read-qprocess-output-to-string) – drescherjm Nov 30 '17 at 22:17
  • Sorry, it should have been `process1.waitForFinished(-1);` instead. No need for the `QProcess::` part that was a copy / paste error that I made when copying from the documentation. – drescherjm Nov 30 '17 at 22:20

1 Answers1

0
process1.QProcess::waitForFinished(-1);
QString output(process1.readAllStandardOutput());
ui->label_4->setText(output);