“实时"更新 Qt TextView

quot;real timequot; update a Qt TextView(“实时更新 Qt TextView)
本文介绍了“实时"更新 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?

顺便说一句,更新例程正在被调用 - 调试输出到标准输出是实时可用的.

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

发出 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)