Qt将所有GUI相关的处理都限制在主线程中,这么做有助于防止意想不到的访问冲突产生,但也限制了线程中某些简单的UI交互的实现,比如QMessageBox。

因为QMessageBox必须在主线程中打开,为了使用它,便不得不自己动手实现一些信号和槽,从而增加了自己代码的复杂度。为降低使用QMessageBox时的设计负担,本文从QThread类继承一个新类MsgBoxThread,将这些实现细节封装为MsgBoxThread的成员函数about、aboutQt、critical、information、question、warning,分别与QMessageBox的静态成员about、aboutQt、critical、information、question、warning对应,使用时可从该类继承子类,并在run函数中使用这些函数:

#include <QThread>
#include <QMessageBox> class MsgBoxThread : public QThread
{
Q_OBJECT
typedef enum {
mbt_about = ,
mbt_aboutqt = ,
mbt_critical = ,
mbt_information = ,
mbt_question = ,
mbt_warning =
} MSGBOXTYPE;
protected:
int m_btnres;
void about(QWidget * parent, const QString &title, const QString &text) {
emit msgbox_sig(mbt_about, parent, title, text, QMessageBox::NoButton, QMessageBox::NoButton);
} void aboutQt(QWidget *parent, const QString &title = QString()) {
emit msgbox_sig(mbt_aboutqt, parent, title, tr(""), QMessageBox::NoButton, QMessageBox::NoButton);
} int critical(QWidget * parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = QMessageBox::Ok, QMessageBox::StandardButtons defaultButton = QMessageBox::NoButton) {
emit msgbox_sig(mbt_critical, parent, title, text, buttons, defaultButton);
return m_btnres;
} int information(QWidget * parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = QMessageBox::Ok, QMessageBox::StandardButtons defaultButton = QMessageBox::NoButton)
{
emit msgbox_sig(mbt_information, parent, title, text, buttons, defaultButton);
return m_btnres;
} int question(QWidget * parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = QMessageBox::StandardButtons(QMessageBox::Yes | QMessageBox::No), QMessageBox::StandardButtons defaultButton = QMessageBox::NoButton) {
emit msgbox_sig(mbt_question, parent, title, text, buttons, defaultButton);
return m_btnres;
} int warning(QWidget * parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = QMessageBox::Ok, QMessageBox::StandardButtons defaultButton = QMessageBox::NoButton) {
emit msgbox_sig(mbt_warning, parent, title, text, buttons, defaultButton);
return m_btnres;
} public:
MsgBoxThread(QObject * parent = )
:QThread(parent), m_btnres(QMessageBox::NoButton) {
qRegisterMetaType<QMessageBox::StandardButtons>("QMessageBox::StandardButtons");
connect(this, SIGNAL(msgbox_sig(MSGBOXTYPE, QWidget *, const QString, const QString, QMessageBox::StandardButtons, QMessageBox::StandardButtons)), SLOT(on_information(MSGBOXTYPE, QWidget *, const QString , const QString, QMessageBox::StandardButtons , QMessageBox::StandardButtons )), Qt::BlockingQueuedConnection);
}
signals:
void msgbox_sig(MSGBOXTYPE type, QWidget * parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButtons defaultButton); private slots:
void on_msgbox(MSGBOXTYPE type, QWidget * parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButtons defaultButton) {
switch(type) {
case mbt_about:
QMessageBox::about(parent,title,text);
break;
case mbt_aboutqt:
QMessageBox::aboutQt(parent, title);
break;
case mbt_critical:
m_btnres = QMessageBox::critical(parent, title, text, buttons, defaultButton);
break;
case mbt_information:
m_btnres = QMessageBox::information(parent, title, text, buttons, defaultButton);
break;
case mbt_question:
m_btnres = QMessageBox::question(parent, title, text, buttons, defaultButton);
break;
case mbt_warning:
m_btnres = QMessageBox::warning(parent, title, text, buttons, defaultButton);
break;
default:
Q_ASSERT_X(false,"QMessageBox in thread","invalid box type specified.");
}
}
};

原理:

自定义的MsgBoxThread类继承自QThread类,其中声明了可在子线程中发射的信号msgbox_sig,并在MsgBoxThread的构造函数中将该信号以阻塞模式(Qt::BlockingQueuedConnection)连接到私有槽函数on_msgbox。因为槽函数的父对象在主线程中创建,这保证了信号msgbox_sig在主线程的事件循环中执行;又由于槽函数on_msgbox采用组色模式连接,使得子线程在调用了这些封装函数(about、aboutQt、information、critical、warning)后会等待主线程的槽函数执行并返回后才会继续执行;综上,即可保证使用QMessageBox实现简单的UI交互,又不违反Qt的GUI相关处理必须在主线程中的限制。

