【QT】如何自定义QMessageBox的窗口大小,通过继承QDialog重新实现美观的弹窗

目录

  • 1. QMessageBox原有的弹窗
  • 2. 网上第一种方法:通过样式表setStyleSheet实现改变弹窗大小(总体不美观)
  • 3. 网上第二种方法:重写ShowEvent()改变弹窗大小(总体也不美观)
  • 4. 最好的办法:继承QDialog重新实现弹窗界面(附完整代码)(v1.0)
  • 5. v1.0的改进:重新实现弹窗界面(附完整代码)(v2.0)

1. QMessageBox原有的弹窗

  QMessageBox messageBox(QMessageBox::Question, "提示", "是否保存当前项目?", QMessageBox::Yes | QMessageBox::No);messageBox.exec();

在这里插入图片描述
可以看出QMessageBox原有的弹窗看起来非常的不美观,有时候大有时候小,只能使用QMessageBox自带的图标,而且不能自定义窗口的大小,那是因为在源码中将其弹窗大小设置成了比较合适的大小,所以不能自定义改变弹窗大小,如下所示代码都是不起作用的:

   messageBox.setGeometry(800, 300, 450, 225);  //(500,300)设置其位置是有效的,但是设置其大小(450, 225)是无效的messageBox.resize(450, 225);                 //resize大小也是无效的

2. 网上第一种方法:通过样式表setStyleSheet实现改变弹窗大小(总体不美观)

  QMessageBox messageBox(QMessageBox::Question, "提示", "是否保存当前项目?", QMessageBox::Yes | QMessageBox::No);messageBox.setStyleSheet("QLabel{""min-width:150px;""min-height:120px;""font-size:16px;""}");messageBox.exec();

在这里插入图片描述
可以看出通过样式表的方法也不太美观,其中text没有居中。

3. 网上第二种方法:重写ShowEvent()改变弹窗大小(总体也不美观)

先上代码实现一下:
MyMessageBox.h

#include <QLabel>
#include <QMessageBox>
#include <QWidget>class MyMessageBox : public QMessageBox {Q_OBJECTpublic:MyMessageBox(Icon icon, const QString& title, const QString& text,StandardButtons buttons, QWidget* parent = 0);~MyMessageBox();protected:void showEvent(QShowEvent* event) override;
};

MyMessageBox.cpp

#include "mymessagebox.h"MyMessageBox::MyMessageBox(Icon icon, const QString& title, const QString& text,StandardButtons buttons, QWidget* parent) :QMessageBox(icon, title, text, buttons, parent) {
}MyMessageBox::~MyMessageBox() {
}void MyMessageBox::showEvent(QShowEvent* event) {QWidget* textLabel = findChild<QWidget*>("qt_msgbox_label");       //获取源码中text的label组件QWidget* iconLabel = findChild<QWidget*>("qt_msgbox_icon_label");  //获取源码中icon的label组件if (textLabel != nullptr) {  //使用指针之前先判断是否为空textLabel->setMinimumSize(450, 255);// textLabel->setFixedSize(450, 255);}if (iconLabel != nullptr) {iconLabel->setMinimumHeight(255);iconLabel->setFixedSize(450, 255);}QMessageBox::showEvent(event);
}

main.cpp

#include <QApplication>#include "mymessagebox.h"int main(int argc, char* argv[]) {QApplication a(argc, argv);MyMessageBox messageBox(QMessageBox::Question, "提示", "是否保存当前项目?", QMessageBox::Yes | QMessageBox::No);messageBox.exec();//w.show();return a.exec();
}

解释一下上面的一些代码
先看一下QMessageBox中的一些源码:
在这里插入图片描述
在对某一个组件设置了setObjectName()属性之后,我们可以通过objectName在外面获得这个组件,如下:

//QLabel继承于QWidget,所以用QWidget*是可以的
QWidget* textLabel = findChild<QWidget*>("qt_msgbox_label");  //获取源码中text的label组件

