当前位置: 首页 > news >正文

qt-11基本对话框(消息框)

基本对话框--消息框

  • msgboxdlg.h
  • msgboxdlg.cpp
  • main.cpp
  • 运行图
    • QustionMsg
    • InFormationMsg
    • WarningMsg
    • CriticalMsg
    • AboutMsg
    • AboutAtMsg
    • 自定义

msgboxdlg.h

#ifndef MSGBOXDLG_H
#define MSGBOXDLG_H#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QGridLayout>
#include <QMessageBox>class msgboxdlg : public QDialog
{Q_OBJECTpublic:msgboxdlg(QWidget *parent = nullptr);~msgboxdlg();
private slots:void ShowQustionMsg();void ShowInFormationMsg();void ShowWarningMsg();void ShowCriticalMsg();void ShowAboutMsg();void ShowAboutAtMsg();void ShowCustomMsg();
private:QLabel* Label;QPushButton* QuestionBtn;QPushButton* InformationBtn;QPushButton* WarningBtn;QPushButton* CriticalBtn;QPushButton* AboutBtn;QPushButton* AboutQtBtn;QPushButton* CustomBtn;QGridLayout* MainLayout;};
#endif // MSGBOXDLG_H

msgboxdlg.cpp

#include "msgboxdlg.h"msgboxdlg::msgboxdlg(QWidget *parent): QDialog(parent)
{setWindowTitle(tr("标准消息对话框实例"));//实例化控件对象Label = new QLabel(tr("请选择一种消息框"));QuestionBtn = new QPushButton(tr("QuestionMsg"));InformationBtn = new QPushButton(tr("InformationMsg"));WarningBtn = new QPushButton(tr("WarningMsg"));CriticalBtn = new QPushButton(tr("CriticalMsg"));AboutBtn = new QPushButton(tr("AboutMsg"));AboutQtBtn = new QPushButton(tr("AboutQtMsg"));CustomBtn = new QPushButton(tr("CustomMsg"));//布局MainLayout = new QGridLayout(this);MainLayout->addWidget(Label,0,0,1,2);MainLayout->addWidget(QuestionBtn,1,0);MainLayout->addWidget(InformationBtn,1,1);MainLayout->addWidget(WarningBtn,2,0);MainLayout->addWidget(CriticalBtn,2,1);MainLayout->addWidget(AboutBtn,3,0);MainLayout->addWidget(AboutQtBtn,3,1);MainLayout->addWidget(CustomBtn,4,0,1,2);//事件关联connect(QuestionBtn,SIGNAL(clicked()),this,SLOT(ShowQustionMsg()));connect(InformationBtn,SIGNAL(clicked()),this,SLOT(ShowInFormationMsg()));connect(WarningBtn,SIGNAL(clicked()),this,SLOT(ShowWarningMsg()));connect(CriticalBtn,SIGNAL(clicked()),this,SLOT(ShowCriticalMsg()));connect(AboutBtn,SIGNAL(clicked()),this,SLOT(ShowAboutMsg()));connect(AboutQtBtn,SIGNAL(clicked()),this,SLOT(ShowAboutAtMsg()));connect(CustomBtn,SIGNAL(clicked()),this,SLOT(ShowCustomMsg()));}msgboxdlg::~msgboxdlg() {}void msgboxdlg::ShowQustionMsg()
{Label->setText(tr("Question Message box"));switch(QMessageBox::question(this,tr("Question 消息框"),tr("您现在已经修改完成,是否要结束程序?"),QMessageBox::Ok|QMessageBox::Cancel,QMessageBox::Ok)){case QMessageBox::Ok:Label->setText(tr("Question button/ok"));break;case QMessageBox::Cancel:Label->setText(tr("Question button/cancel"));break;default:break;}return;
}void msgboxdlg::ShowInFormationMsg()
{Label->setText(tr("Information Message Box"));QMessageBox::information(this,tr("Information消息框"),tr("这是Information消息框测试,欢迎您!"));return;
}void msgboxdlg::ShowWarningMsg()
{Label->setText(tr("warning message box"));switch(QMessageBox::warning(this,tr("warning 消息框"),tr("您修改的文档内容还未保存,是否要保存文档"), QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel,QMessageBox::Save)){case QMessageBox::Save:Label->setText("Save");break;case QMessageBox::Discard:Label->setText("Discard");break;case QMessageBox::Cancel:Label->setText("Cancel");break;default:break;}return;}void msgboxdlg::ShowCriticalMsg()
{Label->setText(tr("Critical MessBox "));QMessageBox::critical(this,tr("Critical 消息框"),tr("这是一个critical 消息框"));return;
}void msgboxdlg::ShowAboutMsg()
{Label->setText(tr("about MessBox "));QMessageBox::about(this,tr("about 消息框"),tr("这是一个about 消息框"));return;
}void msgboxdlg::ShowAboutAtMsg()
{Label->setText(tr("aboutQt MessBox "));QMessageBox::aboutQt(this,tr("aboutQt 消息框"));return;
}void msgboxdlg::ShowCustomMsg()
{Label->setText(tr("Custom 消息框"));QMessageBox CustomBox;CustomBox.setWindowTitle(tr("自定义消息框"));QPushButton* YesBtn = CustomBox.addButton(tr("yes"),QMessageBox::ActionRole);QPushButton* NoBtn = CustomBox.addButton(tr("No"),QMessageBox::ActionRole);QPushButton* Cancel = CustomBox.addButton(QMessageBox::Cancel);CustomBox.setText(tr("这是一个用户自定义消息框"));CustomBox.setIconPixmap(QPixmap("312.png"));CustomBox.exec();if(CustomBox.clickedButton() == YesBtn){Label->setText(tr("Custom YesBtn消息"));return;}if(CustomBox.clickedButton() == NoBtn){Label->setText(tr("Custom NoBtn消息"));return;}if(CustomBox.clickedButton() == Cancel){Label->setText(tr("Custom Cancel消息"));return;}return;
}

main.cpp

#include "msgboxdlg.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);msgboxdlg w;w.show();return a.exec();
}

