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

Qt消息对话框

问题对话框

对应API

[static] QMessageBox::StandardButton QMessageBox::question(
QWidget *parent, 
const QString &title, 
const QString &text, 
QMessageBox::StandardButtons buttons = StandardButtons(Yes | No),
QMessageBox::StandardButton defaultButton = NoButton)

举例:

//参数1:父窗口
//参数2:对话框标题
//参数3:对话框提示文本
//参数4:对话框按钮
//参数5:对话框刚弹出时默认选择的按钮
//返回值:点击的按钮
auto choice = QMessageBox::question(this,"Question","你是否想退出,请选择:",QMessageBox::Ok | QMessageBox::Cancel,QMessageBox::Ok);
switch (choice) {case QMessageBox::Ok://do somethingqDebug() << "选择了ok";break;case QMessageBox::Cancel://do somethingqDebug() << "选择了cancel";break;default:break;
}

提示对话框

对应API

[static] QMessageBox::StandardButton QMessageBox::information(QWidget * parent,const QString & title,const QString & text, QMessageBox::StandardButtons buttons = Ok,QMessageBox::StandardButton defaultButton = NoButton)

 举例:

//参数1:父窗口
//参数2:对话框标题
//参数3:对话框提示文本
//其余几个参数使用默认的就好了,提示对话框不需要什么按钮
//返回值:点击的按钮
auto choice = QMessageBox::information(this, "Information", "即将退出!");
switch (choice) {case QMessageBox::Ok://do somethingqDebug() << "选择了ok";break;case QMessageBox::Cancel://do somethingqDebug() << "选择了cancel";break;default:break;
}

警告对话框

对应API

[static] QMessageBox::StandardButton QMessageBox::warning(QWidget * parent,const QString & title,const QString & text, QMessageBox::StandardButtons buttons = Ok,QMessageBox::StandardButton defaultButton = NoButton)

举例:

//参数1:父窗口
//参数2:对话框标题
//参数3:对话框提示文本
//参数4:对话框按钮
//参数5:对话框刚弹出时默认选择的按钮
//返回值:点击的按钮
auto choice = QMessageBox::warning(this, "Warning", "会被删除!",QMessageBox::Ok | QMessageBox::Cancel,QMessageBox::Cancel);
switch (choice) {case QMessageBox::Ok://do somethingqDebug() << "选择了ok";break;case QMessageBox::Cancel://do somethingqDebug() << "选择了cancel";break;default:break;
}

错误对话框

对应API

[static] QMessageBox::StandardButton QMessageBox::critical(QWidget * parent,const QString & title,const QString & text, QMessageBox::StandardButtons buttons = Ok,QMessageBox::StandardButton defaultButton = NoButton)

举例:

//参数1:父窗口
//参数2:对话框标题
//参数3:对话框提示文本
//其余几个参数使用默认的就好了,错误对话框不需要什么按钮
//返回值:点击的按钮
auto choice = QMessageBox::critical(this, "critical", "错误操作!");
switch (choice) {case QMessageBox::Ok://do somethingqDebug() << "选择了ok";break;case QMessageBox::Cancel://do somethingqDebug() << "选择了cancel";break;default:break;
}

关于对话框

就是一个简单的对话框,可以介绍一些版本信息什么的

对应API

[static] void QMessageBox::about(QWidget * parent,const QString & title,const QString & text)

举例:

//参数1:父窗口
//参数2:对话框标题
//参数3:对话框提示文本
QMessageBox::about(this, "about", "版本:v1.1");

自定义消息对话框

相关API

  • 设置标题
  • void QMessageBox::setWindowTitle(const QString &title)
  • 设置自定义图片
  • void setIconPixmap(const QPixmap &pixmap)
  • 设置文本
  • void setText(const QString &text
  • 设置按钮
  • //自定义按钮role一般设置为QMessageBox::ButtonRole::ActionRole
    //返回值:添加的按钮
    QPushButton *QMessageBox::addButton(const QString &text, QMessageBox::ButtonRole role)
  • 获取点击的按钮
  • QAbstractButton *QMessageBox::clickedButton() const

举例:

QMessageBox custom_message_box;//1.设置标题
custom_message_box.setWindowTitle("自定义对话框");
//2.设置图片
custom_message_box.setIconPixmap(QPixmap(":/11.png").scaled(20, 20));
//3.设置文本
custom_message_box.setText("这是一个自定义对话框");
//4.添加按钮
QPushButton * btn_yes = custom_message_box.addButton("YES", QMessageBox::ButtonRole::ActionRole);
QPushButton * btn_no = custom_message_box.addButton("NO", QMessageBox::ButtonRole::ActionRole);
//模态显示对话框
custom_message_box.exec();
//5.获取点击的按钮
if (custom_message_box.clickedButton() == btn_yes) {//do something...
} else if (custom_message_box.clickedButton() == btn_no) {//do something...
}

 学习链接:https://github.com/0voice

http://www.lryc.cn/news/463046.html

相关文章:

  • FreeRTOS的队列管理
  • 买卖股票的最佳时机(动态规划方法总结)
  • KubeSphere安装mysql8.4.0
  • SpringBoot项目热部署-devtools
  • 从MySQL到OceanBase离线数据迁移的实践
  • ifconfig 和 ip addr
  • NCCL报错
  • 域7:安全运营 第16章 安全运营管理
  • 研发线上事故风险解读之数据库存储
  • react hooks中在setState后输出state为啥没有变化,如何解决
  • C++设计模式——代理模式
  • docker 复制文件,清除不再使用数据导出以及导出文件系统
  • 【Vue】Vue3.0(十一)Vue 3.0 中 computed 计算属性概念、使用及示例
  • 【第三版 系统集成项目管理工程师】第17章 法律法规和标准规范
  • 安装 LLM 编程工具 cursor
  • Java链式编程的定义、例子、使用方法、实际应用场景、自动装配构造
  • 用 Git Stash 临时保存修改,轻松切换任务!
  • Android 下通过触发 SIGTRAP 信号实现反调试
  • 【MySQL】 表的增删操作
  • 新生入门季 | 学习生物信息分析,如何解决个人电脑算力不足的问题?
  • 20255 - 中医方剂学 - 考研 - 执业
  • 【Vue.js设计与实现】第三篇第9章:渲染器-简单Diff算法-阅读笔记
  • 服务器软件之Tomcat
  • Flutter包管理(三)
  • CGNS资料
  • 论文阅读(十六):Deep Residual Learning for Image Recognition
  • Dubbo 序列化方式
  • 如何替换OCP节点(二):使用 antman脚本 | OceanBase应用实践
  • 15.JVM垃圾收集算法
  • 软件工程:图书管理系统甘特图