简述

通过前几节的自定义窗体的学习,我们可以很容易的写出一套属于自己风格的界面框架,通用于各种窗体,比如:QWidget、QDialog、QMainWindow。

大多数窗体的实现都是采用控件堆积来完成的,只要思路清晰,再复杂的界面实现起来都游刃有余。下面我来列举一个由QMessageBox扩展的提示框-根据其源码实现思路来实现!

效果

自定义提示框

实现

message_box.h

  1. #ifndef MESSAGE_BOX
  2. #define MESSAGE_BOX
  3. #include <QMessageBox>
  4. #include <QDialogButtonBox>
  5. #include <QGridLayout>
  6. #include "custom_window.h"
  7. class QLabel;
  8. class MessageBox : public CustomWindow
  9. {
  10. Q_OBJECT
  11. public:
  12. explicit MessageBox(QWidget *parent = 0, const QString &title = tr("Tip"), const QString &text = "",
  13. QMessageBox::StandardButtons buttons = QMessageBox::Ok, QMessageBox::StandardButton defaultButton = QMessageBox::Ok);
  14. ~MessageBox();
  15. QAbstractButton *clickedButton() const;
  16. QMessageBox::StandardButton standardButton(QAbstractButton *button) const;
  17. // 设置默认按钮
  18. void setDefaultButton(QPushButton *button);
  19. void setDefaultButton(QMessageBox::StandardButton button);
  20. // 设置窗体标题
  21. void setTitle(const QString &title);
  22. // 设置提示信息
  23. void setText(const QString &text);
  24. // 设置窗体图标
  25. void setIcon(const QString &icon);
  26. // 添加控件-替换提示信息所在的QLabel
  27. void addWidget(QWidget *pWidget);
  28. protected:
  29. // 多语言翻译
  30. void changeEvent(QEvent *event);
  31. private slots:
  32. void onButtonClicked(QAbstractButton *button);
  33. private:
  34. void translateUI();
  35. int execReturnCode(QAbstractButton *button);
  36. private:
  37. QLabel *m_pIconLabel;
  38. QLabel *m_pLabel;
  39. QGridLayout *m_pGridLayout;
  40. QDialogButtonBox *m_pButtonBox;
  41. QAbstractButton *m_pClickedButton;
  42. QAbstractButton *m_pDefaultButton;
  43. };

