Button running shell script

Asked

Viewed 432 times

2

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

This process I currently do with the following terminal script:

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 I passed to the script.

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

  • 1

    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í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.

  • 1

    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, 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 do rascunho "foo" and that’s it. Then I just do one cade "foo" to call the grep in the draft.

  • 2

    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, 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.

Show 1 more comment

1 answer

4

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.

  • Ual... elaborate solution, much more complex than I imagined. But since I started today trying to learn about Qt, I have no idea what to do with your code. Sorry, but could you comment here where save your code? It would be in the same ui?

  • Boy, what a lot! Thanks for the first lesson of Qt. For a mathematician, it’s too much information in one day... I’m in the note now and downloading the 5.3. I didn’t even know I needed those other files. In fact, on the other micro, I opened Qtdesign and saw that there was a chance that I could make a window into my shell script. I started to draw the objects but had no idea how to make it become a program. Thanks. I’ll try to learn.

  • Cool! Good luck in school! m/

  • Oops. thanks. I’m following an example from Help to make a Findtext. Do you have a little free time to help me with the example? If you can, we can use the chat.

  • I already go to sleep, because I work tomorrow, but add me on Facebook and what I can help I help: Vinicius Gobbo Antunes de Oliveira.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.