关于Qt在子线程中使用QMessageBox的折衷方法
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的折衷方法的更多相关文章
- MFC在子线程中创建窗口(PostMessage方法)
1.创建子线程 C++创建线程的方式比较多 1)最简单易用的<thread>头文件,但是这种方法创建的子线程中无法给主线程PostMessage消息(也可能是我操作有误,总之没成功) 2) ...
- Android在子线程中更新UI(二)
MainActivity如下: package cc.testui2; import android.os.Bundle; import android.view.View; import andro ...
- 子线程中刷新了UI
This application is modifying the autolayout engine from a background thread, which can lead to engi ...
- 【转载】Delphi7从子线程中发送消息到主线程触发事件执行
在对数据库的操作时,有时要用一个子线程来进行后台的数据操作.比如说数据备份,转档什么的.在主窗口还能同是进行其它操作.而有时后台每处理一个数据文件,要向主窗口发送消息,让主窗口实时显示处理进度在窗口上 ...
- 在子线程中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 ...
- 如何在子线程中使用Toast和更新UI
因为没一个Looper处理消息循环,所以子线程中无法使用Toast 方法: Looper.prepare(); Toast.makeText(getActivity(),"刷到底啦" ...
- 在子线程中使用runloop,正确操作NSTimer计时的注意点 三种可选方法
一直想写一篇关于runloop学习有所得的文章,总是没有很好的例子.游戏中有一个计时功能在主线程中调用: 1 + (NSTimer *)scheduledTimerWithTimeInterval:( ...
- 让NSURLConnection在子线程中运行
可以有两个办法让NSURLConnection在子线程中运行,即将NSURLConnection加入到run loop或者NSOperationQueue中去运行. 前面提到可以将NSTimer手动加 ...
- iOS多线程的初步研究(五)-- 如何让NSURLConnection在子线程中运行
可以有两个办法让NSURLConnection在子线程中运行,即将NSURLConnection加入到run loop或者NSOperationQueue中去运行. 前面提到可以将NSTimer手动加 ...
随机推荐
- 用于A*的 二叉堆 AS3实现
package com.copper.isometric.pathing { import flash.sampler.startSampling; /** * ...
- 嵌入式C语言优化小技巧
嵌入式C语言优化小技巧 1 概述 嵌入式系统是指完成一种或几种特定功能的计算机系统,具有自动化程度高,响应速度快等优点,目前已广泛应用于消费电子,工业控制等领域.嵌入式系统受其使用的硬件以及运行环境的 ...
- linux下ssh免密登陆
如果 有A.B两台主机: 要实现的效果: A主机ssh登录B主机无需输入password: 加密方式选 rsa|dsa均能够.默认rsa 做法: 1.登录A主机 2.ssh-keygen -t [rs ...
- python 参议院文本预处理的一维数组的间隔空间
#!/usr/bin/python import re def pre_process_msg ( msgIn ): if msgIn=="": retur ...
- 前端的数据库:IndexedDB 。 ps:入门
应用程序需要数据.对大多数Web应用程序来说,数据在服务器端组织和管理,客户端通过网络请求获取.随着浏览器变得越来越有能力,因此可选择在浏览器存储和操纵应用程序数据. 本文向你介绍名为IndexedD ...
- codevs2034 01串2
/* 一开始认为是个水题 直接模拟 没想到只得了50分 一看数据吓niao了 模拟妥妥的TLE 实在不好优化了0.0(最快O(m)) 然后借鉴别人的 DP+神奇的输出 DP:状态:f[i][j] 前i ...
- HighCharts常用设置(摘抄笔录)
- 滑动页面,顶部导航or顶部 固定在一个位置
现在很多页面 特别是电商用的比较多 比如电商里面某个商品的详细页 往下拉页面 当滚轮到达一定位置的时候 导航栏即固定在顶部 其实他的原理很简单, 就是一开始设置导航为相对定位,然后计算出滚动条离顶部 ...
- [转]Delphi 关键字详解
全文链接地址:http://www.cnblogs.com/del/archive/2008/06/23/1228562.html
- javascript基础学习(十一)
javascript之BOM 学习要点: BOM介绍 Window对象 一.BOM介绍 浏览器对象模型简称为BOM(Brower Object Model),BOM由很多对象构成,对象与对象之间有着相 ...