QT学习第32天-QMessageBox的使用

 

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void on_pushButton_clicked();

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

 

#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QMessageBox>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
    QMessageBox messageBox;
    messageBox.setIcon(QMessageBox::Question);
    messageBox.setText("你确定要关闭窗体吗");
    messageBox.setWindowTitle("提示");
    QPushButton *yesbutton=messageBox.addButton("",messageBox.YesRole);
    QPushButton *nobutton=messageBox.addButton("",messageBox.NoRole);
    int res=messageBox.exec();
    if(messageBox.clickedButton()==yesbutton)
    {
        qDebug()<<"点击yes"<<endl;
    }
    else {
        qDebug()<<"点击no"<<endl;
    }
}

 

 

posted @ 2024-05-08 22:26  WellMandala  阅读(22)  评论(0编辑  收藏  举报