参考:http://blog.csdn.net/johnyork/article/details/46419185

关于Qt在子线程中使用QMessageBox的折衷方法的更多相关文章

  1. MFC在子线程中创建窗口(PostMessage方法)

    1.创建子线程 C++创建线程的方式比较多 1)最简单易用的<thread>头文件,但是这种方法创建的子线程中无法给主线程PostMessage消息(也可能是我操作有误,总之没成功) 2) ...

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

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

  3. 子线程中刷新了UI

    This application is modifying the autolayout engine from a background thread, which can lead to engi ...

  4. 【转载】Delphi7从子线程中发送消息到主线程触发事件执行

    在对数据库的操作时,有时要用一个子线程来进行后台的数据操作.比如说数据备份,转档什么的.在主窗口还能同是进行其它操作.而有时后台每处理一个数据文件,要向主窗口发送消息,让主窗口实时显示处理进度在窗口上 ...

  5. 在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()

    在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException:  Can't create handler inside thread that has ...

  6. 如何在子线程中使用Toast和更新UI

    因为没一个Looper处理消息循环,所以子线程中无法使用Toast 方法: Looper.prepare(); Toast.makeText(getActivity(),"刷到底啦" ...

  7. 在子线程中使用runloop,正确操作NSTimer计时的注意点 三种可选方法

    一直想写一篇关于runloop学习有所得的文章,总是没有很好的例子.游戏中有一个计时功能在主线程中调用: 1 + (NSTimer *)scheduledTimerWithTimeInterval:( ...

  8. 让NSURLConnection在子线程中运行

    可以有两个办法让NSURLConnection在子线程中运行,即将NSURLConnection加入到run loop或者NSOperationQueue中去运行. 前面提到可以将NSTimer手动加 ...

  9. iOS多线程的初步研究(五)-- 如何让NSURLConnection在子线程中运行

    可以有两个办法让NSURLConnection在子线程中运行,即将NSURLConnection加入到run loop或者NSOperationQueue中去运行. 前面提到可以将NSTimer手动加 ...

随机推荐

  1. bzoj3669: [Noi2014]魔法森林 lct

    记得去年模拟赛的时候好像YY出二分答案枚举a,b的暴力,过了55欸 然后看正解,为了将两维变成一维,将a排序,模拟Kruskal的加边过程,同时维护1到n的最大值,加入一条边e(u,v,a,b)时有以 ...

  2. PHP学习之[第07讲]PHP5.4 文件操作函数 之 图片计数器的实例

    1.filetype():输出文件类型: 2.stat():获取文件的基本属性的数组: 3.clearstatcache().is_executable().isDir().idFile().scan ...

  3. PKU 1511 Invitation Cards (SPFA+邻接表)

    题目链接:点击打开链接 题目需要求从原点到所有点的最短距离之和和所有点到原点的最短距离之和,在求所有点到原点最短距离的时候用到了一个技巧:即把图反向,求原点到所有其他点的最短距离,这样用一次SPFA就 ...

  4. [Javascript] Automating Releases with semantic-release

    There are so many repeated steps when releasing a new version of a library. The tool semantic-releas ...

  5. padding与margin的差别

    之前一直没有搞懂android:padding和android:layout_margin的差别,事实上概念非常easy,padding是站在父view的角度描写叙述问题,它规定它里面的内容必须与这个 ...

  6. Protobuf的自动反射消息类型的方法

    1. 每个消息头部中带上type name,作为消息的类型标识 2. 通过type name可以找到描述符Descriptor*, FindMessageTypeByName 3. 通过描述符Desc ...

  7. 使用 trait 时报PHP Parse error: syntax error, unexpected 'use' (T_USE) 这个错误

    找一大圈原因, 最后终于找到了, 不是PHP版本的原因[], 是自己把use 写到了类里的方法里了. 这个东东,  不能脱离类单独使用, 否则的话, 会被认为是命名空间了. 测试例子如下 // Tra ...

  8. 如何参与一个GitHub开源项目

    Github作为开源项目的著名托管地,可谓无人不知,越来越多的个人和公司纷纷加入到Github的大家族里来,为开源尽一份绵薄之力.对于个人来讲,你把自己的项目托管到Github上并不表示你参与了Git ...

  9. codevs 1689 搭建高塔

    /*机智sort二维转一维*/ #include<iostream> #include<cstdio> #include<cstring> #include< ...

  10. 织梦DedeCMS广告管理模块增加图片上传功能插件

    网站广告后台管理非常方便,但是织梦后台的广告管理模块,发布广告时图片没有上传选项,只能用URL地址,很不方便,那么下面就教大家一个方法实现广告图片后台直接上传,非常方便. 先给大家看下修改后的广告图片 ...