I have a Qt application with an embedded script/jit. Now I'd like to receive the output from the script on an QTextEdit (more specific QPlainTextEdit). For this purpose callbacks are being issued. The problem I'm facing is that whatever I try the output to the TextEdit is either delayed until the script has finished or gets stuck after 2-3 seconds (and is delayed then until the script has finished).
I tried to use signals and slots for the update but also direct function calls - neither worked. Also repainting / updating the TextEdit and the parent form as well as even QCoreApplication::flush() did show little/no effect. Seems like I'm doing something fundamentally wrong. Any ideas or examples how to achive the "real time" updates?
Btw, the update routines are being called - debug output to stdout is avaiable in real time.
解决方案
Just to sketch a soluting using threads, which I have used numerous times for logging purposes and which works as desired:
Define your thread class:
class MyThread : public QThread
{
Q_OBJECT
public:
MyThread(QObject *parent=0) : QThread(parent) {}
signals:
void signalLogMessage(const QString &logMessage);
...
};
Whenever you want a log message to be shown in the main thread, just use
emit signalLogMessage("Foo!");
In your main thread:
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 delay or other problems.
I hope that helps.