PDA

View Full Version : Communicating with program for GUI input



ghaas123
28th February 2018, 22:24
Hello,

My question is in regards to if it is possible for QT to access a variable in a C++ program that is not included in the QTCreator IDE. The external project outputs a value to a text file. Reading from this text file and then using that value in the GUI is inefficient. My goal is to use QT to run a shell script to start this program. Then I would like to know if there is some way to incorporate the code containing this variable directly into the GUI. I was looking into using QProcess but saw this was for input and output streams. I am new to programming and this seemed rather complex. Is there a way to just read this value from the variable and use that value for the GUI or is there a way to use QProcess to do so and if so can you explain how that may work?
Thank you

high_flyer
1st March 2018, 11:07
As you say and as can be deduced from how you formulated your question, you are new to programming.
The task you want to achieve is by no means a beginner task.
What you are referring to as a use case for an IPC (Inter process communication) and there are various possibilities, which one to use depends on your needs.
No one of there options is simple for a beginner as you need to be familiar with various programming concepts.
A good place to start would be here:
https://en.wikipedia.org/wiki/Inter-process_communication
In addition there is DBus, and a Qt implementation of it QDBus:http://doc.qt.io/qt-5/qtdbus-index.html

d_stranz
1st March 2018, 16:56
if it is possible for QT to access a variable in a C++ program that is not included in the QTCreator IDE.

Do you have the source code to this C++ program? If you do, you might be able to adapt it so that it runs as part of a Qt GUI-based program. If not, the simplest option for you is probably to just read from the file after the external program has updated it.

ghaas123
1st March 2018, 19:25
The source code is a rather large project, I am unsure as to how to adapt it to a Qt GUI application as I did not write the external project, just the GUI. The way that the external program writes the output that I need to a text file is by redirecting the standard output to a file. Is there any QT Class and functions that you could point me towards that would allow me to read the standard output of the program into the QT GUI without changing the source code that is sending this output to a text file? Any help is greatly appreciated.

d_stranz
2nd March 2018, 17:13
You might be able to run the external program in a QProcess and redirect its stdout into stdin of your Qt program. See QProcess::setProcessChannelMode()


Here's some code from a stackoverflow accepted (https://stackoverflow.com/questions/2148185) answer to a similar question:



QProcess p;
p.start( /* whatever your command is, see the doc for param types */ );
p.waitForFinished(-1);

QString p_stdout = p.readAllStandardOutput();


This, however, blocks your Qt app while the external process is running. You could also connect slots to QProcess signals (finished(), readyReadStandardOutput) and not use waitForFinished. See this other stackoverflow answer (https://stackoverflow.com/questions/10098980).