连续更新GUI表单元素,以使Form不会挂起 [英] Updating GUI form elements continuously so that Form does not hang

查看:76
本文介绍了连续更新GUI表单元素,以使Form不会挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所述,我试图通过线程连续更新GUI表单元素,但是表单似乎仍然很忙.这是我无法理解的.这就是我的处理方式.

As the title states I am attempting to update a GUI form element continuously via a thread however the form still seems to be busy. This I cannot understand.Here is how I am approaching it.

connect(this,SIGNAL(SIGUpdateForm),this,SLOT(MyUpdateMehtod));

现在每当需要更新表单时,我只需执行以下操作.我在新线程中启动方法.然后,新线程会触发上述信号.

now whenever the form needs to be updated I simply do the following.I launch a method in a new thread . The new thread then triggers the above signal.

boost::thread t(&SomeClass::SomeMethod(),this);

现在一旦开始someMethod,这就是我要做的

Now once someMethod is started here is what I do

void SomeMethod()
{
      SIGUpdateForm(); //Launch the signal that will update the form
}

然后,SIGUpdateForm调用MyUpdateMehtod(),但是由于信号(无论是排队还是直接都不启动任何线程,因此似乎挂起了表单.)但是这使我感到困惑,因为信号本身是从独立线程中调用的,为什么?表格挂了吗?我该怎么做才能使这项工作成功?

The SIGUpdateForm then calls MyUpdateMehtod() however since signal (whether queued or direct do not launch any thread thus it seems like the form is hung.) But this confuses me because the signal itself is being called from an independent thread so why is the form hanging ? What can i do to make this work ?

推荐答案

Qt有其自己的线程.实际上,您不需要线程. QTimer会为您做.这是一个例子.

Qt has its own thread. Actually you don't need a thread. QTimer will do it for you. Here is an example.

void updateForm()
{
 ui->bla->setText("bla");
 // bla bla method
}

QTimer timer;
connect(&timer, SIGNAL(timeout()), this, SLOT(updateForm()));
timer.start(3000);

现在将每3秒调用一次updateForm(). GUI将不会挂起.这样做的另一种方法是处理事件循环,

Now updateForm() will be called every 3s. GUI will not hang. Another way of doing so is processing the event loop,

while(....)
{
 // some lengthy task
 qApp->processEvents(QEventLoop::AllEvents);
}

这篇关于连续更新GUI表单元素,以使Form不会挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