QT Creator QProcess - c++

I want to run a fortran executable that is called when I click in push button in the interface that I created.
Although when I click in the button nothing happens. Here is the code:
QProcess *process = new QProcess(this);
QString program = QDir::currentPath() + "/PARROT/Console1.exe";
process->start(program);
The string is in that way because I want to be capable of changing the path to the main executable and the fortran executable.
What I'm doing wrong?

Check this link - QProcess Start command syntax
syntax - void QProcess::start(const QString & program, const QStringList & arguments, OpenMode mode = ReadWrite)
You need to pass the argumentlist as 2nd parameter,along with the process path as 1st parameter, needed to start the process.

Related

How to run Python script from QT creator and print output to GUI

void MainWindow::on_pushButton_clicked()
{
QProcess p;
// get values from ini file
settings->setValue("EMail", ui->lineEditEMail->text());
settings->setValue("Password", ui->lineEditPassword->text());
settings->setValue("Chronological", ui->checkBox->isChecked());
settings->setValue("Current_info", ui->checkBox_2->isChecked());
settings->endGroup();
settings->sync();
// launch python code for login
QString program( "C:/projects/build-test3-Desktop_Qt_6_4_0_MinGW_64_bit-Debug/venv/Scripts/python.exe");
QStringList args = QStringList() << "index.py";
QProcess::execute( program, args );
}
I have this function that is executed after a button is clicked and I need to print the output of "index.py" in to my app. What widget should I use and how? From what I read QTextBrowser should do the trick but I'm not sure how to use it.
This is how my GUI looks like. I'd like to use to output my results somewhere in button right. I didn't add the widget yet, because I'm not sure QTextBrowser is the one I need
The widget you could use for this purpose is QTextEdit (you can set it to be read-only from the graphical user interface).
But if you want to get the output of the execution, you will need a proper instance of QProcess and call the QProcess::readAllStandardOutput() member function to get the standard output.
You may also be interested by QProcess::readAllStandardError() to get the errors in case of failure.
Edit (simple/basic example):
QProcess p;
p.start("path/to/python.exe", QStringList("script.py"));
p.waitForFinished();
QByteArray p_stdout = p.readAllStandardOutput();
QByteArray p_stderr = p.readAllStandardError();
// Do whatever you want with the results (check if they are not empty, print them, fill your QTextEdit contents, etc...)
Note: If you don't want to be blocking with QProcess::waitForFinished(), you can use a signal/slots connection on QProcess::finished() signal.

Why can't I open another process in Qt framework with this function?

In the Qt framework we're supposed to be able to open another .exe using QProcess. The following doesn't work when I click a button and the callback is called:
void MainWindow::on_pushButton_clicked()
{
QProcess *process = new QProcess(this);
QString wordPath = "C:/Program Files/Internet Explorer/iexplore.exe";
process->start(wordPath);
}
However if I change process->start(wordPath) to:
process->start(wordPath, QStringList());
Which is an overload of the same function, it works. The second parameter is supposed to be the arguments passed to the new process you want to start. The only way I can make the single-argument version work it seems is if something is in my PATH variable, because both "explorer.exe" and "msconfig" work. What is the story behind this only working with the second QStringList(), which is just an empty list?
In another SO question I saw a user specifically add an empty string, like this:
QString wordPath = "C:\\Program Files\\Microsoft Office\\Office12\\WINWORD.EXE"
process->start(wordPath, QStringList() << "");
I would love to know what the reason is behind this.
Your path "C:/Program Files/Internet Explorer/iexplore.exe" contains spaces, so when you use the first version the program is interpreted as being "C:/Program" with arguments "Files/Internet" and "Explorer/iexplore.exe".
The second version treats all of the first argument as the program and the QStringList as the arguments.
From the Qt documentation:
Arguments containing spaces must be quoted to be correctly supplied to the new process.
Try
QString wordPath = "\"C:/Program Files/Internet Explorer/iexplore.exe\"";

Redirecting the output of QProcess when running a resource file

