Reading continuous from Console in qt5
-
Dear all;
I need to read from console in qt5. Basically I need to implement it behind the push-button when I will press the button My QT application start reading when I need to stop it then it should be stop. Any help please guide me in right way how it is possible?Thanks in Advance!
-
@Wasee See https://doc.qt.io/qt-5/qtextstream.html
QTextStream stream(stdin); QString line; while (stream.readLineInto(&line)) { ... }
But keep in mind: this is blocking the event loop!
You can put this code into a thread to not to block main thread (https://doc.qt.io/qt-5/qthread.html). -
@ChrisW67 Hi;
Yes I am printing messages in console which is reading from hardware. Now i want to reading printed messages in my console from hardware which basically are the MAC address of wifi devices present in my building. Now I want to read these MAC address in qt from console.
Thanks -
@Wasee said in Reading continuous from Console in qt5:
will read from console output
It can't.
I can only guess what you mean here. If you have some device which is outputting messages which you see in e.g. the Application Output pane while running your Qt application in the debugger, that would indicate the device is sending to either standard output or standard error. If you wish to capture those messages in your app, you would need it to redirect stdout/err from within the app so that they arrive into the program instead of going to the console. But I do not know if that is the situation or what you mean.
-
@Wasee said in Reading continuous from Console in qt5:
Thanks for your help! My GUI will run in start up of the system. After running up my system I will press the push button in my GUI, after that my GUI will read from console output. How it is possible in easy way?
Your application has 3 standard I/O:
- stdin: console input (read only)
- stdout: console output (write only)
- stderr: error output (write only)
So you can use
QTextStream
orQDataStream
with stdin and check if there is something to read (cf. post from @jsulm) and write to stdout.Here a mean less code example:
int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); QTextStream in(stdin); QTextStream out(stdout); QTimer tmr; tmr.setSingleshot(false); tmr.setInterval(500); QObject::connect(&tmr, &QTimer::timeout, [&in, &out]() { if(!in.atEnd()) { const auto data = in.readAll(); out << "recieved: " << data; } }); tmr.start(); return app.exec(); }
-
Hi,
Do you mean you want to read the output of some application that returns the information you want ?
-
@SGaist Hi;
Thanks for your reply! I design the control of a system in qtcreator. My system returns the acknowledgement to my GUI. Now I want to add new feature in my GUI application, for which I need to read the output of my system which is currently I am seeing in serial monitor putty. Now I need to read this serial monitor output in my GUI behind the button when I needed.
Thanks -
@Wasee said in Reading continuous from Console in qt5:
read the output of my system which is currently I am seeing in serial monitor putty
Yre you talking about reading from a serial port?
Please explain clearly what you want to do. -
@Wasee
I'm afraid this does not make it any clearer.I design the control of a system in qtcreator.
We understand this. You are designing a Qt application there.
My system returns the acknowledgement to my GUI.
We do not know what this actually means. Is your system running as a library linked into your application's code, or is it running as a separate, external process, perhaps invoked via Qt's
QProcess
?read the output of my system which is currently I am seeing in serial monitor putty. Now I need to read this serial monitor output in my GUI
So "serial monitor putty" means running the
putty
as an external command, to read what is on the serial port and output it on stdout/err to the terminal? And if that is so, do you need to useputty
at all from your Qt application, would you be happy/better reading from that serial port with Qt serial code?We need to understand clearly whether you mean you have to have your application read the output from another process (
putty
), which seems to be what you say, or whether you mean your application can read the output directly, from the serial port, which seems simpler and preferable?One thing you cannot do, if this is what you have in mind, is: run the
putty
process from your Qt app, have that come up in its own terminal/console window so you can see it, have it send its output there like you are used to seeing, and then at the same time have your Qt application capture the output you see into your Qt application for further processing. You can, however, have your application receive the output --- whether directly or via redirecting theputty
output --- and choose to show it in its own window if you want to see it too. However, as said earlier, it sounds to me like you do not needputty
at all if your own application does the talking to the serial port directly. -
@Wasee said in Reading continuous from Console in qt5:
save terminal output to .txt file
Like I said, you cannot. Once output has gone to a terminal, say via an external process's output, it has gone there, and cannot be recaptured by your Qt application. That's what we are trying to tell you.
Which is why we asking you what is going on. If you are trying to read from and write to a serial port, do not use
putty
or anything else, do it directly from your Qt program. Then you can do what you like with the output received, including saving it to a text file. Why do you need to runputty
at all?Have you seen that Qt already has Qt Serial Port?
-
@JonB Hi;
I am just using putty to check output of my system at serial port not need it anymore. My system giving me MAC address at serial port continuously that the reason I want to read it from serial port or to store them in Qfile and read it back in my qt application that's all. -
Then why not just use QSerialPort to read the serial port directly ?