当前位置: 代码迷 >> QT开发 >> Qt多线程中如何设置进度条
  详细解决方案

Qt多线程中如何设置进度条

热度:244   发布时间:2016-04-25 03:04:42.0
Qt多线程中怎么设置进度条?
本帖最后由 fantasy999999999 于 2014-12-25 11:27:50 编辑
我程序中有一个计算模块会很耗时,造成界面假死,我把计算模块放到了一个新的线程中,但是想通过一个进度条来告诉用户计算模块处理数据的进度,但是貌似进度条在主线程中,也就是GUI线程才能用,不知道怎么才能让进度条能实时显示计算进度.
------解决思路----------------------
信号与槽,或者使用postEvent发送时间给GUI线程。
------解决思路----------------------
Because of limitations inherited from the low-level libraries on which Qt's GUI support is built, QWidget and its subclasses are not reentrant. One consequence of this is that we cannot directly call functions on a widget from a secondary thread. If we want to, say, change the text of a QLabel from a secondary thread, we can emit a signal connected to QLabel::setText() or call QMetaObject::invokeMethod() from that thread. For example:

void MyThread::run()
{
    ...
    QMetaObject::invokeMethod(label, SLOT(setText(const QString &)),
                              Q_ARG(QString, "Hello"));
    ...
}
  相关解决方案