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

相关文章

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Qt中QGroupBox控件的实现

《Qt中QGroupBox控件的实现》QGroupBox是Qt框架中一个非常有用的控件,它主要用于组织和管理一组相关的控件,本文主要介绍了Qt中QGroupBox控件的实现,具有一定的参考价值,感兴趣... 目录引言一、基本属性二、常用方法2.1 构造函数 2.2 设置标题2.3 设置复选框模式2.4 是否

QT进行CSV文件初始化与读写操作

《QT进行CSV文件初始化与读写操作》这篇文章主要为大家详细介绍了在QT环境中如何进行CSV文件的初始化、写入和读取操作,本文为大家整理了相关的操作的多种方法,希望对大家有所帮助... 目录前言一、CSV文件初始化二、CSV写入三、CSV读取四、QT 逐行读取csv文件五、Qt如何将数据保存成CSV文件前言

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

Qt spdlog日志模块的使用详解

《Qtspdlog日志模块的使用详解》在Qt应用程序开发中,良好的日志系统至关重要,本文将介绍如何使用spdlog1.5.0创建满足以下要求的日志系统,感兴趣的朋友一起看看吧... 目录版本摘要例子logmanager.cpp文件main.cpp文件版本spdlog版本:1.5.0采用1.5.0版本主要

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

如何自定义Nginx JSON日志格式配置

《如何自定义NginxJSON日志格式配置》Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种... 目录前言为什么选择jsON格式日志?配置步骤详解1. 安装Nginx服务2. 自定义JSON日志格式各

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现