Qt 之自定义界面(QMessageBox)

2024-05-27 19:48

本文主要是介绍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_OBJECTpublic: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);// 添加控件-替换提示信息所在的QLabelvoid 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)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1008429

相关文章

CSS自定义浏览器滚动条样式完整代码

《CSS自定义浏览器滚动条样式完整代码》:本文主要介绍了如何使用CSS自定义浏览器滚动条的样式,包括隐藏滚动条的角落、设置滚动条的基本样式、轨道样式和滑块样式,并提供了完整的CSS代码示例,通过这些技巧,你可以为你的网站添加个性化的滚动条样式,从而提升用户体验,详细内容请阅读本文,希望能对你有所帮助...

Python中的可视化设计与UI界面实现

《Python中的可视化设计与UI界面实现》本文介绍了如何使用Python创建用户界面(UI),包括使用Tkinter、PyQt、Kivy等库进行基本窗口、动态图表和动画效果的实现,通过示例代码,展示... 目录从像素到界面:python带你玩转UI设计示例:使用Tkinter创建一个简单的窗口绘图魔法:用

基于Qt Qml实现时间轴组件

《基于QtQml实现时间轴组件》时间轴组件是现代用户界面中常见的元素,用于按时间顺序展示事件,本文主要为大家详细介绍了如何使用Qml实现一个简单的时间轴组件,需要的可以参考下... 目录写在前面效果图组件概述实现细节1. 组件结构2. 属性定义3. 数据模型4. 事件项的添加和排序5. 事件项的渲染如何使用

Python中构建终端应用界面利器Blessed模块的使用

《Python中构建终端应用界面利器Blessed模块的使用》Blessed库作为一个轻量级且功能强大的解决方案,开始在开发者中赢得口碑,今天,我们就一起来探索一下它是如何让终端UI开发变得轻松而高... 目录一、安装与配置:简单、快速、无障碍二、基本功能:从彩色文本到动态交互1. 显示基本内容2. 创建链

基于Qt开发一个简单的OFD阅读器

《基于Qt开发一个简单的OFD阅读器》这篇文章主要为大家详细介绍了如何使用Qt框架开发一个功能强大且性能优异的OFD阅读器,文中的示例代码讲解详细,有需要的小伙伴可以参考一下... 目录摘要引言一、OFD文件格式解析二、文档结构解析三、页面渲染四、用户交互五、性能优化六、示例代码七、未来发展方向八、结论摘要

SpringBoot 自定义消息转换器使用详解

《SpringBoot自定义消息转换器使用详解》本文详细介绍了SpringBoot消息转换器的知识,并通过案例操作演示了如何进行自定义消息转换器的定制开发和使用,感兴趣的朋友一起看看吧... 目录一、前言二、SpringBoot 内容协商介绍2.1 什么是内容协商2.2 内容协商机制深入理解2.2.1 内容

python与QT联合的详细步骤记录

《python与QT联合的详细步骤记录》:本文主要介绍python与QT联合的详细步骤,文章还展示了如何在Python中调用QT的.ui文件来实现GUI界面,并介绍了多窗口的应用,文中通过代码介绍... 目录一、文章简介二、安装pyqt5三、GUI页面设计四、python的使用python文件创建pytho

QT实现TCP客户端自动连接

《QT实现TCP客户端自动连接》这篇文章主要为大家详细介绍了QT中一个TCP客户端自动连接的测试模型,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录版本 1:没有取消按钮 测试效果测试代码版本 2:有取消按钮测试效果测试代码版本 1:没有取消按钮 测试效果缺陷:无法手动停

基于Qt实现系统主题感知功能

《基于Qt实现系统主题感知功能》在现代桌面应用程序开发中,系统主题感知是一项重要的功能,它使得应用程序能够根据用户的系统主题设置(如深色模式或浅色模式)自动调整其外观,Qt作为一个跨平台的C++图形用... 目录【正文开始】一、使用效果二、系统主题感知助手类(SystemThemeHelper)三、实现细节

Qt实现文件的压缩和解压缩操作

《Qt实现文件的压缩和解压缩操作》这篇文章主要为大家详细介绍了如何使用Qt库中的QZipReader和QZipWriter实现文件的压缩和解压缩功能,文中的示例代码简洁易懂,需要的可以参考一下... 目录一、实现方式二、具体步骤1、在.pro文件中添加模块gui-private2、通过QObject方式创建