Button executing shell script

2

By clicking the Save button, I wanted the contents of the text box (which I typed or pasted into plain text ) to be inserted at the end of a certain text file that exists in my home.

This process I currently do with the following script in the terminal:

folder=/mnt/dados/Dropbox
file=$folder/rascunho
content=$1
printf "\n\n" >> $file
echo "------< $(date "+%b %d, %Y - %H:%M:%S") >------" >> $file
echo "$1" >> $file

That is, always in the same file, inserting at the end of it a line with date and time and below the content that I passed pro script.

Now, I wanted to be able to type this content into the text area of the window and use the Save button to insert it into the file.

    
asked by anonymous 19.09.2014 / 00:08

1 answer

4

Use the QProcess class. Since you did not specify the version of Qt being used, I'm assuming version 5.3.

Make sure that the first line of the script is #!/bin/bash so that Linux decides how to execute the file, and modify the code below to better serve you:

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!

The Qt documentation is excellent. I recommend reading about the QProcess class to get the most out of it:

Qt 5.3 QProcess Class

More details on Qt

Qt is a framework sometimes classified as an operating system abstraction. This comes from the fact that it not only abstracts windows and buttons, but also allows you to perform various low-level tasks with complete portability between systems.

The visual 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 hehehe).

In Qt, files with .ui extension are nothing more than an XML file that describes your form, so that when your application is executed the same form created during design is created by Qt in execution, , the connections between objects. Anyway, the .ui file 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 .ui file says that a method should be executed, but the method implementation does not stay in the .ui file.

I believe you are developing in C ++ with QtCreator (if you are not, I highly recommend!). Note that in addition to the file with .ui extension, there are two other files with the extension .h and .cpp . Now, I'll assume you know the basics of C ++ programming.

Your project should then have the following files (I run the risk of my memory crashing because I usually do not implement my interfaces inside the .ui file):

main.cpp
MainWindow.ui
MainWindow.h
MainWindow.cpp

main.cpp

Just start your application by creating the main form of the application and running the event loop. Normally, do not move in 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 purpose of the question so I will not detail it further).

MainWindow.h

It 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 (non-methods) declared in file .h .

Now that I've worked out a little bit about how Qt works, let's start implementing the script in your form.

To be clear, we want to execute this code when a button is clicked on your interface.

For this, we need to define a behavior for when the button is clicked. This is done by connecting a slot to the clicked sign of the button. If you are using QtCreator, just right click on the desired button, select the Go to slot option and select the clicked() slot.

When doing this, note that a method was declared in the MainWindow.h file and declared the implementation of this method in the MainWindow.cpp file. Now, we just need to add the code to execute the shell script on the method that was created inside the MainWindow.cpp file.

You'll get something like this:

void MainWindow::pushButton1_clicked() {
}

Within this method you will insert the code that I posted before:

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 that the QLineEdit with the text to be inserted in the file is actually ui->texto , compile and run!

I tried to be as brief as possible but covering the main points needed to perform this task in Qt. On the QProcess class check the documentation. It may seem a bit rude to recommend this, but the code is fairly straightforward, and Qt is arguably one of the best documentation frameworks (in my opinion, better until .Net if we disregard the examples). >

Any questions are available! I hope I have helped.

    
19.09.2014 / 00:38