线程类Demo:

InvokeThread::InvokeThread(QPlainTextEdit* edit, QObject *parent) : QThread(parent), m_edit(edit)
{

}

void InvokeThread::run()
{
    QString str = "";
    for (int i = 0; i < 10; ++i) {
        str = QString("Subthread Id: %1, Time:%2").arg((unsigned)QThread::currentThreadId())
                .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss zzz"));

        if (m_edit)
            // 第一个参数是控件对象的指针,第二个是控件的方法名称必须写对否则不会成功,该函数还有其他多种重载形式
            QMetaObject::invokeMethod(m_edit, "appendPlainText", Q_ARG(QString, str));
        QThread::msleep(500);
    }
}

在GUI所在线程,比如主窗口中创建并开启线程,传递需要刷新的控件的指针

InvokeThread* thd = new InvokeThread(ui->plainTextEdit, this);
thd->start();

测试效果如下:

Qt子线程中通过QMetaObject::invokeMethod刷新UI控件

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-02
  • 2022-12-23
  • 2021-08-05
猜你喜欢
  • 2022-12-23
  • 2021-11-10
  • 2021-04-24
  • 2022-12-23
  • 2022-02-28
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案