然后对label组件的大小进行调整,达到调整弹窗大小的目的。可以看到弹窗的大小是改变了,但是还是不美观,主要是图标没有居中,还是处在左上角的位置,如下图所示:

在这里插入图片描述
那对于上面这个问题出现的原因是什么呢?那就看一下QMessageBox中的关于布局Layout的一些源码:
在这里插入图片描述
可以看出在布局时,其icon总是处在第0行第0列的位置,且其Aliment设置的是Top,所以导致了QMessageBox的图标总是在左上角。
所以重写ShowEvent()这种方法也达不到实现美观的弹窗的预期。

4. 最好的办法:继承QDialog重新实现弹窗界面(附完整代码)(v1.0)

重新写一个弹窗TipDialog类,继承于QDialog类。
先看效果:
在这里插入图片描述
直接上代码:
TipDialog.h

#ifndef TIPDIALOG_H
#define TIPDIALOG_H#include <QDialog>
#include <QDialogButtonBox>
#include <QHBoxLayout>
#include <QIcon>
#include <QLabel>
#include <QMessageBox>
#include <QMetaEnum>
#include <QPixmap>
#include <QPushButton>
#include <QVBoxLayout>class TipDialog : public QDialog {Q_OBJECTpublic:enum TipIcon {NoIcon = 0,Information = 1,Question = 2,Success = 3,Warning = 4,Critical = 5};Q_ENUM(TipIcon)  //使用Q_ENUM注册枚举enum StandardButton {//保持与 QDialogButtonBox::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 {//保持与 QDialogButtonBox::ButtonRole 一致InvalidRole = -1,AcceptRole,RejectRole,DestructiveRole,ActionRole,HelpRole,YesRole,NoRole,ResetRole,ApplyRole,NRoles};explicit TipDialog(QWidget* parent = nullptr);TipDialog(TipIcon icon, const QString& title, const QString& text, StandardButtons buttons,QWidget* parent = nullptr);  //构造函数有默认值的要放后面~TipDialog();static void information(QWidget* parent, const QString& title, const QString& text, StandardButtons buttons = Ok);static void question(QWidget* parent, const QString& title, const QString& text, StandardButtons buttons = StandardButtons(Yes | No));static void success(QWidget* parent, const QString& title, const QString& text, StandardButtons buttons = Ok);static void warning(QWidget* parent, const QString& title, const QString& text, StandardButtons buttons = Ok);static void critical(QWidget* parent, const QString& title, const QString& text, StandardButtons buttons = Ok);private:void init(const QString& title = QString(), const QString& text = QString());void setupLayout();QPixmap standardIcon(TipDialog::TipIcon icon);void setIcon(TipIcon icon);void createButton(StandardButton button);void createStandardButtons(StandardButtons buttons);void setStandardButtons(StandardButtons buttons);//    QString standardTitle(TipDialog::TipIcon icon);QString getEnumKey(StandardButton button);void addButton(StandardButton button, QDialogButtonBox::ButtonRole buttonRole);private:QLabel* m_pIconLabel;QLabel* m_pTextLabel;QDialogButtonBox* m_pButtonBox;//    QList<QPushButton*> m_pButtonList;QString m_text;
};//在全局任意地方使用"|"操作符计算自定义的枚举量,需要使用Q_DECLARE_OPERATORS_FOR_FLAGS宏
Q_DECLARE_OPERATORS_FOR_FLAGS(TipDialog::StandardButtons)#endif  // TIPDIALOG_H

TipDialog.cpp

