关于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手动加 ...
随机推荐
- JavaBean基础
JavaBean的概念 JavaBean是一种可重复使用.且跨平台的软件组件.JavaBean可分为两种:一种是有用户界面(UI,User Interface)的JavaBean:还有一种是没有用户界 ...
- 设计模式21---设计模式之享元模式(Flyweight)(结构型)
1.讲解享元模式(结构型) 1.1享元模式定义 运用共享技术有效地支持大量细粒度对象. 享元:把内部状态共享出来 1.2享元模式要点 重点在于分离变与不变. 把一个对象的状态分为内部状态和外部状态,内 ...
- 账户管理命令 useradd、groupadd
内容提要: 1. 掌握用户的 增/删/改 命令 2. 掌握组的 增/删/改 命令 组管理 1)groupadd groupadd 用于添加组账号.格式如下: groupadd [-g GID] GRO ...
- git分享:Git_DataPro
基于TCP/UDP网络协议,在Java NIO特性下实现Socket通信,实现终端设备数据的实时接收解析存储.主要技术点:多线程.缓存.C3P0连接池等. <项目执行:打包下载全部文件导入Ecl ...
- IOS Dictionary和Model相互转换
// // HYBJSONModel.h // Json2ModelDemo // // Created by huangyibiao on 14-9-15. // Copyright (c) 201 ...
- Java基础知识强化之IO流笔记18:FileOutputStream写入数据
1. 创建字节输出流对象,做了几件事情: (1)调用系统功能去创建文件(2)创建fos对象(3)把fos对象指向这个文件 2. 代码示例: package com.himi.fileoutputstr ...
- WebView使用详解(一)——Native与JS相互调用(附JadX反编译)
念念不忘,必有回响,永远坚持你所坚持的! 一直在用WebView,还没有系统的总结过它的用法,下面就系统的总结下,分享给大家 一.基本用法 1.加载在线URL void loadUrl(String ...
- IOS 获取wifi的SSID
#import <SystemConfiguration/CaptiveNetwork.h> - (NSString *)currentWifiSSID { // Does not wor ...
- php laravel mysql无法连接处理方案(linux服务器配置)
阿里云 Ubuntu 14.*上搭建laravel环境 之前做项目时都是搭建在自己的服务器上,可是自己的那个服务器是很久以前一点点配置好的,也是各种百度,该忘记的都忘了, 所以前一段在客户的阿里云Ub ...
- photoshop mac版下载及破解
1.下载 直接百度photoshop,就可以找到百度的下载源: 2.破解 http://zhidao.baidu.com/question/581955095.html