"real time" update Qt TextView

I have a Qt application with built-in script / jit. Now, I would like to get the output from the script in QTextEdit (more specific QPlainTextEdit). For this, callbacks come out. The problem I am facing is that everything I try to execute in TextEdit either lingers until the script ends or gets stuck in 2-3 seconds (and lingers until the script ends ) I tried to use signals and slots for updating, but also direct function calls - it did not work. Also redrawing / updating TextEdit and the parent form, as well as even QCoreApplication :: flush (), really had little / no effect. Looks like I'm doing something fundamentally wrong. Any ideas or examples on how to get a real-time update?

Btw, the update procedure is called - debug output in stdout is possible in real time.

+4
source share
2 answers

Just to create a cut using threads, which I used many times to log and which works as desired:

Define a stream class:

class MyThread : public QThread { Q_OBJECT public: MyThread(QObject *parent=0) : QThread(parent) {} signals: void signalLogMessage(const QString &logMessage); ... }; 

Whenever you want the log message to appear in the main thread, just use

emit signalLogMessage("Foo!");

In the main topic:

 MyThread *thread = new MyThread(this); connect(thread, SIGNAL(signalLogMessage(const QString&)), this, SLOT(logMessageFromThread(const QString&))); connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); ... thread->start(); 

where logMessageFromThread does something like myPlainTextEdit->appendPlainText(message) . This works without any delays or other problems.

I hope this helps.

+1
source

Please let me answer the question myself (in parts). First of all, you need to understand that Qt itself is very much built around its own concept of signal and slot. Therefore, you cannot expect a real-time update of QTextView from the moment you add text to it (maybe with a text cursor or just add it), it simply triggers a signal. So, no matter what you do, when you have only one thread, all you do is trigger signals to update your widgets. Corresponding slots will be processed with a much lower priority, and, therefore, after the completion of the locking work cycle. All this can be eliminated by calling QCoreApplication :: processEvents (), as noted in Idan K.'s comments. This ensures that all unprocessed events are processed sequentially and returns later. With this function, QTextEdit can be used as a real-time output console. However, as Idan and Greg point out, the best solution uses a separate workflow that emits signals to the GUI stream. Since these are separate threads, the GUI can process the corresponding slots while the worker continues to work. Thus, the output in theory may be slightly delayed. to the above solution, but the whole application will remain responsive.

I also want to add that my problems with using QThread together with mono, where they are solved by creating a global application domain outside ththread and using mono_thread_attach (), as suggested. It works well on both Mac OS X and Windows 7.

+1
source

All Articles