QT 自定义QMessageBox

.h文件

#ifndef NEWMESSAGEBOX_H
#define NEWMESSAGEBOX_H

#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QLayout>
class NewMessageBox : public QDialog
{
    Q_OBJECT
public:
    static int creatQuestionMessage(QWidget *parent = nullptr,
                  QString confirm_str = "Y", QString cancel_str = "N",
                                    QString content = "");
signals:
private slots:
    void on_m_btn_confirm_clicked();
    void on_m_btn_cancel_clicked();
private:
    explicit NewMessageBox(QWidget *parent,
                           QString confirm_str, QString cancel_str
                           ,QString content);
    QPushButton *m_btn_confirm;
    QPushButton *m_btn_cancel;
    QLabel *m_lab_icon;
    QLabel *m_lab_text;

};

#endif // NEWMESSAGEBOX_H

.cpp 文件

#include "newmessagebox.h"

NewMessageBox::NewMessageBox(QWidget *parent, QString confirm_str, QString cancel_str, QString content) : QDialog(parent)
{
    m_btn_confirm = new QPushButton(this);
    m_btn_cancel = new QPushButton(this);
    m_lab_icon = new QLabel(this);
    m_lab_text = new QLabel(this);

    m_btn_confirm->setText(confirm_str);
    m_btn_confirm->setMinimumSize(QSize(80, 50));
    m_btn_cancel->setText(cancel_str);
    m_btn_cancel->setMinimumSize(QSize(80, 50));

    m_lab_text->setText(content);
    QFont font;
    font.setPixelSize(20);
    m_lab_text->setFont(font);
    m_lab_text->setAlignment(Qt::AlignCenter);
    QPixmap pixmap(":/png/png/warning.png");
    m_lab_icon->setPixmap(pixmap);
    m_lab_icon->setScaledContents(true);

    QHBoxLayout *layout_btn = new QHBoxLayout;
    layout_btn->addWidget(m_btn_confirm, 0);
    layout_btn->addWidget(m_btn_cancel, 1);
    layout_btn->setStretch(0,1);
    layout_btn->setStretch(1,1);

    QVBoxLayout *layout_btn_text = new QVBoxLayout;
    layout_btn_text->addWidget(m_lab_text, 0);
    layout_btn_text->addLayout(layout_btn, 1);
    layout_btn_text->setStretch(0,3);
    layout_btn_text->setStretch(1,1);

    QHBoxLayout *m_widget_latout = new QHBoxLayout;
    m_widget_latout->addWidget(m_lab_icon, 0);
    m_widget_latout->addLayout(layout_btn_text, 1);
    m_widget_latout->setStretch(0,1);
    m_widget_latout->setStretch(1,1);

    this->setLayout(m_widget_latout);
    this->setMinimumSize(380,200);
    this->resize(380, 200);
    this->setWindowTitle("警告");

    /*窗口关闭时,delete串口的内存*/
    this->setAttribute(Qt::WA_DeleteOnClose);

    connect(m_btn_confirm, &QPushButton::clicked, this, &NewMessageBox::on_m_btn_confirm_clicked);
    connect(m_btn_cancel, &QPushButton::clicked, this, &NewMessageBox::on_m_btn_cancel_clicked);
}

int NewMessageBox::creatQuestionMessage(QWidget *parent, QString confirm_str, QString cancel_str, QString content)
{
    NewMessageBox *msg_box = new NewMessageBox(parent, confirm_str, cancel_str, content);
    return msg_box->exec();
}

void NewMessageBox::on_m_btn_confirm_clicked()
{
    done(1);	//= accept();  更改exec()函数的返回值
    this->close();
}

void NewMessageBox::on_m_btn_cancel_clicked()
{
    done(0);  //= reject();
    this->close();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值