message_box.cpp

  1. #include <QLabel>
  2. #include <QPushButton>
  3. #include <QMessageBox>
  4. #include <QCheckBox>
  5. #include <QHBoxLayout>
  6. #include <QEvent>
  7. #include <QApplication>
  8. #include "message_box.h"
  9. MessageBox::MessageBox(QWidget *parent, const QString &title, const QString &text,
  10. QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
  11. : CustomWindow(parent)
  12. {
  13. setWindowIcon(QIcon(":/Images/logo"));
  14. setWindowTitle(title);
  15. setMinimumSize(300, 130);
  16. setMinimizeVisible(false);
  17. setMaximizeVisible(false);
  18. setWidgetResizable(false);
  19. m_pButtonBox = new QDialogButtonBox(this);
  20. m_pButtonBox->setStandardButtons(QDialogButtonBox::StandardButtons(int(buttons)));
  21. setDefaultButton(defaultButton);
  22. QPushButton *pYesButton = m_pButtonBox->button(QDialogButtonBox::Yes);
  23. if (pYesButton != NULL)
  24. {
  25. pYesButton->setObjectName("blueButton");
  26. pYesButton->setStyle(QApplication::style());
  27. }
  28. m_pIconLabel = new QLabel(this);
  29. m_pLabel = new QLabel(this);
  30. QPixmap pixmap(":/Images/information");
  31. m_pIconLabel->setPixmap(pixmap);
  32. m_pIconLabel->setFixedSize(35, 35);
  33. m_pIconLabel->setScaledContents(true);
  34. m_pLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  35. m_pLabel->setObjectName("whiteLabel");
  36. m_pLabel->setOpenExternalLinks(true);
  37. m_pLabel->setText(text);
  38. m_pGridLayout = new QGridLayout();
  39. m_pGridLayout->addWidget(m_pIconLabel, 0, 0, 2, 1, Qt::AlignTop);
  40. m_pGridLayout->addWidget(m_pLabel, 0, 1, 2, 1);
  41. m_pGridLayout->addWidget(m_pButtonBox, m_pGridLayout->rowCount(), 0, 1, m_pGridLayout->columnCount());
  42. m_pGridLayout->setSizeConstraint(QLayout::SetNoConstraint);
  43. m_pGridLayout->setHorizontalSpacing(10);
  44. m_pGridLayout->setVerticalSpacing(10);
  45. m_pGridLayout->setContentsMargins(10, 10, 10, 10);
  46. m_pLayout->addLayout(m_pGridLayout);
  47. translateUI();
  48. connect(m_pButtonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(onButtonClicked(QAbstractButton*)));
  49. }
  50. MessageBox::~MessageBox()
  51. {
  52. }
  53. void MessageBox::changeEvent(QEvent *event)
  54. {
  55. switch (event->type())
  56. {
  57. case QEvent::LanguageChange:
  58. translateUI();
  59. break;
  60. default:
  61. CustomWindow::changeEvent(event);
  62. }
  63. }
  64. void MessageBox::translateUI()
  65. {
  66. QPushButton *pYesButton = m_pButtonBox->button(QDialogButtonBox::Yes);
  67. if (pYesButton != NULL)
  68. pYesButton->setText(tr("Yes"));
  69. QPushButton *pNoButton = m_pButtonBox->button(QDialogButtonBox::No);
  70. if (pNoButton != NULL)
  71. pNoButton->setText(tr("No"));
  72. QPushButton *pOkButton = m_pButtonBox->button(QDialogButtonBox::Ok);
  73. if (pOkButton != NULL)
  74. pOkButton->setText(tr("Ok"));
  75. QPushButton *pCancelButton = m_pButtonBox->button(QDialogButtonBox::Cancel);
  76. if (pCancelButton != NULL)
  77. pCancelButton->setText(tr("Cancel"));
  78. }
  79. QMessageBox::StandardButton MessageBox::standardButton(QAbstractButton *button) const
  80. {
  81. return (QMessageBox::StandardButton)m_pButtonBox->standardButton(button);
  82. }
  83. QAbstractButton *MessageBox::clickedButton() const
  84. {
  85. return m_pClickedButton;
  86. }
  87. int MessageBox::execReturnCode(QAbstractButton *button)
  88. {
  89. int nResult = m_pButtonBox->standardButton(button);
  90. return nResult;
  91. }
  92. void MessageBox::onButtonClicked(QAbstractButton *button)
  93. {
  94. m_pClickedButton = button;
  95. done(execReturnCode(button));
  96. }
  97. void MessageBox::setDefaultButton(QPushButton *button)
  98. {
  99. if (!m_pButtonBox->buttons().contains(button))
  100. return;
  101. m_pDefaultButton = button;
  102. button->setDefault(true);
  103. button->setFocus();
  104. }
  105. void MessageBox::setDefaultButton(QMessageBox::StandardButton button)
  106. {
  107. setDefaultButton(m_pButtonBox->button(QDialogButtonBox::StandardButton(button)));
  108. }
  109. void MessageBox::setTitle(const QString &title)
  110. {
  111. setWindowTitle(title);
  112. }
  113. void MessageBox::setText(const QString &text)
  114. {
  115. m_pLabel->setText(text);
  116. }
  117. void MessageBox::setIcon(const QString &icon)
  118. {
  119. m_pIconLabel->setPixmap(QPixmap(icon));
  120. }
  121. void MessageBox::addWidget(QWidget *pWidget)
  122. {
  123. m_pLabel->hide();
  124. m_pGridLayout->addWidget(pWidget, 0, 1, 2, 1);
  125. }

接口说明

  • CustomWindow

    主要对界面的无边框可拖动进行了封装

  • MessageBox

    整体界面布局及事件处理参考了QMessageBox源码,接口包含:设置标题、提示信息、默认按钮及事件触发等操作。

二次封装

针对于各种提示框,我们可以再次进行封装,将常用的提取出来,作为全局函数来使用。

  1. QMessageBox::StandardButton showInformation(QWidget *parent, const QString &title,
  2. const QString &text, QMessageBox::StandardButtons buttons,
  3. QMessageBox::StandardButton defaultButton)
  4. {
  5. MessageBox msgBox(parent, title, text, buttons, defaultButton);
  6. msgBox.setIcon(":/Images/information");
  7. if (msgBox.exec() == -1)
  8. return QMessageBox::Cancel;
  9. return msgBox.standardButton(msgBox.clickedButton());
  10. }
  11. QMessageBox::StandardButton showError(QWidget *parent, const QString &title,
  12. const QString &text, QMessageBox::StandardButtons buttons,
  13. QMessageBox::StandardButton defaultButton)
  14. {
  15. MessageBox msgBox(parent, title, text, buttons, defaultButton);
  16. msgBox.setIcon(":/Images/error");
  17. if (msgBox.exec() == -1)
  18. return QMessageBox::Cancel;
  19. return msgBox.standardButton(msgBox.clickedButton());
  20. }
  21. QMessageBox::StandardButton showSuccess(QWidget *parent, const QString &title,
  22. const QString &text, QMessageBox::StandardButtons buttons,
  23. QMessageBox::StandardButton defaultButton)
  24. {
  25. MessageBox msgBox(parent, title, text, buttons, defaultButton);
  26. msgBox.setIcon(":/Images/success");
  27. if (msgBox.exec() == -1)
  28. return QMessageBox::Cancel;
  29. return msgBox.standardButton(msgBox.clickedButton());
  30. }
  31. QMessageBox::StandardButton showQuestion(QWidget *parent, const QString &title,
  32. const QString &text, QMessageBox::StandardButtons buttons,
  33. QMessageBox::StandardButton defaultButton)
  34. {
  35. MessageBox msgBox(parent, title, text, buttons, defaultButton);
  36. msgBox.setIcon(":/Images/question");
  37. if (msgBox.exec() == -1)
  38. return QMessageBox::Cancel;
  39. return msgBox.standardButton(msgBox.clickedButton());
  40. }
  41. QMessageBox::StandardButton showWarning(QWidget *parent, const QString &title,
  42. const QString &text, QMessageBox::StandardButtons buttons,
  43. QMessageBox::StandardButton defaultButton)
  44. {
  45. MessageBox msgBox(parent, title, text, buttons, defaultButton);
  46. msgBox.setIcon(":/images/warning");
  47. if (msgBox.exec() == -1)
  48. return QMessageBox::Cancel;
  49. return msgBox.standardButton(msgBox.clickedButton());
  50. }
  51. QMessageBox::StandardButton showCritical(QWidget *parent, const QString &title,
  52. const QString &text, QMessageBox::StandardButtons buttons,
  53. QMessageBox::StandardButton defaultButton)
  54. {
  55. MessageBox msgBox(parent, title, text, buttons, defaultButton);
  56. msgBox.setIcon(":/Images/warning");
  57. if (msgBox.exec() == -1)
  58. return QMessageBox::Cancel;
  59. return msgBox.standardButton(msgBox.clickedButton());
  60. }
  61. QMessageBox::StandardButton showCheckBoxQuestion(QWidget *parent, const QString &title,
  62. const QString &text, QMessageBox::StandardButtons buttons,
  63. QMessageBox::StandardButton defaultButton)
  64. {
  65. MessageBox msgBox(parent, title, text, buttons, defaultButton);
  66. msgBox.setIcon(":/Images/question");
  67. QCheckBox *pCheckBox = new QCheckBox(&msgBox);
  68. pCheckBox->setText(text);
  69. msgBox.addWidget(pCheckBox);
  70. if (msgBox.exec() == -1)
  71. return QMessageBox::Cancel;
  72. QMessageBox::StandardButton standardButton = msgBox.standardButton(msgBox.clickedButton());
  73. if (standardButton == QMessageBox::Yes)
  74. {
  75. return pCheckBox->isChecked() ? QMessageBox::Yes : QMessageBox::No;
  76. }
  77. return QMessageBox::Cancel;
  78. }

使用方式

  1. showInformation(this, QStringLiteral("提示"), QStringLiteral("这是一个普通的提示框-Information!"));
  2. showQuestion(this, QStringLiteral("提示"), QStringLiteral("这是一个普通的提示框-Question!"));
  3. showSuccess(this, QStringLiteral("提示"), QStringLiteral("这是一个普通的提示框-Success!"));
  4. 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. Careercup - Facebook面试题 - 5412018236424192

    2014-05-01 01:32 题目链接 原题: Given a linked list where apart from the next pointer, every node also has ...

  2. Python Socket File Transfer

    I have a RPi which I intented to use it to crawl data. The development environment in RPi is very ba ...

  3. jsp的<%@ include file="jsp/common.jsp" %>报错误Duplicate local variable basePath

    将公共引入的文件放到common.jsp中,其他页面引入该jsp即可使用 <%@ page language="java" import="java.util.*& ...

  4. [转载]C#获取本机IPv4地址

    C#获取本机IP地址在C#1.0之后都使用下面的这种形式: IPHostEntry ipe = Dns.GetHostEntry(Dns.GetHostName()); IPAddress ipa=i ...

  5. Deep Learning and Shallow Learning

    Deep Learning and Shallow Learning 由于 Deep Learning 现在如火如荼的势头,在各种领域逐渐占据 state-of-the-art 的地位,上个学期在一门 ...

  6. <%@page include%>、<%@include%>、<jsp:include>三者之间的本质区别

    <%@page include%>.<%@include%>.<jsp:include>三者之间的本质区别 先从它的几个内置对象说起. application和se ...

  7. 弱弱的玩下Javascript

    前言 好久没有更新博客了,也蛮少捣弄javascript,今儿看到一个题目,关于给你一个面板,你可以随意的在上面画矩形,可以移动和删除任意一个你创建的矩形,心血来潮搞着玩哈,实现起来挺简单的,但这代码 ...

  8. jquery层居中,点击小图查看大图,弹出层居中代码,顶部层固定不动,滚动条滚动情况

    jquery层居中,点击小图查看大图,弹出层居中代码 http://www.cnblogs.com/simpledev/p/3566280.html 见第一版,发现一个情况,如果页面内容多出一屏的情况 ...

  9. 精华阅读第 10 期 |解开阿尔法狗(AlphaGo)人工智能的画皮

    谷歌用一个变了身的古老「穷举算法」,披上「神经网络」的画皮,假装「跨时代」的黑科技,忽悠广大「膜拜者」,「狮仙」我实在看不下去了,来揭一揭这只幺蛾子小狗的画皮. 本期是移动开发精英俱乐部的第10期推荐 ...

  10. [C++]默认构造函数

    默认构造函数(default constructor)就是在没有显示提供初始化式时调用的构造函数.它由不带参数的构造函数,或者为所有的形参提供默认实参的构造函数定义.若个定义某个类的变量时没有提供初始 ...