在Delphi里我记得是使用TThread.Synchronize(TThreadMethod),原理是利用了一个隐藏窗口来处理。

在QT Debug模式一下,碰到了同样的问题,显示错误:

cannot send events to objects owned by a different thread

解决方案是使用信号槽,就是在线程里不断的发信号,UI线程的槽函数不断的接受信号并做处理:

So as a solution I would propose the following:

  • Define a signal in your PaintThread class
  • connect this signal to the paint() slot in QWidget subclass
  • Emit it in the run() function

参考:http://stackoverflow.com/questions/9018434/qthread-doesnt-work-well

或者使用:

int ThreadTest::showProgress(int percent, void *pContext)
{
// instead of: someOtherQObject->updateProgress(percent);
QMetaObject::invokeMethod(someOtherQObject, // obj
SLOT(updateProgress(int)), // member
Qt::QueuedConnection, // connection type
Q_ARG(int, percent)); // val1

//qDebug() << "Progress: " << QString::number(percent);
return 0;
}

https://forum.qt.io/topic/9119/solved-how-can-i-evade-the-error-cannot-send-events-to-objects-owned-by-a-different-thread/4

在非UI线程中更改UI(Delphi使用隐藏窗口来处理,QT使用信号槽)的更多相关文章

  1. Android开之在非UI线程中更新UI

    当在非UI线程中更新UI(程序界面)时会出现例如以下图所看到的的异常: 那怎样才干在非UI线程中更细UI呢? 方法有非常多种.在这里主要介绍三种: 第一种:调用主线程mHandler的post(Run ...

  2. 一个解决在非UI线程中访问UI 异常的小方法

    写 WPF 的童鞋可能都会碰到 在非UI线程中访问 UI 异常的问题.这是为了防止数据不一致做的安全限制. 子线程中更新UI还要交给主线程更新,引用满天飞,实在是麻烦. 接下来,我们推出一个可以称之为 ...

  3. 为什么在非UI线程中操作UI的改变失不安全的

    因为你如果允许在非UI线程更新操作UI的东西,那我再另一个非UI线程也可以更新这个Ui的东西 这样就会有冲突,比如你的线程刚好跑到修改UI这里,我的另一个UI也有可能跑到这里,所以这样导致线程不安全. ...

  4. UWP 在非UI线程中更新UI

    大家都知道,不可以在 其他线程访问 UI 线程,访问 UI 线程包括给 依赖属性设置值.读取依赖属性.调用方法(如果方法里面修改了依赖属性)等.一旦访问UI线程,那么就会报错,为了解决这个问题,需要使 ...

  5. Android在非UI线程中更新UI的方法

    1.使用Thread+Handler实现非UI线程更新UI界面 在UI Thread中创建Handler.用sendMessage(message)或者obtainMessage(result, ob ...

  6. Android利用Looper在子线程中改变UI

    MainActivity如下: package cn.testlooper; import android.app.Activity; import android.os.Bundle; import ...

  7. Android在子线程中更新UI(二)

    MainActivity如下: package cc.testui2; import android.os.Bundle; import android.view.View; import andro ...

  8. Android在子线程中更新UI(一)

    MainActivity如下: package cc.testui1; import android.os.Bundle; import android.os.Handler; import andr ...

  9. WPF非UI线程中调用App.Current.MainWindow.Dispatcher提示其他线程拥有此对象,无权使用。

    大家都知道在WPF中对非UI线程中要处理对UI有关的对象进行操作,一般需要使用委托的方式,代码基本就是下面的写法 App.Current.MainWindow.Dispatcher.Invoke(ne ...

随机推荐

  1. 3行3列表格 table实现,div+css实现

    table实现: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...

  2. Linux下搭建Oracle11g RAC(5)----配置ASM磁盘

    将共享磁盘格式化.然后用asmlib将其配置为ASM磁盘,用于将来存放OCR.Voting Disk和数据库用. 注意:只需在其中1个节点上格式化就可以,接下来我们选择在node1节点上格式化. 这里 ...

  3. springMvc解决json中文乱码

    springMvc解决json中文乱码 springMvc解决json中文乱码,springMvc中文乱码,spring中文乱码 >>>>>>>>> ...

  4. Unity3D获取当前键盘按键

    获取当前键盘按键,代码如下: using UnityEngine; using System.Collections; public class GetCurrentKey : MonoBehavio ...

  5. everything is nothing

    可以选ios,可以选择android ,可以选择javaee,因为不想让自己这段时间的努力没有一个完美的结局.最起码真的能做点东西出来,所以6.10--8.10,两个月把javaee实训的该准备.可以 ...

  6. PHP 数组转字符串,与字符串转数组

    implode 使用一个字符串将数组变成字符串 <?php $array = array('lastname', 'email', 'phone'); $comma_separated = im ...

  7. JQuery Datatables(二)

    前篇讲到了Datatables的基本用法,链接地址:http://www.cnblogs.com/wumian1360/p/4263129.html 今天来实现5,6,7三点. 其实Datatable ...

  8. ubuntu下配置java环境变量

    1.官网下载linux对应的jdk安装包tar.gz 2.filezilla上传tar.gz到对应ubuntu目录test下(见上一篇) 3.解压:tar -zcvf XXX.tar.gz 4.修改解 ...

  9. Ubuntu 之旅 —— 解决sudo: source: command not found错误

    $ sudo -s # source /etc/profile

  10. Java学习----Math函数

    public class TestMath { public static void main(String[] args) { System.out.println(Math.E); System. ...