#include "tipdialog.h"#define fontFamily "宋体"
#define fontSize 12TipDialog::TipDialog(QWidget* parent) :QDialog(parent) {init();
}TipDialog::TipDialog(TipIcon icon, const QString& title, const QString& text, StandardButtons buttons, QWidget* parent) :QDialog(parent) {init(title, text);setWindowTitle(title);setIcon(icon);setStandardButtons(buttons);//    setupLayout();
}TipDialog::~TipDialog() {}void TipDialog::information(QWidget* parent, const QString& title, const QString& text, StandardButtons buttons) {TipDialog tip(Information, title, text, buttons, parent);tip.exec();
}void TipDialog::question(QWidget* parent, const QString& title, const QString& text, StandardButtons buttons) {TipDialog tip(Question, title, text, buttons, parent);tip.exec();
}void TipDialog::success(QWidget* parent, const QString& title, const QString& text, StandardButtons buttons) {TipDialog tip(Success, title, text, buttons, parent);tip.exec();
}void TipDialog::warning(QWidget* parent, const QString& title, const QString& text, StandardButtons buttons) {TipDialog tip(Warning, title, text, buttons, parent);tip.exec();
}void TipDialog::critical(QWidget* parent, const QString& title, const QString& text, StandardButtons buttons) {TipDialog tip(Critical, title, text, buttons, parent);tip.exec();
}void TipDialog::init(const QString& title, const QString& text) {resize(QSize(450, 225));setWindowIcon(QIcon(":/new/prefix1/Icon/项目.png"));setWindowTitle("operation tip...");setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint);  //设置右上角按钮//setAttribute(Qt::WA_DeleteOnClose);                                   //关闭窗口时将窗口释放掉即释放内存m_pIconLabel = new QLabel(this);m_pIconLabel->setObjectName(QLatin1String("iconLabel"));m_pTextLabel = new QLabel(this);m_pTextLabel->setObjectName(QLatin1String("textLabel"));m_pTextLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);  //label中的内容可用鼠标选择文本复制,链接激活m_pTextLabel->setOpenExternalLinks(true);                           //label中的内容若为链接,可直接点击打开m_pButtonBox = new QDialogButtonBox(this);m_pButtonBox->setObjectName(QLatin1String("buttonBox"));if (!title.isEmpty() || !text.isEmpty()) {setWindowTitle(title);m_pTextLabel->setFont(QFont(fontFamily, fontSize));m_pTextLabel->setText(text);}m_text = text;setupLayout();
}void TipDialog::setupLayout() {QHBoxLayout* HLay = new QHBoxLayout;HLay->addWidget(m_pIconLabel, 1, Qt::AlignVCenter | Qt::AlignRight);HLay->addWidget(m_pTextLabel, 5, Qt::AlignCenter);QVBoxLayout* VLay = new QVBoxLayout;VLay->addLayout(HLay);VLay->addWidget(m_pButtonBox);setLayout(VLay);
}QPixmap TipDialog::standardIcon(TipDialog::TipIcon icon) {QPixmap pixmap;switch (icon) {case TipDialog::Information:pixmap.load(":/new/prefix1/Image/Information.png");break;case TipDialog::Question:pixmap.load(":/new/prefix1/Image/Question.png");break;case TipDialog::Success:pixmap.load(":/new/prefix1/Image/Success.png");break;case TipDialog::Warning:pixmap.load(":/new/prefix1/Image/Warning.png");break;case TipDialog::Critical:pixmap.load(":/new/prefix1/Image/Critical.png");break;default:break;}if (!pixmap.isNull())return pixmap;return QPixmap();
}void TipDialog::setIcon(TipDialog::TipIcon icon) {m_pIconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);m_pIconLabel->setPixmap(standardIcon(icon));
}void TipDialog::createButton(StandardButton button) {switch (button) {case TipDialog::Ok:addButton(button, QDialogButtonBox::AcceptRole);break;case TipDialog::Save:addButton(button, QDialogButtonBox::AcceptRole);break;case TipDialog::Yes:addButton(button, QDialogButtonBox::YesRole);break;case TipDialog::No:addButton(button, QDialogButtonBox::NoRole);break;case TipDialog::Cancel:addButton(button, QDialogButtonBox::RejectRole);break;default:break;}
}
void TipDialog::createStandardButtons(StandardButtons buttons) {//TipDialog::StandardButtons 与 QDialogButtonBox::StandardButtons 的枚举是一样的uint i = QDialogButtonBox::FirstButton;  //枚举值为非负整数,使用uintwhile (i <= QDialogButtonBox::LastButton) {if (i & buttons) {createButton(StandardButton(i));}i = i << 1;}
}void TipDialog::setStandardButtons(StandardButtons buttons) {if (buttons != NoButton)createStandardButtons(buttons);
}//QString TipDialog::standardTitle(TipDialog::TipIcon icon) {
//    QString title;
//    switch (icon) {
//        case TipDialog::Information:
//            title = "Information";
//            break;
//        case TipDialog::Question:
//            title = "Question";
//            break;
//        case TipDialog::Success:
//            title = "Success";
//            break;
//        case TipDialog::Warning:
//            title = "Warning";
//            break;
//        case TipDialog::Critical:
//            title = "Critical";
//            break;
//        default:
//            break;
//    }
//    if (!title.isEmpty())
//        return title;
//    return QString();
//}QString TipDialog::getEnumKey(StandardButton button) {QMetaEnum metaEnum = QMetaEnum::fromType<StandardButton>();  //获取QMetaEnum对象,为了获取枚举值QString str = metaEnum.valueToKey(button);if (!str.isEmpty())return str;return QString();
}void TipDialog::addButton(TipDialog::StandardButton button, QDialogButtonBox::ButtonRole buttonRole) {QString buttonText = getEnumKey(button);QPushButton* pushButton = m_pButtonBox->addButton(buttonText, buttonRole);//    if (pushButton->text() == QLatin1String("Yes") && m_text.contains("save", Qt::CaseSensitive)) {//        pushButton->setText("Save");//    } else if (pushButton->text() == QLatin1String("Yes") && m_text.contains("delete", Qt::CaseSensitive)) {//        pushButton->setText("Delete");//    }//    m_pButtonList.append(pushButton);connect(pushButton, &QPushButton::clicked, this, &TipDialog::close);
}