运行图

QustionMsg

在这里插入图片描述

InFormationMsg

在这里插入图片描述

WarningMsg

在这里插入图片描述

CriticalMsg

在这里插入图片描述

AboutMsg

在这里插入图片描述

AboutAtMsg

在这里插入图片描述

自定义

在这里插入图片描述


http://www.mrgr.cn/news/1887.html

相关文章:

  • Python基础知识学习总结(五)
  • 夏季炎热,宠物化身掉毛大王,猫咪浮毛异味问题该如何解决?
  • C#:基本语法
  • Vue+SortableJs实现拖拽排序
  • 《深入理解 Java 接口》
  • 【区块链+金融服务】广电融汇通供应链金融平台 | FISCO BCOS应用案例
  • 如何从Mac 电脑恢复已删除的文件
  • pygame开发课程系列(7):打砖块,飞行射击,跳跃游戏实例开发
  • Linux安装MQTT 服务器(图文教程)
  • AI作画提示词(Prompts)工程:技巧与最佳实践
  • uniapp中节点信息的使用
  • 华三(H3C)UIS3030 Uni-R4900服务器硬件监控指标解读
  • 工业匠芯 | 匠芯创科技工业级芯片D21X、D13X、D12X、G73X选型应用
  • Conditional Flow Matching: Simulation-Free Dynamic Optimal Transport论文阅读笔记
  • 照片整理专家,照片整理大师,照片图库整理,智能图片整理软件
  • GEC6818开发板的学习
  • 【微信小程序】自定义组件 - 组件的生命周期
  • Python实现GAN(生成对抗网络)图像修复算法
  • re模块入门教程
  • 花几千上万学习Java,真没必要!(四十六)