• QT 消息对话框


     消息对话框分为6种

    1.information 消息对话框

    2.critical 错误对话框

    3.question 问题对话框

    4.warning 对话框

    5.about 对话框

    他们的图标不同  按键不同

    QMessageBox消息对话框的使用

    成员方法使用
    void QMessageBox::setWindowTitle(constQString &title)设置对话框的标题.
    void setText(const QString &text)设置对话框中要显示的文本。
    void setlconPixmap(const QPixmap &pixmap)设置对话框中使用的图片。
    QAbstractButton*QMessageBox::clickedButton() consto-返回用户点击的按钮。
    QPushButton*QMessageBox:addButton(const QString&text, ButtonRole role)向对话框中添加按钮,text为按钮的文本,role是 QMessageBox::ButtonRole枚举类型的变量,用于描述按钮扮演的角色,它的可选值有QMessageBox::AcceptRole (同OK按钮)、QMessageBox::RejectRole (同Cancel按钮)等。
    int QMessageBox::exec()使当前对话框弹出,除非用户关闭对话框,否则对话框将一直存在。此外,当对话框中使用的都是Qt提供的按钮时,该方法可以监听用户点击的是哪个按钮,并将该按钮对应的枚举值返回;如果对话框中包含自定义按钮,需要借助clickedButton()方法确定用户点击的按钮。

    信号和槽很少使用  这里就不介绍了

    1. QMessageBox::StandardButton st= QMessageBox::question(&w,"按y弹出消息对话框","按n弹出错误对话框");//默认是模态的
    2. if(st==QMessageBox:: Yes)
    3. {
    4. QMessageBox::information(&w,"提示","hello");//默认是模态的
    5. }
    6. else
    7. {
    8. QMessageBox::critical(&w,"错误","wrong");//默认是模态的
    9. }

    完整代码如下

    1. #include "widget.h"
    2. #include
    3. #include
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. Widget w;
    8. //QMessageBox mb(QMessageBox::NoIcon,"提示","按一下",QMessageBox::Yes|QMessageBox::No,&w);
    9. QMessageBox mb(&w);
    10. mb.setWindowTitle("提示");
    11. mb.setText("按一下");
    12. /*QPushButton *but1=*/mb.addButton("提示",QMessageBox ::AcceptRole);
    13. /*QPushButton *but2=*/mb.addButton("提示",QMessageBox ::RejectRole);
    14. w.show();
    15. int but= mb.exec();
    16. switch(but)
    17. {
    18. case QMessageBox::AcceptRole:
    19. QMessageBox::information(&w,"提示","hello");//默认是模态的
    20. break;
    21. case QMessageBox::RejectRole:
    22. QMessageBox::critical(&w,"错误","wrong");//默认是模态的
    23. break;
    24. }
    25. //mb默认就是模态的 即使mb.show也是模态的
    26. // QMessageBox::StandardButton st= QMessageBox::question(&w,"按y弹出消息对话框","按n弹出错误对话框");//默认是模态的
    27. // if(st==QMessageBox:: Yes)
    28. // {
    29. // QMessageBox::information(&w,"提示","hello");//默认是模态的
    30. // }
    31. // else
    32. // {
    33. // QMessageBox::critical(&w,"错误","wrong");//默认是模态的
    34. // }
    35. // QMessageBox::warning(&w,"警告","警告");//默认是模态的
    36. return a.exec();
    37. }

    执行  按提示

     

     显示

     按错误

     

    显示

  • 相关阅读:
    LeetCode530.二叉搜索树的最小绝对差 501二叉搜索树中的众数 236二叉树的最近公共祖先
    威尔士和英格兰同属英国,但为啥还要在世界杯上进行PK?
    基于go语言gin框架的web项目骨架
    SpringCloud Alibaba系列 Sentinel(三)
    【广州华锐互动】石油钻井井控VR互动实训系统
    webpack-bundle-analyzer 插件配置
    Meta开源数字水印Stable Signature,极大增强生成式AI安全
    Nacos——Distro一致性协议
    OpenCV入门7——OpenCV中的滤波器(包括低通滤波与高通滤波,其中低通滤波用于降噪,而高通滤波用于边缘检测)
    [免费专栏] ATTACK安全之Android ICMP隧道攻击原理与入侵检测实践
  • 原文地址:https://blog.csdn.net/van9527/article/details/126007681