I have designed a very simple qt application (UI widget) that displays 3 floating point values in text boxes. There is one main function (the likes of the QT beginner tutorial) doing all the processing and a QMainWindow class with 3 text boxes, that are set through setter methods in this QMainWindow; these methods are called from the main function and I simply pass the sensor readings. All it's supposed to do is keep updating the values on the UI as they change in the background process. This background process is a c++ project executable that deals with the sensor hardware, and is called by a QProcess object; this executable couts 3 numbers constantly at around 20 Hz (these are sensor readouts and they keep fluctuating). I have all the logic working correctly in the main function in the QApplication, and I can qDebug() and see that the proper values are being outputted.
My only problem is updating the value displayed in the UI widget. I started working on QT yesterday, so I'm new to it, and I read about slots and signals, but I don't really need event handling. I just need the textEdit values to get updated.
The actual value processing is done in a sensor-read-processing-loop; it's a infinite while loop that will keep spewing values from the executable until I escape the loop:
1) If I call the app.exec() in this while loop (which sounds like a really bad idea at first because I thought it'll keep creating new windows), nothing happens; the UI shows up, the values keep getting generated in the background but don't show up in the UI.
2) If I call app.exec() before the while loop, which is the proper way, no values show up in the UI, but keep getting generated in the background by the executable (similar to (1)). This is my real problem; how do I refresh the values in the UI AFTER calling the app.exec()?
3)If I call app.exec() after this loop, it will display only one set of values since I've already escaped the loop.
To that end I've read stuff on google about event handling using slots and signals (most forums recommend that). Although I don't really have a complicated application with multiple objects. I've also tried using pointers but after I got through all the runtime exceptions, the values still don't get updated. I won't be able to post the code here because the code is on an embedded device and it's not able to connect to the internet currently.
I'm using process.start(programPath, arguments)
to start the process, and p.waitForReadyRead()
and p.readLine()
to read data off the console output of the executable
Is there any simple think that'll let me do this. Thanks in advance
As a novice, it helps to forget about any and all methods named
waitFor...
. They block, and in a gui application this generally makes things not work. You don't need these methods, so don't use them.You need to react when new input is available from
QProcess
. Below is a simple stand-alone example for Qt 5 and C++11.If the application is invoked with any command line arguments, it acts as a generator to simulate your data source process. When invoked without arguments, it will start itself in the emulation mode, and display the user interface that reflects the incoming data in real time.
There are several points worth mentioning:
QCoreApplication::applicationFilePath()
is used to refer to the executable.readLine
, you must do it this way.QProcess
can indicate data in any chunks - there's no guarantee that any number of lines is available. WhenreadyRead
is emitted, you can see any number of lines, including zero! All thatreadyRead
means is that there's at least one byte of data to read. That's all.QObject
-derived class to provide the necessary slots.