当前位置: 首页 > 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.lryc.cn/news/425300.html

相关文章:

  • Windows11下wsl闪退的解决
  • 通过调整JVM的默认内存配置来解决内存溢出(‌OutOfMemoryError)‌或栈溢出(‌StackOverflowError)‌等错误
  • RCE---eval长度限制绕过技巧
  • C++11标准模板(STL)- 算法库 - 类似 std::accumulate,但不依序执行 -(std::reduce)
  • 反射机制的介绍
  • AI图文带货,手把手教学,傻瓜操作,轻松日入500+,小白教程
  • java:实现简单的验证码功能
  • MybatisPlus使用指南
  • 5. MongoDB 集合创建、更新、删除
  • PHP中如何将变量从函数传递给acf_add_filter
  • KNN算法的使用
  • java文件上传
  • MySQL 数据库经验总结
  • Python环境安装及PIP安装(Mac OS版)
  • 2024自动驾驶(多模态)大模型综述:从DriveGPT4、DriveMLM到DriveLM、DriveVLM
  • 晨控CK-GW08-EC与汇川AC801系列PLC的EtherCAT通讯连接说明手册
  • 向上or向下调整建堆 的时间复杂度的本质区别的讲解
  • 阿一网络安全实战演练之利用 REST URL 中的服务器端参数污染
  • [游戏开发] LuaTable转string存读二进制文件
  • 光伏业务管理系统的一些妙用功能
  • Java面试八股之请简述消息队列的发布订阅模式
  • 七、2 ADC数模转换器有关函数介绍(Keil5)
  • 了解载波侦听多路访问CSMA(上)
  • 开启教育新征程:“集师” 知识付费平台搭建
  • Vue3 + Electron 创建新的子窗口 且子窗口唯一
  • 海康VisionMaster使用学习笔记2-相机取图及参数设置
  • 【网络】【Linux】Linux内核中连接的组织形式与全连接队列
  • 记录一次 npm ERR! cb() never called! 解决过程
  • WEB渗透免杀篇-加载器免杀
  • 什么是反人性设计?