Qt中QMessageBox的使用与中文按钮显示方法(Qt提示框)

相关资料:
https://www.cnblogs.com/zhoug2020/p/10094855.html qt5信息提示框QMessageBox用法
https://blog.csdn.net/weixin_40314351/article/details/106061672 将QT标准对话框由英文显示为中文
https://blog.csdn.net/libaineu2004/article/details/19030129 如何修改Qt标准对话框的文字(例如,英文改成中文)
https://blog.csdn.net/weixin_37633951/article/details/114699654 vs+qt 突然无法生成ts文件解决方式
https://www.cnblogs.com/ppffs/p/3171565.html QMessageBox按钮简单实现中文显示
https://jingyan.baidu.com/article/0eb457e53cb69f03f0a90572.html 用Qt QMessageBox类创建带汉字按钮的消息框

https://download.csdn.net/download/zhujianqiangqq/21516136    代码包下载

.pro 

 1 QT       += core gui
 2 
 3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 4 
 5 CONFIG += c++11
 6 TRANSLATIONS += qt_en_cn.ts
 7 
 8 # The following define makes your compiler emit warnings if you use
 9 # any Qt feature that has been marked deprecated (the exact warnings
10 # depend on your compiler). Please consult the documentation of the
11 # deprecated API in order to know how to port your code away from it.
12 DEFINES += QT_DEPRECATED_WARNINGS
13 
14 # You can also make your code fail to compile if it uses deprecated APIs.
15 # In order to do so, uncomment the following line.
16 # You can also select to disable deprecated APIs only up to a certain version of Qt.
17 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
18 
19 SOURCES += \
20     main.cpp \
21     mainwindow.cpp
22 
23 HEADERS += \
24     mainwindow.h
25 
26 FORMS += \
27     mainwindow.ui
28 
29 #TRANSLATION += translation_simpleChinese.qm
30 #RANSLATIONS += qzher_en.ts
31 
32 # Default rules for deployment.
33 qnx: target.path = /tmp/$${TARGET}/bin
34 else: unix:!android: target.path = /opt/$${TARGET}/bin
35 !isEmpty(target.path): INSTALLS += target
36 
37 RESOURCES += \
38     resources.qrc
View Code

  main.cpp

 1 #include "mainwindow.h"
 2 
 3 #include <QApplication>
 4 #include <QTranslator>
 5 
 6 int main(int argc, char *argv[])
 7 {
 8     QApplication a(argc, argv);
 9     MainWindow w;
10 
11     // 我的路径为D:\RuanJian\QT5\Qt5.14.2\5.14.2\msvc2017_64\translations(仅作为参考)
12     QTranslator tran;
13     bool IsLoad = tran.load(":/new/prefix1/qt_zh_CN.qm");//注,这里使用的是绝对路径,建议把.qm拷贝到工程目录下,然后使用相对路径.
14     if(IsLoad)
15     {
16         a.installTranslator(&tran);
17     }
18     w.show();
19     return a.exec();
20 }
View Code

 mainwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 #include <QMessageBox>
 6 #include <QDebug>
 7 
 8 QT_BEGIN_NAMESPACE
 9 namespace Ui { class MainWindow; }
