PDA

View Full Version : Display an output buffer (text stream) in QWidget



nomiz
29th June 2011, 16:45
Hi all,

1. From my program I run a thread (QThread).
2. When the thread runs it issues an API for a program called CPLEX (sort of solver).
3. CPLEX is executed and writes info to the terminal as it runs, just like qDebug() does.

Through the API, I can give CPLEX an alternate output buffer. Can I use QTextBlock or QTextEdit to display the contents of the alternate output buffer as CPLEX writes to it? How would I implement this?

Thank you very much!

mcosta
29th June 2011, 17:24
I think it depends from CPLEX API.

nomiz
29th June 2011, 17:52
How would you display an arbitrary buffer in a QWidget of some sort, while this buffer is written to from another thread?

stampede
29th June 2011, 17:56
When the thread runs it issues an API for a program called CPLEX (sort of solver).
If you launch the program using QProcess you can read its output channels and use signals that will notify you if new output from the program is ready. You can then forward the signals up to append(const QString&) slot in text edit (or another mehod that will display new text).

nomiz
29th June 2011, 18:20
So I need a QProcess. Can I then still used shared memory, signals and slots, etc. ?

stampede
29th June 2011, 18:34
I don't know if you need a QProcess, this is just a suggestion, suitable for the situation when you need to run external program as separate process and communicate with it.
Check out the QProcess documentation for details.
Maybe I just got it wrong, but this sentence: "it issues an API for a program called CPLEX " suggests that you run an external application.

nomiz
29th June 2011, 18:43
Aha, no this is not the case. It's like this:
First everything (including the CPLEX implementation) used the same thread.
Then I moved the solving part to a separate thread (so I could still use the rest of the program).

It is possible to create a new buffer and hand it over to CPLEX (using the API). But I would like to read it out in real time (using a QWidget).

What would you suggest?

Thanks!

stampede
29th June 2011, 18:54
In that case, a simple signal emitted from the worker thread will do the job.
Declare a void message(const QString& text) signal in your worker thread, and each time you want to send new message call emit message(message text);, connect this signal to a slot for display in your main widget ( for example append(const QString&) in QTextEdit ) and its done. You don't have to worry about thread-safety in that case, Qt will use queued connections for your signals automatically.

nomiz
30th June 2011, 10:44
I'll give it a try, thank very much for your help.