“实时"更新一个Qt TextView [英] "real time" update a Qt TextView
问题描述
我有一个带有嵌入式脚本/jit的Qt应用程序.现在,我想从QTextEdit(更具体的QPlainTextEdit)上的脚本接收输出.为此,将发出回调.我面临的问题是,无论我尝试执行什么操作,TextEdit的输出要么延迟到脚本完成,要么在2-3秒后卡住(然后延迟,直到脚本完成). 我尝试使用信号和插槽进行更新,但也使用直接函数调用-均无效.同样,重新粉刷/更新TextEdit和父窗体,甚至QCoreApplication :: flush()都显示很少/没有效果.似乎我在做一些根本错误的事情.有什么想法或示例可以实现实时"更新吗?
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?
顺便说一句,正在调用更新例程-实时提供对stdout的调试输出.
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:
定义您的线程类:
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!");
在您的主线程中:
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();
其中logMessageFromThread
的作用类似于myPlainTextEdit->appendPlainText(message)
.
这样可以正常工作,没有任何延迟或其他问题.
where logMessageFromThread
does something like myPlainTextEdit->appendPlainText(message)
.
This works without any delay or other problems.
我希望能帮上忙.
这篇关于“实时"更新一个Qt TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!