Shell script execution - Error handling - c++

I am designing a gui application using qt creator. In there if i press a button, values from a .csv file gets imported to an sqlite3 database table.
For doing this I have used the QProcess to launch a shell script that contains the commands for importing data.
void MainWindow::on_pushButton_clicked()
{
QProcess process;
process.startDetached("/bin/sh", QStringList()<< "/home/aj/myscript.sh");
}
and myscript.sh for importing is--
echo -e '.separator ","\n.import import_table.csv test_table' | sqlite3 testdatabase.db
the reason behind this approach is that dot-commands can not be integrated directly to the c++ gui program without creating custom parsing solutions.
Now, this approach (QProcess and shell script) works perfectly well for me. But one bottle neck is it does not offer any error-handling/identification.
For example if the import command stops midway during data retrieval or all the values in .csv are not imported I would not know the problem. Is there any way to know if problem has occurred during importing ??? (redirecting the message via QDebug???)

QProcess has a member function called exitCode() which oddly enough returns the exit code of the process.
http://qt-project.org/doc/qt-5/qprocess.html
Finding that answer required a 10-second search in google for the documentation...

Related

Interactive Command Line Interface for Qt

I'm looking to create a cross-platform readline/linenoise type command line interface for an application in Qt. I'd rather not reinvent the wheel if possible. The terminal in which the user is operating should be able to receive commands via a command prompt. This will be done in Qt, thus my question is: is there a Qt-esque way of doing this via signals and slots such that when a user enters a line into the terminal a slot can be called?
I understand that this can be fairly simply done using a QThread and running a blocking process to signal on a line being read. This question is specifically geared towards using built-in Qt functionality.
While attempting to discern a viable answer to this, I realized that my question was improperly phrased. I was looking for what could be termed a Terminal Emulator. There are several existing solutions to this, including QTermWidget. However, it appears that there does not yet exist a non-UI based terminal emulator for Qt. That is, in order to use these one must have an active UI instance of Qt, not merely employing a Qt Command Line application.
Going the route of using a simple Qt Command Line, it is not as simple as connecting something like a QTextStream and observing the output of the command line. As with most command line programs, native, low-level processing must occur in order to assess things like the arrow keys. Unfortunately QKeyPressEvent and the like require the UI to be running.

Command execution in tcl shell when python is called from the TCL shell

I'm writing a GUI utility using tkinter and I'm calling the python script from a TCL shell of a unix software. Now, I'm able to create the GUI as I want, but unable to execute any commands in the main TCL shell through the GUI. Is there any way we can realize it? Basically, I want to execute certain commands in the main TCL shell(from which python script was called), using the GUI created using Tkinter.
PS: I know the client-socket way to establish communication, wherein I write a socket.py and client.tcl, but the problem is that I need to keep on coming to the TCL shell to execute the client TCL which defeats the purpose of having a tkinter based GUI.
If I understood your question correctly, you would run:
catch { exec ifconfig } result
puts $result
For example, this would give you the result as output from the command 'ifconfig', which is a *nix os dependent command.

Read output from python script in C++ Qt app

I have a python script that outputs to the console every 10 seconds or so.
I want to run that script from my c++ code, and keep it running to continue getting updates until I close my app. If I use popen, I can get the output line by line, without having to wait for the script to "finish", but I cannot kill it when I close my app now.
Is there a way to read the output of a script as it is spit out, keeping the script running until I close my qt app?
Thanks!
QProcess allows you to call a external process from code. The documentation includes information on how to close it within the program. Also you can use the read/write channels for communication.
There are some examples in the documentation and method descriptions, for getting started.

Attach a terminal to created thread, in C++ on Linux

I'm developing a chat server in C++. The programme is built and run from a terminal. As running in the terminal, I can write to this terminal normally using 'printf'. Some of the information written to this terminal are the alerts of new incoming connections, outgoing connections, etc.
Now I need to get the keyboard input so that admin can type commands to see the values of variables in the chat server. I intend to create a new thread and attach a new terminal to it. A suggestion is to call system("gnome-terminal"), but it requires a little delay with sleep(), doesn't seem to be a good choice because all the contents redirected to this gnome-terminal will be considered as bash commands. And I don't know how to attach the terminal opened by 'system'` command to the thread.
Any simple way to attach a terminal to created thread?
Maybe have a read of this on how to use pipes in Linux
http://linuxprograms.wordpress.com/tag/pipes/
As partially answered in this question: Avoid gnome-terminal close after script execution?
There is a good option like this:
(1) Use the main terminal for normal input/ouput.
(2) Create log file (log.file) before calling 'tail'
(3) Use 'tail' command for showing log contents (log files)
//c++ code
system("gnome-terminal -e \"bash -c 'tail -f log.file'\"");
(4) Append the content to 'log.file' to tell 'tail' to show it up.

Ahk script and C++ communication

I wish to use the functions of autohotkey within a C++ program.
I am currently running my scripts triggered by the c++ program- I just run them as a .bat file. This works well but the problem is that I cannot return values from the script to the c++ program.
I wish to be able to read the position of the mouse from the script and make decisions based upon this in my C++ program. My scripts do quite complex things- so doing this in autohotkey is the best solution for me- I have knowledge of C, but little of C++.
I have read about the Autohotkey .DLL - I know how to trigger it but not how to read values from it. If anyone could instruct me or even post example code of a .dll being loaded and a value sent to a script and a value returned- I would be eternally grateful!!
I have spent hours on this and to no avail!
to return a value, could this possibly work http://www.autohotkey.net/~tinku99/ahkdll/functions/ahkgetvar.htm
I'm not sure about the dll, but you could just write your own application in Autohotkey and package it along with your C++.
The communication takes place over a hidden window with an edit control and a button. You use one application to set text in the edit box, and then to click the submit button. The other application owns the window can process whatever is put into the edit control - as if you were passing a variable. Basically, that's it.
Check out this thread where I've explained it in more detail: How to send a command to a running application via commandline
Now, that's not quite what you wanted, but the effect is the same, and you already know all the api.