main.cpp

#include <QApplication>
#include <QMessageBox>#include "tipdialog.h"int main(int argc, char* argv[]) {QApplication a(argc, argv);//TipDialog aa(TipDialog::Question, "Question", "The current project has been modified.\nDo you want to save it?", TipDialog::Yes | TipDialog::No);//TipDialog aa(TipDialog::Success, "Success", "Project saved successfully!", TipDialog::Ok);//TipDialog aa(TipDialog::Warning, "Warning", "Parsed file not found! Please reconfigure!", TipDialog::Ok);TipDialog aa(TipDialog::Critical, "Critical", "Loading failed.Please tryagain!", TipDialog::Ok);aa.show();return a.exec();
}

注意:需要在Resources文件中自己手动添加icon图片。

5. v1.0的改进:重新实现弹窗界面(附完整代码)(v2.0)

增加了许多接口,并且之前v1.0版本添加按钮是自己实现的,其实不需要,直接使用QDialogButtonBox的接口会更方便。
MyMessageBox.h

#ifndef MyMessageBox_H
#define MyMessageBox_H#include <QDebug>
#include <QDialog>
#include <QDialogButtonBox>
#include <QHBoxLayout>
#include <QIcon>
#include <QLabel>
#include <QMetaEnum>
#include <QPixmap>
#include <QPushButton>
#include <QVBoxLayout>class MyMessageBox : public QDialog {Q_OBJECTpublic:enum Icon {NoIcon = 0,Information = 1,Question = 2,Success = 3,Warning = 4,Critical = 5};Q_ENUM(Icon)  //使用Q_ENUM注册枚举enum StandardButton {//保持与 QDialogButtonBox::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 {//保持与 QDialogButtonBox::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);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;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宏
Q_DECLARE_OPERATORS_FOR_FLAGS(MyMessageBox::StandardButtons)#endif  // MyMessageBox_H

MyMessageBox.cpp

