增加了许多接口,并且之前v1.0版本添加按钮是自己实现的,其实不需要,直接使用QDialogButtonBox的接口会更方便。
MyMessageBox.h
#ifndef MyMessageBox_H
#define MyMessageBox_H
#include <QApplication>
#include <QDebug>
#include <QDialog>
#include <QDialogButtonBox>
#include <QFile>
#include <QHBoxLayout>
#include <QIcon>
#include <QLabel>
#include <QMetaEnum>
#include <QPixmap>
#include <QPushButton>
#include <QVBoxLayout>
class MyMessageBox : public QDialog {
Q_OBJECT
public:
enum Icon {
NoIcon = 0,
Information = 1,
Question = 2,
Success = 3,
Warning = 4,
Critical = 5
Q_ENUM(Icon)
enum StandardButton {
NoButton = 0x00000000,
Ok = 0x00000400,
Save = 0x00000800,
SaveAll = 0x00001000,
Open = 0x00002000,
Yes = 0x00004000,
YesToAll = 0x00008000,
No = 0x00010000,
NoToAll = 0x00020000,
Abort = 0x00040000,
Retry = 0x00080000,
Ignore = 0x00100000,
Close = 0x00200000,
Cancel = 0x00400000,
Discard = 0x00800000,
Help = 0x01000000,
Apply = 0x02000000,
Reset = 0x04000000,
RestoreDefaults = 0x08000000,
FirstButton = Ok,
LastButton = RestoreDefaults
Q_ENUM(StandardButton)
Q_DECLARE_FLAGS(StandardButtons, StandardButton)
Q_FLAG(StandardButtons)
enum ButtonRole {
InvalidRole = -1,
AcceptRole,
RejectRole,
DestructiveRole,
ActionRole,
HelpRole,
YesRole,
NoRole,
ResetRole,
ApplyRole,
NRoles
explicit MyMessageBox(QWidget* parent = nullptr);
MyMessageBox(Icon icon, const QString& title, const QString& text, StandardButtons buttons,
QWidget* parent = nullptr);
~MyMessageBox();
void setTitle(const QString& title);
Icon icon() const;
void setIcon(Icon icon);
QPixmap iconPixmap() const;
void setIconPixmap(const QPixmap& pixmap);
QString text() const;
void setText(const QString& text);
StandardButtons standardButtons() const;
void setStandardButtons(StandardButtons buttons);
StandardButton standardButton(QAbstractButton* button) const;
QAbstractButton* button(StandardButton which) const;
ButtonRole buttonRole(QAbstractButton* button) const;
QAbstractButton* clickedButton() const;
static StandardButton information(QWidget* parent, const QString& text, QString title = "Message", StandardButtons buttons = Ok);
static StandardButton question(QWidget* parent, const QString& text, QString title = "Message", StandardButtons buttons = StandardButtons(Yes | No));
static StandardButton success(QWidget* parent, const QString& text, QString title = "Message", StandardButtons buttons = Ok);
static StandardButton warning(QWidget* parent, const QString& text, QString title = "Message", StandardButtons buttons = Ok);
static StandardButton critical(QWidget* parent, const QString& text, QString title = "Message", StandardButtons buttons = Ok);
static StandardButton showMessageBox(QWidget* parent, Icon icon, const QString& text, QString title, StandardButtons buttons);
static void setMessageBoxGeometry(QWidget* parent, MyMessageBox& msgBox);
private slots:
void slotPushButtonClicked(QAbstractButton* button);
private:
void init();
void setupLayout();
QPixmap standardIcon(Icon icon);
void setClickedButton(QAbstractButton* button);
void finalize(QAbstractButton* button);
int dialogCodeForButton(QAbstractButton* button) const;
void setPushButtonProperty(QList<QAbstractButton*> buttonList);
private:
QLabel* m_pIconLabel;
MyMessageBox::Icon m_icon;
QLabel* m_pTextLabel;
QLabel* m_pLineLabel;
QDialogButtonBox* m_pButtonBox;
QAbstractButton* m_pClickedButton;
Q_DECLARE_OPERATORS_FOR_FLAGS(MyMessageBox::StandardButtons)
#endif
Q_DECLARE_OPERATORS_FOR_FLAGS(MyMessageBox::StandardButtons)
#endif
MyMessageBox.cpp
#include "MyMessageBox.h"
#define MESSAGEWIDTH 450
#define MESSAGEHEIGHT 225
#define TEXTFONTFAMILY "微软雅黑"
#define TEXTFONTSIZE 12
#define BUTTONFONTFAMILY "微软雅黑"
#define BUTTONFONTSIZE 10
#define BUTTONWIDTH 100
#define BUTTONHEIGHT 30
MyMessageBox::MyMessageBox(QWidget* parent) :
QDialog(parent) {
init();
MyMessageBox::MyMessageBox(Icon icon, const QString& title, const QString& text, StandardButtons buttons, QWidget* parent) :
QDialog(parent) {
init();
setTitle(title);
setIcon(icon);
setText(text);
if (buttons != NoButton)
setStandardButtons(buttons);
MyMessageBox::~MyMessageBox() {}
void MyMessageBox::setTitle(const QString& title) {
setWindowTitle(title);
MyMessageBox::Icon MyMessageBox::icon() const {
return m_icon;
void MyMessageBox::setIcon(MyMessageBox::Icon icon) {
setIconPixmap(standardIcon(icon));
m_icon = icon;
QPixmap MyMessageBox::iconPixmap() const {
return *m_pIconLabel->pixmap();
void MyMessageBox::setIconPixmap(const QPixmap& pixmap) {
m_pIconLabel->setPixmap(pixmap);
m_icon = NoIcon;
QString MyMessageBox::text() const {
return m_pTextLabel->text();
void MyMessageBox::setText(const QString& text) {
m_pTextLabel->setFont(QFont(TEXTFONTFAMILY, TEXTFONTSIZE));
m_pTextLabel->setText(text);
MyMessageBox::StandardButtons MyMessageBox::standardButtons() const {
QDialogButtonBox::StandardButtons standardButtons = m_pButtonBox->standardButtons();
return MyMessageBox::StandardButtons(int(standardButtons));
void MyMessageBox::setStandardButtons(StandardButtons buttons) {
QDialogButtonBox::StandardButtons standardButtons = QDialogButtonBox::StandardButtons(int(buttons));
m_pButtonBox->setStandardButtons(standardButtons);
setPushButtonProperty(m_pButtonBox->buttons());
MyMessageBox::StandardButton MyMessageBox::standardButton(QAbstractButton* button) const {
QDialogButtonBox::StandardButton standardButton = m_pButtonBox->standardButton(button);
return (MyMessageBox::StandardButton)standardButton;
QAbstractButton* MyMessageBox::button(MyMessageBox::StandardButton which) const {
QDialogButtonBox::StandardButton standardButton = QDialogButtonBox::StandardButton(which);
return m_pButtonBox->button(standardButton);
MyMessageBox::ButtonRole MyMessageBox::buttonRole(QAbstractButton* button) const {
QDialogButtonBox::ButtonRole buttonRole = m_pButtonBox->buttonRole(button);
return MyMessageBox::ButtonRole(buttonRole);
QAbstractButton* MyMessageBox::clickedButton() const {
return m_pClickedButton;
MyMessageBox::StandardButton MyMessageBox::information(QWidget* parent, const QString& text, QString title, StandardButtons buttons) {
return showMessageBox(parent, Information, text, title, buttons);
MyMessageBox::StandardButton MyMessageBox::question(QWidget* parent, const QString& text, QString title, StandardButtons buttons) {
return showMessageBox(parent, Question, text, title, buttons);
MyMessageBox::StandardButton MyMessageBox::success(QWidget* parent, const QString& text, QString title, StandardButtons buttons) {
return showMessageBox(parent, Success, text, title, buttons);
MyMessageBox::StandardButton MyMessageBox::warning(QWidget* parent, const QString& text, QString title, StandardButtons buttons) {
return showMessageBox(parent, Warning, text, title, buttons);
MyMessageBox::StandardButton MyMessageBox::critical(QWidget* parent, const QString& text, QString title, StandardButtons buttons) {
return showMessageBox(parent, Critical, text, title, buttons);
MyMessageBox::StandardButton MyMessageBox::showMessageBox(QWidget* parent, Icon icon, const QString& text, QString title, StandardButtons buttons) {
MyMessageBox msgBox(icon, title, text, buttons, parent);
setMessageBoxGeometry(parent, msgBox);
if (msgBox.exec() == -1)
return MyMessageBox::Cancel;
return msgBox.standardButton(msgBox.clickedButton());
void MyMessageBox::setMessageBoxGeometry(QWidget* parent, MyMessageBox& msgBox) {
QRect rect = parent->geometry();
int x = rect.x() + (rect.width() - msgBox.geometry().width()) / 2;
int y = rect.y() + (rect.height() - msgBox.geometry().height()) / 2;
msgBox.setGeometry(x, y, msgBox.geometry().width(), msgBox.geometry().height());
msgBox.move(x, y);
void MyMessageBox::slotPushButtonClicked(QAbstractButton* button) {
setClickedButton(button);
finalize(button);
close();
void MyMessageBox::init() {
resize(QSize(MESSAGEWIDTH, MESSAGEHEIGHT));
setWindowIcon(QIcon(":/new/prefix1/Icon/project.ico"));
setWindowTitle("Message");
Qt::WindowFlags flags = Qt::Dialog;
flags |= Qt::WindowCloseButtonHint;
setWindowFlags(flags);
m_pIconLabel = new QLabel(this);
m_pIconLabel->setObjectName(QLatin1String("iconLabel"));
m_pIconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
m_pTextLabel = new QLabel(this);
m_pTextLabel->setObjectName(QLatin1String("textLabel"));
m_pTextLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
m_pTextLabel->setOpenExternalLinks(true);
m_pLineLabel = new QLabel(this);
m_pLineLabel->setFrameStyle(QFrame::HLine | QFrame::Sunken);
m_pButtonBox = new QDialogButtonBox(this);
m_pButtonBox->setObjectName(QLatin1String("buttonBox"));
connect(m_pButtonBox, &QDialogButtonBox::clicked, this, &MyMessageBox::slotPushButtonClicked);
setupLayout();
setModal(true);
setStyleSheet("QDialog{background-color:rgb(255 , 255 , 255);}"
"QPushButton{background-color:#E0E0E0;border: 1px solid #A6A6A6;border-radius:5px;}"
"QPushButton:hover{color:white;background-color:#4188FF;border-radius:5px;border: 0px}"
"QPushButton:pressed{padding-left:3px;padding-top:3px;}");
void MyMessageBox::setupLayout() {
QHBoxLayout* HLay = new QHBoxLayout;
HLay->addWidget(m_pIconLabel, 1, Qt::AlignVCenter | Qt::AlignRight);
HLay->addWidget(m_pTextLabel, 5, Qt::AlignCenter);
QHBoxLayout* HLay1 = new QHBoxLayout;
HLay1->addWidget(m_pButtonBox, Qt::AlignRight);
QVBoxLayout* VLay = new QVBoxLayout;
VLay->addLayout(HLay, 10);
VLay->addWidget(m_pLineLabel, 1);
VLay->addLayout(HLay1, 4);
VLay->setSpacing(0);
setLayout(VLay);
QPixmap MyMessageBox::standardIcon(Icon icon) {
QPixmap pixmap;
switch (icon) {
case MyMessageBox::Information:
pixmap.load(":/new/prefix1/Image/Information.png");
break;
case MyMessageBox::Question:
pixmap.load(":/new/prefix1/Image/Question.png");
break;
case MyMessageBox::Success:
pixmap.load(":/new/prefix1/Image/Success.png");
break;
case MyMessageBox::Warning:
pixmap.load(":/new/prefix1/Image/Warning.png");
break;
case MyMessageBox::Critical:
pixmap.load(":/new/prefix1/Image/Critical.png");
break;
default:
break;
if (!pixmap.isNull())
return pixmap;
return QPixmap();
void MyMessageBox::setClickedButton(QAbstractButton* button) {
m_pClickedButton = button;
void MyMessageBox::finalize(QAbstractButton* button) {
int dialogCode = dialogCodeForButton(button);
if (dialogCode == QDialog::Accepted) {
emit accept();
} else if (dialogCode == QDialog::Rejected) {
emit reject();
int MyMessageBox::dialogCodeForButton(QAbstractButton* button) const {
switch (buttonRole(button)) {
case MyMessageBox::AcceptRole:
case MyMessageBox::YesRole:
return QDialog::Accepted;
case MyMessageBox::RejectRole:
case MyMessageBox::NoRole:
return QDialog::Rejected;
default:
return -1;
void MyMessageBox::setPushButtonProperty(QList<QAbstractButton*> buttonList) {
for (int i = 0; i < buttonList.size(); i++) {
QPushButton* pushButton = qobject_cast<QPushButton*>(buttonList.at(i));
pushButton->setMinimumSize(BUTTONWIDTH, BUTTONHEIGHT);
pushButton->setFont(QFont(BUTTONFONTFAMILY, BUTTONFONTSIZE));
注意:
需要在Resources文件中自己手动添加icon图片。
QT 自定义提示框
Qt 自定义提示框 类似QMessageBox https://blog.csdn.net/qq_37373742/article/details/127402276?spm=1001.2014.3001.5502
为什么需要设计自定义提示框呢? 1.Qt自带的提示框样式单一; 2.提示框的太小; 3.界面风格跟项目的不搭配
public:
MyMessageBox(Icon icon,const QString & title, const QString & text, StandardButtons buttons = NoButton, QWidget * parent):QMessageBox(icon,tit.
简介:Qt5.6版本提供了强大的GUI功能,其中包括标准的MessageBox,但它可能不满足所有个性化界面需求。开发者可以通过继承QDialog或QMessageBox类来设计一个自定义的MessageBox,使用Qt的布局管理、控件装饰、信号与槽机制,以及QSS样式表来创建具有特定外观和行为的对话框。此指南涵盖从创建自定义对话框类到测试...
QDialog::exec():模态(应用程序级)窗口显示。exec() 先设置modal属性,而后调用 show() 显示对话框,最后启用事件循环。在用户关闭这个对话框之前,不能和同一应用程序中的其它窗口交互。第一行申请的栈空间,函数运行结束后内存释放,弹窗会闪退。但是窗口弹出后可以对其他窗口进行操作,不符合要求。将第四行换用dialog->exec()即可解决。换用第二行申请堆空间可解决。
本文详细的介绍了QMessageBox控件的各种操作,例如:消息提示框的使用、判断消息提示框的按钮、标准图标和自定义图标、定时关闭、自定义样式等操作。information:标准信息提示框。本文作者原创,转载请附上文章出处与本文链接。5. QMessageBox自定义样式。1. 标准信息提示框。2. 判断提示框按钮。3. 提示框自带图标。4. 定时关闭提示框。
在qt quick中,可以使用Popup组件来轻松创建弹出窗口。最后,在弹出窗口内部,我们使用Rectangle和Text组件实现了弹出窗口的基本UI,并添加了一个Button组件,用于关闭弹出窗口。在这个例子中,我们首先创建了一个按钮,当用户点击该按钮时,弹出窗口将显示出来。在这个例子中,我们设置了modal属性为true,这将使弹出窗口成为模态的,阻止用户与其他应用程序部分交互,直到关闭弹出窗口。总之,在qt quick中,使用Popup组件可以很容易地创建自定义的弹出窗口,并实现一些简单的用户交互。