简述

通过前几节的自定义窗体的学习,我们可以很容易的写出一套属于自己风格的界面框架,通用于各种窗体,比如: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之自定义界面(QMessageBox)的更多相关文章

  1. 【Qt】Qt之自定义界面(QMessageBox)【转】

    简述 通过前几节的自定义窗体的学习,我们可以很容易的写出一套属于自己风格的界面框架,通用于各种窗体,比如:QWidget.QDialog.QMainWindow. 大多数窗体的实现都是采用控件堆积来完 ...

  2. Qt学习笔记 QMessageBox

    Qt的几种MessageBox 1.Infomation类型 QMessageBox::information(this,tr("hello"),tr("title&qu ...

  3. 【Qt】Qt之自定义界面(窗体缩放-跨平台终极版)【转】

    简述 通过上一节内容,我们实现了窗体的缩放,功能很不错,但是很遗憾-不支持跨平台!如果对于多平台来说,这是一个硬伤,所以,我们急需要一个能够支持跨平台的实现方案. 在网上看到过很多不同的实现方式,多多 ...

  4. 【Qt】Qt之自定义界面(添加自定义标题栏)【转】

    简述 通过上节内容,我们实现了自定义窗体的移动,但是我们缺少一个标题栏来显示窗体的图标.标题,以及控制窗体最小化.最大化.关闭的按钮. 自定义标题栏后,所有的控件我们都可以定制,比如:在标题栏中添加换 ...

  5. 【Qt】Qt之自定义界面(实现无边框、可移动)【转】

    简述 UI设计是指对软件的人机交互.操作逻辑.界面美观的整体设计.好的UI设计不仅是让软件变得有个性.有品位,还要让软件的操作变得舒适简单.自由,充分体现软件的定位和特点. 爱美之心人皆有之.其实软件 ...

  6. Qt之自定义界面(窗体缩放-跨平台终极版)

    简述 通过上一节内容,我们实现了窗体的缩放,功能很不错,但是很遗憾-不支持跨平台!如果对于多平台来说,这是一个硬伤,所以,我们急需要一个能够支持跨平台的实现方案. 在网上看到过很多不同的实现方式,多多 ...

  7. Qt之自定义界面(窗体缩放)

    简述 通过前两节内容,我们实现了自定义窗体的移动,以及自定义标题栏-用来显示窗体的图标.标题,以及控制窗体最小化.最大化.关闭. 在这之后,我们还缺少窗体的缩放-当鼠标移动到窗体的边框-左.上.右.下 ...

  8. Qt之自定义界面(添加自定义标题栏)

    简述 通过上节内容,我们实现了自定义窗体的移动,但是我们缺少一个标题栏来显示窗体的图标.标题,以及控制窗体最小化.最大化.关闭的按钮. 自定义标题栏后,所有的控件我们都可以定制,比如:在标题栏中添加换 ...

  9. Qt之自定义界面(实现无边框、可移动)

    简述 UI设计是指对软件的人机交互.操作逻辑.界面美观的整体设计.好的UI设计不仅是让软件变得有个性.有品位,还要让软件的操作变得舒适简单.自由,充分体现软件的定位和特点. 爱美之心人皆有之.其实软件 ...

随机推荐

  1. python调用C语言

    标签(空格分隔): python test.c代码如下 #include<stdio.h> void display(char* msg) { printf("%s\n" ...

  2. mysql注入绕过的一些技巧

    虽然mysql + php的开发中可以使用pdo中,但是有些老久的程序没有使用,或其他原因 1.注释绕过 select/*comment*/user/*zzsdsdsf*/from mysql.use ...

  3. linux下MySQL 5.6源码安装

    linux下MySQL 5.6源码安装 1.下载:当前mysql版本到了5.6.20 http://dev.mysql.com/downloads/mysql 选择Source Code 2.必要软件 ...

  4. c++ 格式化printf

    类型为uint64_t的变量,使用printf进行打印时,需要区分操作系统: 64位系统:使用%ld 32位系统:使用%llu #include<stdio.h>#include < ...

  5. web.xml中servlet的配置

    <servlet>元素是配置Servlet所用的元素. <servlet-mapping>元素在Servlet和URL样式之间定义一个映射,即servlet类提供一个url,在 ...

  6. MyEclipse 简单快捷键

    1) Ctrl+/  注释当前行,再按则取消注释 2) Ctrl+M切换窗口的大小 3) Ctrl+Shift+O作用是缺少的Import语句被加入,多余的Import语句被删除. 4)Alt+/ 代 ...

  7. 查看 dmp 字符集

    用Oracle的exp工具导出的dmp文件也包含了字符集信息,dmp文件的第2和第3个字节记录了dmp文件的字符集.如果dmp文件不大,比如只有几M或几十M,可以用UltraEdit打开(16进制方式 ...

  8. Unity3D Development模式下的一个小问题

    今天客户提交了一个反馈,说测试版本的应用在按下电源键的时候屏幕变黑,然后重新按下电源键启动的时候发现没有出现屏幕锁屏的情况,直接回到应用界面. 我这边看了一下,发现如果装了360之类的手机助手就没这个 ...

  9. firefox无法安装未通过验证的扩展

    firefox43版本无法安装未验证附加组件,利用以下方法: 1.进入firefox  about:config页面中 2.搜索xpinstall.signatures.required,将值改为fa ...

  10. Candy

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...