#include "MyMessageBox.h"#define fontFamily "微软雅黑"
#define fontSize 12MyMessageBox::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);//    setupLayout();
}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->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);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(fontFamily, fontSize));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);//    m_pButtonBox->setStandardButtons(QDialogButtonBox::StandardButtons(int(buttons)));  //上面两句归为一句
}MyMessageBox::StandardButton MyMessageBox::standardButton(QAbstractButton* button) const {QDialogButtonBox::StandardButton standardButton = m_pButtonBox->standardButton(button);return (MyMessageBox::StandardButton)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) {MyMessageBox msgBox(Information, title, text, buttons, parent);if (msgBox.exec() == -1)return MyMessageBox::Cancel;return msgBox.standardButton(msgBox.clickedButton());
}MyMessageBox::StandardButton MyMessageBox::question(QWidget* parent, const QString& text, QString title, StandardButtons buttons) {MyMessageBox msgBox(Question, title, text, buttons, parent);if (msgBox.exec() == -1)return MyMessageBox::Cancel;return msgBox.standardButton(msgBox.clickedButton());
}MyMessageBox::StandardButton MyMessageBox::success(QWidget* parent, const QString& text, QString title, StandardButtons buttons) {MyMessageBox msgBox(Success, title, text, buttons, parent);if (msgBox.exec() == -1)return MyMessageBox::Cancel;return msgBox.standardButton(msgBox.clickedButton());
}MyMessageBox::StandardButton MyMessageBox::warning(QWidget* parent, const QString& text, QString title, StandardButtons buttons) {MyMessageBox msgBox(Warning, title, text, buttons, parent);if (msgBox.exec() == -1)return MyMessageBox::Cancel;return msgBox.standardButton(msgBox.clickedButton());
}MyMessageBox::StandardButton MyMessageBox::critical(QWidget* parent, const QString& text, QString title, StandardButtons buttons) {MyMessageBox msgBox(Critical, title, text, buttons, parent);if (msgBox.exec() == -1)return MyMessageBox::Cancel;return msgBox.standardButton(msgBox.clickedButton());
}void MyMessageBox::slotPushButtonClicked(QAbstractButton* button) {setClickedButton(button);finalize(button);close();
}void MyMessageBox::init() {resize(QSize(450, 225));setWindowIcon(QIcon(":/new/prefix1/Icon/project.ico"));setWindowTitle("Message");setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint);  //设置右上角按钮//setAttribute(Qt::WA_DeleteOnClose);                                   //关闭窗口时将窗口释放掉即释放内存m_pIconLabel = new QLabel(this);m_pIconLabel->setObjectName(QLatin1String("iconLabel"));m_pTextLabel = new QLabel(this);m_pTextLabel->setObjectName(QLatin1String("textLabel"));m_pTextLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);  //label中的内容可用鼠标选择文本复制,链接激活m_pTextLabel->setOpenExternalLinks(true);                           //label中的内容若为链接,可直接点击打开m_pLineLabel = new QLabel(this);m_pLineLabel->setFrameStyle(QFrame::HLine | QFrame::Sunken);  //Sunken:凹陷,Raised:凸起m_pButtonBox = new QDialogButtonBox(this);m_pButtonBox->setObjectName(QLatin1String("buttonBox"));connect(m_pButtonBox, &QDialogButtonBox::clicked, this, &MyMessageBox::slotPushButtonClicked);setupLayout();
}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);//    QMargins margin;//    margin.setRight(9);//    HLay1->setContentsMargins(margin);  //调节按钮不要太靠右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 accepted();} else if (dialogCode == QDialog::Rejected) {emit rejected();}
}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;}
}

注意:需要在Resources文件中自己手动添加icon图片。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/2009.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

webstorm2022 TS1109: Expression expected.

在使用webstorm2022&#xff0c;加入ESLint&#xff08;已禁用&#xff09;的情况下&#xff0c;编写vue3的typescript代码时&#xff0c;报错&#xff1a; TS1109: Expression expected. 原因&#xff1a;2022版本不支持volar&#xff0c;需升级到2023版本。 官方描述为&am…

IAM风险CTF挑战赛

wiz启动了一个名为“The Big IAM Challenge”云安全CTF挑战赛。旨在让白帽子识别和利用 IAM错误配置&#xff0c;并从现实场景中学习&#xff0c;从而更好的认识和了解IAM相关的风险。比赛包括6个场景&#xff0c;每个场景都专注于各种AWS服务中常见的IAM配置错误。 Challenge…