fairly new to Qt.
I'm using QProcess to run an external shell script and redirecting the output to a textBrowser on my GUI. Code:
In mainwindow.h:
private:
QProcess *myProcess;
and mainwindow.cpp:
void MainWindow::onButtonPressed(){
myProcess = new QProcess(this);
myProcess->connect(myProcess, SIGNAL(readyRead()), this, SLOT(textAppend()));
myProcess->start("./someScript.sh", arguments);
}
void MainWindow::textAppend(){
ui->textBrowser->append(myProcess->readAll());
}
This works perfectly to run an external script. My question is how to apply the same process with the script included as a resource file.
I've tried simply replacing "./someScript.sh" with the resource version ":/someScript.sh" but it does not seem to work.
The resource script runs perfectly, but the console output disappears.
For this reason, there is something called "QTemporaryFile" class.
Because you need to call a file that already exists in your system - ok!
let's take this example :
using QProcess we need to run a python file from resource
//[1] Get Python File From Resource
QFile RsFile(":/send.py");
//[2] Create a Temporary File
QTemporaryFile *NewTempFile = QTemporaryFile::createNativeFile(RsFile);
//[3] Get The Path of Temporary File
QStringList arg;
arg << NewTempFile->fileName();
//[4] Call Process
QProcess *myProcess = new QProcess(this);
myProcess->start("python", arg);
//[5] When You Finish, remove the temporary file
NewTempFile->remove();
Note : on windows, the Temporary File stored in %TEMP% directory
and for more informations you can visit Qt Documentation - QTemporaryFile Class
Good Luck ♥
I does not work because when you run myProcess->start(":/someScript.sh", arguments); you ask your system to run :/someScript.sh which does not exist for your system.
A quick solution would be to copy the script to a temporary folder and run it from there.
QFile::copy(":/someScript.sh", pathToTmpFile);
myProcess->start(pathToTmpFile, arguments);
I would also suggest you make use of QTemporaryFile to get a unique temporary file name.

How to start a process in Windows using QT?

I'm trying to start a console application on Windows using QProcess's method 'start'. Official documentation says I can do it like this:
QProcess process;
process.start("C:/Windows/System32/cmd.exe");
I expect that a standard greeting message will appear in the calling application's console, but this does not happen, though the called process is running. What is wrong here?
use of bellow example :
QProcess *process = new QProcess(this);
QString program = "explorer.exe";
QString folder = "C:\";
process->start(program, QStringList() << folder);
also you can use of system() as bellow :
system("C:/Windows/System32/cmd.exe");
How about this static call?
QProcess::startDetached(FilePath,Arguments,StartInDir);
No need to create any objects!

My Qt code doesn't compile with "cannot call member function without object"

So i'm struggling to find a solution to this and don't know where i'm going wrong. I'm new to QT (today) and i'm not sure if i'm doing the right thing.
I'm trying to create a GUI for an already created c program (an image scraper). The image scraper works but I am trying to implement the GUI which allows a user to input a website to scrape images from into a line edit box (lineEdit), and then on click of a push button (pushButton) it takes the input text from the line edit box and uses it as the argument to run the C program in the background. Except I can't get that far because of the issue mentioned above.
Any assistance will be appreciated. Below is my code, the header and main files haven't been changed, and any changes that have been made have been done through the GUI designer over manual changes.
#include "mainwindow.h"
#include "ui_mainwindow.h"
char *arguments;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QString program = "~/Desktop/IS";
QString arguments = QLineEdit::text(); //error on this line
QProcess *myProcess = newQProcess(parent);
myProcess->start(program, arguments);
}
All errors are in on_pushButton_clicked().
A space is missing between new and QProcess.
There's no parent variable in scope. There is, of course, a parent() member of QObject. You can simply parent the process to the window itself.
You can't call QLineEdit::text without an object, as the error says. Only you know what object you need. Let's pretend for now that the object is ui->myLineEdit.
The tilde expansion is done by the shell. The kernel has no idea what a tilde is, neither does a QProcess. You need to provide full path to the executable.
The home directory is not always available from the HOME environment variable either. It should be obtained from the portable QDir::homePath().
QProcess::start() does not take two strings. It needs a list of strings as the second parameter. Since you only intend to provide one argument, it's a simple matter to wrap it up in a string list.
void MainWindow::on_pushButton_clicked()
{
QString program = QDir::homePath() + "/Desktop/IS";
QProcess *myProcess = new QProcess(this);
myProcess->start(program, QStringList(ui->myLineEdit->text()));
// The variant above is slightly shorter then the equivalent line below:
myProcess->start(program, QStringList() << ui->myLineEdit->text());
}
On your MainWindow Form just create a lineEdit somewhere called 'InputWebSite'
and then replace
QString arguments = QLineEdit::text();
With
QString arguments = ui->InputWebSite->text();