QMessageBox类
- 静态方法
- 例子
静态方法
调用这一些静态成员函数,就可以得到模态提示框
枚举值为:
例子
头文件:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
QMessageBox* mb;
private slots:
void on_msgbox_clicked();
void on_aboutQt_clicked();
void on_critical_clicked();
void on_information_clicked();
void on_question_clicked();
void on_warning_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
cpp文件:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QIcon"
#include "QDebug"
#include "QString"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QString wintitle = "title";
this->setWindowTitle(wintitle);
QString pic = "E:\\Code\\Qt\\myQWidget\\a.png";
QIcon *qicon = new QIcon(pic);
this->setWindowIcon(*qicon);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_msgbox_clicked()
{
QMessageBox::about(this,"这是标题","内容为:这是一个错误提示框");
}
void MainWindow::on_aboutQt_clicked()
{
QMessageBox::aboutQt(this,"这是aboutQT的标题");
}
void MainWindow::on_critical_clicked()
{
int res = QMessageBox::critical(this, "这是critical的标题", "内容为:这是一个critical提示框",
QMessageBox::Open,
QMessageBox::Ok);
qDebug()<<res;
}
void MainWindow::on_information_clicked()
{
QMessageBox::information(this,"这是information的标题", "内容为:这是一个information提示框",
QMessageBox::SaveAll,
QMessageBox::Help);
}
void MainWindow::on_question_clicked()
{
int res = QMessageBox::question(this,"这是question的标题", "内容为:这是一个question提示框",
QMessageBox::Yes|QMessageBox::No,
QMessageBox::No);
if(res==QMessageBox::Yes){
QMessageBox::warning(this,"YES", "成功选中YES",
QMessageBox::Reset,
QMessageBox::Help);
}else if(res = QMessageBox::No){
QMessageBox::warning(this,"NO", "成功选中NO",
QMessageBox::Reset,
QMessageBox::Help);
}
}
void MainWindow::on_warning_clicked()
{
QMessageBox::warning(this,"这是warning的标题", "内容为:这是一个warning提示框",
QMessageBox::Reset,
QMessageBox::Help);
}