Linux下vim的常见命令操作(快速复查)

目录 前言1、Vim常用操作1.1、环境参数1.2、方向1.3、插入命令1.4、定位命令1.5、删除命令1.6、复制和剪切命令1.7、替换和取消命令1.8、搜索和搜索替换命令1.9、保存和退出命令1.10、其他命令1.11、可视模式 前言 本篇文章不面向新手&#xff0c;全文几乎都是命令&#xff0c;…

使用python编程数学建模-常见excel数据使用python以行的方式按需读取

读取原始数据 首先导入pandas库   接着使用pandas库里面的read_csv方法来读取我们的文件&#xff0c;由于数据文件和程序文件是在统一目录下&#xff0c;因此无需使用绝对路径 import pandas as pd data1 pd.read_csv("data1.csv")读取数据的前20行数据 这里我们…

Java Web HTMLCSS(1)23.6.29

HTML&CSS 1&#xff0c;HTML 1.1 介绍 HTML 是一门语言&#xff0c;所有的网页都是用HTML 这门语言编写出来的&#xff0c;也就是HTML是用来写网页的&#xff0c;像京东&#xff0c;12306等网站有很多网页。 这些都是网页展示出来的效果。而HTML也有专业的解释 HTML(Hy…

【软件测试】Java+selenium环境搭建

目录 1.下载Chrome浏览器&#xff0c;查看浏览器的版本 2.根据浏览器版本下载驱动 根据电脑版本下载驱动&#xff1a; 3.去maven仓库寻找selenium驱动 4.在idea中创建一个项目 1.在pom.xml中添加依赖 2.点击右侧刷新按钮 3.在Java下创建一个类Main 4.将以下代码写入 5.…

Redis【Redis数据类型(String、List、Set、Hash 、Zset)】(二)-全面详解(学习总结---从入门到深化)

目录 Redis数据类型_String set get append strlen setex setnx getrange setrange incr decr incrby/decrby key step mset mget getset Redis数据类型_List lrange lpop/rpop lindex llen lrem linsert lset Redis数据类型_Set smembers sism…

【并发编程】Java的Future机制详解(Future接口和FutureTask类)

目录 一、彻底理解Java的Future模式 二、为什么出现Future机制 2.1 Future 类有什么用&#xff1f; 三、Future的相关类图 2.1 Future 接口 2.2 FutureTask 类 五、FutureTask源码分析 5.1 state字段 5.2 其他变量 5.3 CAS工具初始化 5.4 构造函数 5.5 jdk1.8和之前…

轻量服务器外网访问不了的原因分析

​  轻量服务器外网访问不了原因的分析。很多用户在选择轻量服务器的时候都没考虑&#xff0c;直接就购买了&#xff0c;导致在使用的时候遇见了很多问题&#xff0c;下面我们就简单的聊聊关于轻量服务器外网无法访问的原因。 这里我们按照标题的意思可以解读为两种情况&…

基于Servlet+JDBC实现的基础博客系统>>系列2 -- 前端基础页面

目录 1. 博客公共页面样式 2. 博客列表页 3. 博客详情页 4. 博客登录页 5. 博客编辑页 1. 博客公共页面样式 导航栏以及背景图设置 <body> <!-- 1.navigation 导航栏 --><div class"nav"><!-- logo --><img src"image/logo.png&q…

JAVA http

javahttp 请求数据格式servletservlet生命周期servletrequest获取请求数据解决乱码response相应字符&字节数据 请求数据格式 servlet servlet生命周期 servlet request获取请求数据 解决乱码 response相应字符&字节数据 response.setHeader("content-type",…

Modbus协议学习方法

在刚开始接触modbus协议的时候&#xff0c;很容易被里面的各种功能码搞晕&#xff0c;同时在编写程序的时候也容易搞不清楚每一位数据代表的含义。如果在学习的过程中有实际的发送和接收数据的例子话&#xff0c;那么理解modbus协议就会更容易一些。   下面我将自己借助软件学…