【Qt】Qt之自定义界面(QMessageBox)【转】
简述
通过前几节的自定义窗体的学习,我们可以很容易的写出一套属于自己风格的界面框架,通用于各种窗体,比如:QWidget、QDialog、QMainWindow。
大多数窗体的实现都是采用控件堆积来完成的,只要思路清晰,再复杂的界面实现起来都游刃有余。下面我来列举一个由QMessageBox扩展的提示框-根据其源码实现思路来实现!
效果
自定义提示框
实现
message_box.h
#ifndef MESSAGE_BOX
#define MESSAGE_BOX
#include <QMessageBox>
#include <QDialogButtonBox>
#include <QGridLayout>
#include "custom_window.h"
class QLabel;
class MessageBox : public CustomWindow
{
Q_OBJECT
public:
explicit MessageBox(QWidget *parent = 0, const QString &title = tr("Tip"), const QString &text = "",
QMessageBox::StandardButtons buttons = QMessageBox::Ok, QMessageBox::StandardButton defaultButton = QMessageBox::Ok);
~MessageBox();
QAbstractButton *clickedButton() const;
QMessageBox::StandardButton standardButton(QAbstractButton *button) const;
// 设置默认按钮
void setDefaultButton(QPushButton *button);
void setDefaultButton(QMessageBox::StandardButton button);
// 设置窗体标题
void setTitle(const QString &title);
// 设置提示信息
void setText(const QString &text);
// 设置窗体图标
void setIcon(const QString &icon);
// 添加控件-替换提示信息所在的QLabel
void addWidget(QWidget *pWidget);
protected:
// 多语言翻译
void changeEvent(QEvent *event);
private slots:
void onButtonClicked(QAbstractButton *button);
private:
void translateUI();
int execReturnCode(QAbstractButton *button);
private:
QLabel *m_pIconLabel;
QLabel *m_pLabel;
QGridLayout *m_pGridLayout;
QDialogButtonBox *m_pButtonBox;
QAbstractButton *m_pClickedButton;
QAbstractButton *m_pDefaultButton;
};
message_box.cpp
#include <QLabel>
#include <QPushButton>
#include <QMessageBox>
#include <QCheckBox>
#include <QHBoxLayout>
#include <QEvent>
#include <QApplication>
#include "message_box.h"
MessageBox::MessageBox(QWidget *parent, const QString &title, const QString &text,
QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
: CustomWindow(parent)
{
setWindowIcon(QIcon(":/Images/logo"));
setWindowTitle(title);
setMinimumSize(300, 130);
setMinimizeVisible(false);
setMaximizeVisible(false);
setWidgetResizable(false);
m_pButtonBox = new QDialogButtonBox(this);
m_pButtonBox->setStandardButtons(QDialogButtonBox::StandardButtons(int(buttons)));
setDefaultButton(defaultButton);
QPushButton *pYesButton = m_pButtonBox->button(QDialogButtonBox::Yes);
if (pYesButton != NULL)
{
pYesButton->setObjectName("blueButton");
pYesButton->setStyle(QApplication::style());
}
m_pIconLabel = new QLabel(this);
m_pLabel = new QLabel(this);
QPixmap pixmap(":/Images/information");
m_pIconLabel->setPixmap(pixmap);
m_pIconLabel->setFixedSize(35, 35);
m_pIconLabel->setScaledContents(true);
m_pLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
m_pLabel->setObjectName("whiteLabel");
m_pLabel->setOpenExternalLinks(true);
m_pLabel->setText(text);
m_pGridLayout = new QGridLayout();
m_pGridLayout->addWidget(m_pIconLabel, 0, 0, 2, 1, Qt::AlignTop);
m_pGridLayout->addWidget(m_pLabel, 0, 1, 2, 1);
m_pGridLayout->addWidget(m_pButtonBox, m_pGridLayout->rowCount(), 0, 1, m_pGridLayout->columnCount());
m_pGridLayout->setSizeConstraint(QLayout::SetNoConstraint);
m_pGridLayout->setHorizontalSpacing(10);
m_pGridLayout->setVerticalSpacing(10);
m_pGridLayout->setContentsMargins(10, 10, 10, 10);
m_pLayout->addLayout(m_pGridLayout);
translateUI();
connect(m_pButtonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(onButtonClicked(QAbstractButton*)));
}
MessageBox::~MessageBox()
{
}
void MessageBox::changeEvent(QEvent *event)
{
switch (event->type())
{
case QEvent::LanguageChange:
translateUI();
break;
default:
CustomWindow::changeEvent(event);
}
}
void MessageBox::translateUI()
{
QPushButton *pYesButton = m_pButtonBox->button(QDialogButtonBox::Yes);
if (pYesButton != NULL)
pYesButton->setText(tr("Yes"));
QPushButton *pNoButton = m_pButtonBox->button(QDialogButtonBox::No);
if (pNoButton != NULL)
pNoButton->setText(tr("No"));
QPushButton *pOkButton = m_pButtonBox->button(QDialogButtonBox::Ok);
if (pOkButton != NULL)
pOkButton->setText(tr("Ok"));
QPushButton *pCancelButton = m_pButtonBox->button(QDialogButtonBox::Cancel);
if (pCancelButton != NULL)
pCancelButton->setText(tr("Cancel"));
}
QMessageBox::StandardButton MessageBox::standardButton(QAbstractButton *button) const
{
return (QMessageBox::StandardButton)m_pButtonBox->standardButton(button);
}
QAbstractButton *MessageBox::clickedButton() const
{
return m_pClickedButton;
}
int MessageBox::execReturnCode(QAbstractButton *button)
{
int nResult = m_pButtonBox->standardButton(button);
return nResult;
}
void MessageBox::onButtonClicked(QAbstractButton *button)
{
m_pClickedButton = button;
done(execReturnCode(button));
}
void MessageBox::setDefaultButton(QPushButton *button)
{
if (!m_pButtonBox->buttons().contains(button))
return;
m_pDefaultButton = button;
button->setDefault(true);
button->setFocus();
}
void MessageBox::setDefaultButton(QMessageBox::StandardButton button)
{
setDefaultButton(m_pButtonBox->button(QDialogButtonBox::StandardButton(button)));
}
void MessageBox::setTitle(const QString &title)
{
setWindowTitle(title);
}
void MessageBox::setText(const QString &text)
{
m_pLabel->setText(text);
}
void MessageBox::setIcon(const QString &icon)
{
m_pIconLabel->setPixmap(QPixmap(icon));
}
void MessageBox::addWidget(QWidget *pWidget)
{
m_pLabel->hide();
m_pGridLayout->addWidget(pWidget, 0, 1, 2, 1);
}
接口说明
CustomWindow
主要对界面的无边框可拖动进行了封装
MessageBox
整体界面布局及事件处理参考了QMessageBox源码,接口包含:设置标题、提示信息、默认按钮及事件触发等操作。
二次封装
针对于各种提示框,我们可以再次进行封装,将常用的提取出来,作为全局函数来使用。
QMessageBox::StandardButton showInformation(QWidget *parent, const QString &title,
const QString &text, QMessageBox::StandardButtons buttons,
QMessageBox::StandardButton defaultButton)
{
MessageBox msgBox(parent, title, text, buttons, defaultButton);
msgBox.setIcon(":/Images/information");
if (msgBox.exec() == -1)
return QMessageBox::Cancel;
return msgBox.standardButton(msgBox.clickedButton());
}
QMessageBox::StandardButton showError(QWidget *parent, const QString &title,
const QString &text, QMessageBox::StandardButtons buttons,
QMessageBox::StandardButton defaultButton)
{
MessageBox msgBox(parent, title, text, buttons, defaultButton);
msgBox.setIcon(":/Images/error");
if (msgBox.exec() == -1)
return QMessageBox::Cancel;
return msgBox.standardButton(msgBox.clickedButton());
}
QMessageBox::StandardButton showSuccess(QWidget *parent, const QString &title,
const QString &text, QMessageBox::StandardButtons buttons,
QMessageBox::StandardButton defaultButton)
{
MessageBox msgBox(parent, title, text, buttons, defaultButton);
msgBox.setIcon(":/Images/success");
if (msgBox.exec() == -1)
return QMessageBox::Cancel;
return msgBox.standardButton(msgBox.clickedButton());
}
QMessageBox::StandardButton showQuestion(QWidget *parent, const QString &title,
const QString &text, QMessageBox::StandardButtons buttons,
QMessageBox::StandardButton defaultButton)
{
MessageBox msgBox(parent, title, text, buttons, defaultButton);
msgBox.setIcon(":/Images/question");
if (msgBox.exec() == -1)
return QMessageBox::Cancel;
return msgBox.standardButton(msgBox.clickedButton());
}
QMessageBox::StandardButton showWarning(QWidget *parent, const QString &title,
const QString &text, QMessageBox::StandardButtons buttons,
QMessageBox::StandardButton defaultButton)
{
MessageBox msgBox(parent, title, text, buttons, defaultButton);
msgBox.setIcon(":/images/warning");
if (msgBox.exec() == -1)
return QMessageBox::Cancel;
return msgBox.standardButton(msgBox.clickedButton());
}
QMessageBox::StandardButton showCritical(QWidget *parent, const QString &title,
const QString &text, QMessageBox::StandardButtons buttons,
QMessageBox::StandardButton defaultButton)
{
MessageBox msgBox(parent, title, text, buttons, defaultButton);
msgBox.setIcon(":/Images/warning");
if (msgBox.exec() == -1)
return QMessageBox::Cancel;
return msgBox.standardButton(msgBox.clickedButton());
}
QMessageBox::StandardButton showCheckBoxQuestion(QWidget *parent, const QString &title,
const QString &text, QMessageBox::StandardButtons buttons,
QMessageBox::StandardButton defaultButton)
{
MessageBox msgBox(parent, title, text, buttons, defaultButton);
msgBox.setIcon(":/Images/question");
QCheckBox *pCheckBox = new QCheckBox(&msgBox);
pCheckBox->setText(text);
msgBox.addWidget(pCheckBox);
if (msgBox.exec() == -1)
return QMessageBox::Cancel;
QMessageBox::StandardButton standardButton = msgBox.standardButton(msgBox.clickedButton());
if (standardButton == QMessageBox::Yes)
{
return pCheckBox->isChecked() ? QMessageBox::Yes : QMessageBox::No;
}
return QMessageBox::Cancel;
}
使用方式
showInformation(this, QStringLiteral("提示"), QStringLiteral("这是一个普通的提示框-Information!"));
showQuestion(this, QStringLiteral("提示"), QStringLiteral("这是一个普通的提示框-Question!"));
showSuccess(this, QStringLiteral("提示"), QStringLiteral("这是一个普通的提示框-Success!"));
showError(this, QStringLiteral("提示"), QStringLiteral("这是一个普通的提示框-Error!"));
源码学习
其实Qt中有很多自带的比较好的效果,里面用了很好的实现方式,建议安装的时候把源码download下来,随时可以研究并学习。例如:D:\Qt\Qt5.5.1\5.5\Src\qtbase\src\widgets\dialogs下面包含了所有关于dialog的实现-QProgressDialog、QMessageBox、QFileDialog。。。
原文作者:一去丶二三里
作者博客:去作者博客空间
【Qt】Qt之自定义界面(QMessageBox)【转】的更多相关文章
- Qt... configure: error: Qt (>= Qt 2.2.2) (headers…
转载:http://blog.chinaunix.net/uid-23733724-id-290980.html 昨天开始在自己的fedora12下装qt~ 但是按照教程在/opt/Embed ...
- Qt, QT/E, Qtopia 的区别
转自Qt, QT/E, Qtopia 的区别 Qt泛指Qt的所有桌面版本,比如Qt/X11,Qt Windows,Qt Mac等.由于Qt最早是在Linux中随着KDE流行开来的,因此通常很多人说的Q ...
- QT QT creator QTsdk的区别
Qt是一个跨平台的C++图形用户界面应用程序框架.它提供给应用程序开发者建立艺术级的图形用户界面所需的所用功能.Qt是完全面向对象的,很容易扩展,并且允许真正地组件编程. QT Creator 跨平台 ...
- QT,QT SDK, QT Creator 区别
Qt是一个跨平台的C++图形用户界面应用程序框架.(不仅仅是C++,还包括QML,Qquick,html5)它提供给应用程序开发者建立艺术级的图形用户界面所需的所用功能.Qt是完全面向对象的,很容易扩 ...
- 【Qt】Qt国际化(系统文本-QMessageBox按钮、QLineEdit右键菜单等)【转】
简介 使用Qt的时候,经常会遇到英文问题,例如:QMessageBox中的按钮.QLineEdit.QSpinBox.QScrollBar中的右键菜单等.通常情况下,我们软件都不会是纯英文的,那么如何 ...
- 【Qt开发】Qt标准对话框之QMessageBox
好久没有更新博客,主要是公司里面还在验收一些东西,所以没有及时更新.而且也在写一个基于Qt的画图程序,基本上类似于PS的东西,主要用到的是Qt Graphics View Framework.好了,现 ...
- Qt 信息提示框 QMessageBox
information QMessageBox::information(NULL, "Title","Content",QMessageBox::Yes | ...
- QT+QT creator+OpenCV图像灰度化
1).pro文件 #------------------------------------------------- # # Project created by QtCreator 2014-05 ...
- Qt QT的IO流 QT输入输出
1. QFile QDataStream 读写文件 二进制读写文件 #include <QApplication> #include <QtGui> #include < ...
- [QT]Qt+VS2012+Win8 64Bit安装
学习Qt鸟,当年没听@Coding_Peon(http://weibo.com/u/1764451551?topnav=1&wvr=5&topsug=1)话好好学习QT和Python之 ...
随机推荐
- Unity3D 使用 UI 的 Grid Layout Group 组件。
1.首先创建一个容器,用于存放列表项的内容. 这里使用 Panel 来做为容器. 这里要注意! “Grid Layout Group”是要增加在容器的游戏对象里. 同时,只有容器对象的子对象才有排列效 ...
- [C.Sharp] TimeSpan的用法,获取测试程序运行时间
TimeSpan的用法 TimeSpan是用来表示一个时间段的实例,两个时间的差可以构成一个TimeSpan实例,现在就来简单介绍一下几点重要的用法: a 先来介绍几个方法 TimeSpan.Minu ...
- 不停服务情况下升级nginx
第三方支付平台因安全问题对nginx做了升级操作,为了不影响业务,整个操作过程都不能停服务,因此对升级方法做出了要求.以下为我整理的生产环境实际操作方法,已在第三方支付平台上成功应用,希望对即将或者可 ...
- linux_iptables 详解
iptables工具__过滤包—命令(-A.-I.-D.-R.-L等).参数(-p.-s.-d.--sport.--dport.-i.-o等).动作-j (ACCEPT.DROP.REJECT.RED ...
- linux初学 :简易的shell脚本
什么是shell Shell本身是一个用C语言编写的程序,它是用户使用Unix/Linux的桥梁,用户的大部分工作都是通过Shell完成的 Shell有两种执行命令的方式: 交互式(Interacti ...
- [freemarker篇]04.简单介绍-基本数据类型
好几天没有更新博客了,最近一顿乱七八糟的忙!今天简单更新第四章,这里只能说是简单介绍,因为权威的还是需要看Freemarker的官方中文手册! 好几天没有更新博客了,最近一顿乱七八糟的忙!今天简单更新 ...
- asp.net Calendar 日历控件用法
asp.net Calendar 是微软自带的一款日历控件,除了简单显示日期时间外, 还可以绑定一些需要的事件. Calendar_DayRender 事件,是在加载都去时间日期时候的方法,用此方法可 ...
- canvas基础2--绘制图形
栅格 绘制矩形 不同于SVG,HTML中的元素canvas只支持一种原生的图形绘制:矩形.所有其他的图形的绘制都至少需要生成一条路径.不过,我们拥有众多路径生成的方法让复杂图形的绘制成为了可能. 首先 ...
- WinFrom“动态”WebService
1.首先添加一个WebService: 2.输入地址....Ok: 3.在WebService用到的类上按F12: 4.进入类中,找到其构造函数: 5.修改其构造函数为代参数,并且让this.Url= ...
- JS 回车提交
1.JavaScript 方法: <script> document.onkeydown=function(event){ e = event ? event :(window.event ...