Use the class QProcess
. Since you did not specify the version of Qt being used, I am assuming version 5.3.
Check if the first line of the script is #!/bin/bash
for Linux to decide automagically how to run the file, and modify the code below to better serve it:
QString program = "/path/to/the/script.sh"; // caminho do script.
QStringList arguments;
arguments << this->ui->texto->text(); // argumentos passados para o script.
QProcess *myProcess = new QProcess(this);
myProcess->start(program, arguments); // executa o script.
myProcess->waitForFinished(-1); // aguarda a finalização do processo.
delete myProcess; // nada de memory leaks!
Qt documentation is excellent. I recommend reading about the class QProcess
to make the most of it:
Qt 5.3 Qprocess Class
More details about Qt
Qt is a framework sometimes classified as operating system abstraction. This comes from the fact that it not only abstracts windows and buttons, but allows you to perform several low-level tasks with full portability between systems.
The look is just one of Qt’s many strengths, and it’s a bit scary when it comes from other worlds like. Net or Delphi (which was my case hehehehe).
In Qt, files with extension .ui
They are nothing more than an XML file that describes your form, so that when your application runs the same form created during the design is created by Qt in the execution, saving even the connections between objects. Anyway, the file .ui
is nothing more than this detailed description of the graphical interface.
However, despite connecting the various components used in the form, the function of this file ends there. For example, when a button is clicked, the file .ui
says that a method must be executed, but the method implementation is not in the file .ui
.
I believe you are developing in C++ with Qtcreator (if you are not, I strongly recommend it!). Note that in addition to the file with extension .ui
, there are two other files with the extension .h
and .cpp
. Now, I’m going to assume you know the basics of C programming++.
Your project, then, should have the following files (I run the risk of my memory crashing as I usually do not implement my interfaces inside the file .ui
):
main.cpp
MainWindow.ui
MainWindow.h
MainWindow.cpp
main.cpp
Just start your application by creating its main form and running the event loop. Normally, let’s not touch this file, but if you want to create a splash screen or start the maximized form, we will have to change some things in it (it is not the goal of the issue so I will not detail further).
Mainwindow. h
Has the declaration of the Signals, slots and other members of your form. These are responsible for generating the desired behavior of your form.
Mainwindow.cpp
Implementation of methods and slots (still methods) declared in the file .h
.
Now that I have detailed a little how Qt works, let’s go to implement the execution of the script in your form.
To be clear, what we want is to execute this code when a button is clicked on its interface.
For this, we need to define a behavior for when the button is clicked. This is done by connecting a slot
at the sign clicked
button. If you are using Qtcreator, just right-click on the desired button, select the option Go to slot
and select the slot clicked()
.
When doing this, note that a method has been declared in the file MainWindow.h
and declared the implementation of this method in the archive MainWindow.cpp
. Now, we just need to add the code to run the shell script in the method that was created within the file MainWindow.cpp
.
You’ll have something like this:
void MainWindow::pushButton1_clicked() {
}
Within this method you will enter the code I posted earlier:
void MainWindow::pushButton1_clicked() {
QString program = "/path/to/the/script.sh"; // caminho do script.
QStringList arguments;
arguments << this->ui->texto->text(); // argumentos passados para o script.
QProcess *myProcess = new QProcess(this);
myProcess->start(program, arguments); // executa o script.
myProcess->waitForFinished(-1); // aguarda a finalização do processo.
delete myProcess; // nada de memory leaks!
}
Now, just replace the script path in the marked location, check if the QLineEdit
with the text to be inserted into the file is actually the ui->texto
, compile and rotate!
I tried to be as brief as possible, but covering the main points needed to accomplish this task in Qt. About the class QProcess
check the documentation. It may seem a little rude to recommend this, but is that the code is quite straightforward, and Qt is undoubtedly one of the frameworks with the best documentation (in my opinion, better than . Net if we disregard the examples).
Any doubt I’m at your disposal!
I hope I’ve helped.
Please, in your next question, confine yourself to what matters in your question. For example, it was not necessary to post the file
.ui
, since your doubt is about running processes, and not user interface. The smaller and more objective the questions are, the easier and faster some collaborator find the correct answer.– Vinícius Gobbo A. de Oliveira
@Viníciusgobboa.deOliveira, thank you for the remark. But as I have no idea of the subject, I did not know if there were few or many of my information. So I chose to put everything. Thank you.
– Sigur
But if it is to append a file, it is not better to do it directly with C++ instead of calling a shell script?
– bfavaretto
@bfavaretto, sorry but I have no knowledge to decide what is best. What I want is to try to make a GUI for my famous script
rascunho
that stores everything in the text file. Whenever I need to save anything, I dorascunho "foo"
and that’s it. Then I just do onecade "foo"
to call thegrep
in the draft.– Sigur
Okay, I get it. If the whole program was in C++ it would make more sense to use the language itself for such a simple operation. I say that without knowing C++, but writing to a file is a basic operation. If you’re going to take your project further, consider migrating all the code to C++.
– bfavaretto
@bfavaretto, actually, I tried Qt because I thought that’s how Gnome programs were created. I just opened Qtcreator and have no idea how to use it.
– Sigur