.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();
}