10 QT_END_NAMESPACE
11 
12 class MainWindow : public QMainWindow
13 {
14     Q_OBJECT
15 
16 public:
17     MainWindow(QWidget *parent = nullptr);
18     ~MainWindow();
19 
20 private slots:
21     void on_pushButton_clicked();
22 
23     void on_pushButton_2_clicked();
24 
25     void on_pushButton_3_clicked();
26 
27     void on_pushButton_4_clicked();
28 
29     void on_pushButton_5_clicked();
30 
31     void on_pushButton_6_clicked();
32 
33     void on_pushButton_7_clicked();
34 
35     void on_pushButton_8_clicked();
36 
37     void on_pushButton_9_clicked();
38 
39     void on_pushButton_10_clicked();
40 
41     void on_pushButton_11_clicked();
42 
43     void on_pushButton_12_clicked();
44 
45     void on_pushButton_13_clicked();
46 
47     void on_pushButton_14_clicked();
48 
49     void on_pushButton_15_clicked();
50 
51     void on_pushButton_16_clicked();
52 
53     void on_pushButton_17_clicked();
54 
55     void on_pushButton_18_clicked();
56 
57 private:
58     Ui::MainWindow *ui;
59 };
60 #endif // MAINWINDOW_H
View Code

 mainwindow.cpp

  1 // 相关资料:
  2 // https://www.cnblogs.com/zhoug2020/p/10094855.html  qt5信息提示框QMessageBox用法
  3 // https://blog.csdn.net/weixin_40314351/article/details/106061672   将QT标准对话框由英文显示为中文
  4 // https://blog.csdn.net/libaineu2004/article/details/19030129    如何修改Qt标准对话框的文字(例如,英文改成中文)
  5 // https://blog.csdn.net/weixin_37633951/article/details/114699654 vs+qt 突然无法生成ts文件解决方式
  6 // https://www.cnblogs.com/ppffs/p/3171565.html  QMessageBox按钮简单实现中文显示
  7 // https://jingyan.baidu.com/article/0eb457e53cb69f03f0a90572.html  用Qt QMessageBox类创建带汉字按钮的消息框
  8 #include "mainwindow.h"
  9 #include "ui_mainwindow.h"
 10 
 11 MainWindow::MainWindow(QWidget *parent)
 12     : QMainWindow(parent)
 13     , ui(new Ui::MainWindow)
 14 {
 15     ui->setupUi(this);
 16 }
 17 
 18 MainWindow::~MainWindow()
 19 {
 20     delete ui;
 21 }
 22 
 23 // 常用法
 24 void MainWindow::on_pushButton_clicked()
 25 {
 26     QMessageBox::information(NULL, "Title", "Content",
 27                              QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
 28 }
 29 // 第四第五为默认参数
 30 void MainWindow::on_pushButton_2_clicked()
 31 {
 32     QMessageBox::information(NULL, "Title", "Content");
 33 }
 34 // 与实例1相同
 35 void MainWindow::on_pushButton_3_clicked()
 36 {
 37     QMessageBox::information(NULL, "Title", "Content",QMessageBox::Yes|QMessageBox::No);
 38 }
 39 // 添加多个按钮用|运算符连接
 40 void MainWindow::on_pushButton_4_clicked()
 41 {
 42     QMessageBox::information(NULL, "Title", "Content",QMessageBox::Yes|QMessageBox::No|
 43                                  QMessageBox::Abort);
 44 }
 45 // 创建消息提示框后,我们怎么知道用户点了什么呢
 46 void MainWindow::on_pushButton_5_clicked()
 47 {
 48     QMessageBox:: StandardButton result= QMessageBox::information(NULL, "Title", "Content",QMessageBox::Yes|QMessageBox::No);
 49     switch (result)
 50     {
 51     case QMessageBox::Yes:
 52         setWindowTitle("Yes");
 53         break;
 54     case QMessageBox::No:
 55         setWindowTitle("No");
 56         break;
 57     default:
 58         break;
 59     }
 60 }
 61 // critical adj. 关键的; 批评的,爱挑剔的; 严重的; 极重要的;
 62 void MainWindow::on_pushButton_6_clicked()
 63 {
 64     QMessageBox::critical(NULL, "critical", "Content", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
 65 }
 66 // warning adj.警告的;告诫的;引以为戒的
 67 void MainWindow::on_pushButton_7_clicked()
 68 {
 69     QMessageBox::warning(NULL, "warning", "Content", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
 70 }
 71 // 问题;疑问;(待讨论或处理的)事情;议题;课题;怀疑;困惑
 72 void MainWindow::on_pushButton_8_clicked()
 73 {
 74     QMessageBox::question(NULL, "question", "Content", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
 75 }
 76 // 关于;对于;目的是;为了;涉及…方面;忙于;从事于
 77 void MainWindow::on_pushButton_9_clicked()
 78 {
 79     QMessageBox::about(NULL, "About", "https://www.cnblogs.com/FKdelphi/");
 80 }
 81 // 创建的QMessageBox对象,而不是用上面的static函数,
 82 // messageBox是函数局部变量,函数结束后它的生命周期也结束了。会出现消息框一闪而过
 83 void MainWindow::on_pushButton_10_clicked()
 84 {
 85     QMessageBox messageBox;
 86     messageBox.show();
 87 }
 88 // 这样就好理解了,c++函数里面的static变量在函数结束时不会被回收。
 89 void MainWindow::on_pushButton_11_clicked()
 90 {
 91     static QMessageBox messageBox;
 92     messageBox.show();
 93 }
 94 // 这样写也能显示提示框,但是这样会内存泄漏。
 95 void MainWindow::on_pushButton_12_clicked()
 96 {
 97     QMessageBox *messageBox=new QMessageBox;
 98     messageBox->show();
 99 }
100 // 因为QMessageBox继承QDialog,而QDialog有一个神奇的函数exec(),
101 // 调用这个函数后,消息循环会在这个函数里面进行更新,而调用它的函数是被“暂停”的,
102 // 就是说等用户点击按钮后,调用exec()的函数才继续执行。
103 void MainWindow::on_pushButton_13_clicked()
104 {
105     QMessageBox messageBox(QMessageBox::NoIcon,
106                                QStringLiteral("退出"), QStringLiteral("你确定要退出吗?"),
107                                QMessageBox::Yes | QMessageBox::No, NULL); ;
108         int result=messageBox.exec();
109 
110 
111         switch (result)
112         {
113         case QMessageBox::Yes:
114             qDebug("Yes");
115             close();
116             break;
117         case QMessageBox::No:
118             setWindowTitle("No");
119             break;
120         default:
121             break;
122         }
123 }
124 
125 void MainWindow::on_pushButton_14_clicked()
126 {
127     QMessageBox box(QMessageBox::Warning, QStringLiteral("警告"), QStringLiteral("不能点击这个按钮!"));
128     box.setStandardButtons(QMessageBox::Ok);
129     box.setButtonText(QMessageBox::Ok, QStringLiteral("确定"));
130     box.exec();
131 }
132 
133 void MainWindow::on_pushButton_15_clicked()
134 {
135     QMessageBox box(QMessageBox::Warning, tr("Qt warning"), tr("Qt wo re you daye"));
136     box.exec();
137 }
138 
139 void MainWindow::on_pushButton_16_clicked()
140 {
141     int result = QMessageBox::information(NULL,
142                              QStringLiteral("提示"),
143                              QStringLiteral("显示信息!"),
144                              QStringLiteral("确定"),
145                              QStringLiteral("取消"));
146     setWindowTitle(QString::number(result));
147 }
148 
149 void MainWindow::on_pushButton_17_clicked()
150 {
151     QPushButton *okbtn=new QPushButton(QStringLiteral("确定"));
152     QPushButton *cancelbtn=new QPushButton(QStringLiteral("取消"));
153     QMessageBox *mymsgbox=new QMessageBox;
154     mymsgbox->addButton(okbtn,QMessageBox::AcceptRole);
155     mymsgbox->addButton(cancelbtn,QMessageBox::RejectRole);
156     int result = mymsgbox->exec();
157     switch (result)
158     {
159     case QMessageBox::AcceptRole:
160         qDebug("ok");
161         close();
162         break;
163     case QMessageBox::RejectRole:
164         setWindowTitle("No");
165         break;
166     default:
167         break;
168     }
169     // 你TMD记得删除呀,要不有内存泄漏。
170     return;
171     delete okbtn;
172     delete cancelbtn;
173     delete mymsgbox;
174 }
175 
176 void MainWindow::on_pushButton_18_clicked()
177 {
178     QMessageBox::information(NULL,
179                              QStringLiteral("提示"),
180                              QStringLiteral("有人说可以修改qt_en.qm和 qt_zh_CN.qm,反正我没实现!"),
181                              QStringLiteral("他娘的"));
182 }
View Code

 mainwindow.ui

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <ui version="4.0">
  3  <class>MainWindow</class>
  4  <widget class="QMainWindow" name="MainWindow">
  5   <property name="geometry">
  6    <rect>
  7     <x>0</x>
  8     <y>0</y>
  9     <width>602</width>
 10     <height>287</height>
 11    </rect>
 12   </property>
 13   <property name="windowTitle">
 14    <string>MainWindow</string>
 15   </property>
 16   <widget class="QWidget" name="centralwidget">
 17    <widget class="QPushButton" name="pushButton">
 18     <property name="geometry">
 19      <rect>
 20       <x>30</x>
 21       <y>20</y>
 22       <width>100</width>
 23       <height>23</height>
 24      </rect>
 25     </property>
 26     <property name="text">
 27      <string>information常规</string>
 28     </property>
 29    </widget>
 30    <widget class="QPushButton" name="pushButton_2">
 31     <property name="geometry">
 32      <rect>
 33       <x>30</x>
 34       <y>50</y>
 35       <width>100</width>
 36       <height>23</height>
 37      </rect>
 38     </property>
 39     <property name="text">
 40      <string>4、5默认参数</string>
 41     </property>
 42    </widget>
 43    <widget class="QPushButton" name="pushButton_3">
 44     <property name="geometry">
 45      <rect>
 46       <x>30</x>
 47       <y>80</y>
 48       <width>100</width>
 49       <height>23</height>
 50      </rect>
 51     </property>
 52     <property name="text">
 53      <string>与实例1相同</string>
 54     </property>
 55    </widget>
 56    <widget class="QPushButton" name="pushButton_4">
 57     <property name="geometry">
 58      <rect>
 59       <x>30</x>
 60       <y>110</y>
 61       <width>100</width>
 62       <height>23</height>
 63      </rect>
 64     </property>
 65     <property name="text">
 66      <string>增加按钮</string>
 67     </property>
 68    </widget>
 69    <widget class="QPushButton" name="pushButton_5">
 70     <property name="geometry">
 71      <rect>
 72       <x>30</x>
 73       <y>140</y>
 74       <width>100</width>
 75       <height>23</height>
 76      </rect>
 77     </property>
 78     <property name="text">
 79      <string>用户点了什么</string>
 80     </property>
 81    </widget>
 82    <widget class="QPushButton" name="pushButton_6">
 83     <property name="geometry">
 84      <rect>
 85       <x>150</x>
 86       <y>20</y>
 87       <width>100</width>
 88       <height>23</height>
 89      </rect>
 90     </property>
 91     <property name="text">
 92      <string>严重的</string>
 93     </property>
 94    </widget>
 95    <widget class="QPushButton" name="pushButton_7">
 96     <property name="geometry">
 97      <rect>
 98       <x>260</x>
 99       <y>20</y>
100       <width>100</width>
101       <height>23</height>
102      </rect>
103     </property>
104     <property name="text">
105      <string>警告</string>
106     </property>
107    </widget>
108    <widget class="QPushButton" name="pushButton_8">
109     <property name="geometry">
110      <rect>
111       <x>370</x>
112       <y>20</y>
113       <width>100</width>
114       <height>23</height>
115      </rect>
116     </property>
117     <property name="text">
118      <string>问题</string>
119     </property>
120    </widget>
121    <widget class="QPushButton" name="pushButton_9">
122     <property name="geometry">
123      <rect>
124       <x>480</x>
125       <y>20</y>
126       <width>100</width>
127       <height>23</height>
128      </rect>
129     </property>
130     <property name="text">
131      <string>关于</string>
132     </property>
133    </widget>
134    <widget class="QPushButton" name="pushButton_10">
135     <property name="geometry">
136      <rect>
137       <x>30</x>
138       <y>210</y>
139       <width>100</width>
140       <height>23</height>
141      </rect>
142     </property>
143     <property name="text">
144      <string>QMessageBox对象</string>
145     </property>
146    </widget>
147    <widget class="QPushButton" name="pushButton_11">
148     <property name="geometry">
149      <rect>
150       <x>150</x>
151       <y>210</y>
152       <width>100</width>
153       <height>23</height>
154      </rect>
155     </property>
156     <property name="text">
157      <string>static变量</string>
158     </property>
159    </widget>
160    <widget class="QPushButton" name="pushButton_12">
161     <property name="geometry">
162      <rect>
163       <x>260</x>
164       <y>210</y>
165       <width>100</width>
166       <height>23</height>
167      </rect>
168     </property>
169     <property name="text">
170      <string>内存泄漏</string>
171     </property>
172    </widget>
173    <widget class="QPushButton" name="pushButton_13">
174     <property name="geometry">
175      <rect>
176       <x>370</x>
177       <y>210</y>
178       <width>100</width>
179       <height>23</height>
180      </rect>
181     </property>
182     <property name="text">
183      <string>调用exec()</string>
184     </property>
185    </widget>
186    <widget class="QPushButton" name="pushButton_14">
187     <property name="geometry">
188      <rect>
189       <x>30</x>
190       <y>250</y>
191       <width>100</width>
192       <height>23</height>
193      </rect>
194     </property>
195     <property name="text">
196      <string>中文方法1</string>
197     </property>
198    </widget>
199    <widget class="QPushButton" name="pushButton_15">
200     <property name="geometry">
201      <rect>
202       <x>150</x>
203       <y>250</y>
204       <width>100</width>
205       <height>23</height>
206      </rect>
207     </property>
208     <property name="text">
209      <string>中文方法2</string>
210     </property>
211    </widget>
212    <widget class="QPushButton" name="pushButton_16">
213     <property name="geometry">
214      <rect>
215       <x>260</x>
216       <y>250</y>
217       <width>100</width>
218       <height>23</height>
219      </rect>
220     </property>
221     <property name="text">
222      <string>中文方法3</string>
223     </property>
224    </widget>
225    <widget class="QPushButton" name="pushButton_17">
226     <property name="geometry">
227      <rect>
228       <x>370</x>
229       <y>250</y>
230       <width>100</width>
231       <height>23</height>
232      </rect>
233     </property>
234     <property name="text">
235      <string>中文显示4</string>
236     </property>
237    </widget>
238    <widget class="QPushButton" name="pushButton_18">
239     <property name="geometry">
240      <rect>
241       <x>480</x>
242       <y>250</y>
243       <width>100</width>
244       <height>23</height>
245      </rect>
246     </property>
247     <property name="text">
248      <string>中文显示5失败</string>
249     </property>
250    </widget>
251   </widget>
252  </widget>
253  <resources/>
254  <connections/>
255 </ui>
View Code

 resources省略 

posted on 2021-08-25 17:08  疯狂delphi  阅读(1174)  评论(2编辑  收藏  举报

导航