-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathch11-05.htm
More file actions
1921 lines (1376 loc) · 115 KB
/
ch11-05.htm
File metadata and controls
1921 lines (1376 loc) · 115 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>ch11-05</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="thumbnailviewer.css" type="text/css">
<script src="thumbnailviewer.js" type="text/javascript">
/***********************************************
* Image Thumbnail Viewer Script- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
</script> </head>
<body>
<div class="os1">11.5 通用对话框:QMessageBox、QWizard</div>
<br>
本节介绍通用对话框:QMessageBox、QWizard,并通过示例程序展示消息对话框和向导对话框的各自使用方法。 <br>
<br>
<div class="os2">11.5.1 QMessageBox</div>
<br>
QMessageBox 消息框一般作为模态对话框,告知用户信息、提供选项并接收用户反馈。消息框同时提供三种文本显示:primary
text(基本文本,必须有的,显示情况信息)、informative text(信息文本,向用户解释更多情况或提出问题)、detailed
text(详细文本,可选的,根据用户需要显示更多数据信息),这些文本都是用于提供信息给用户理解消息框提出的问题,用户理解后作出相应的选择。消
息框还提供图标和一系 列标准按钮,程序员根据需要设置图标和按
钮,用户点击按钮后,消息框返回用户点击的按钮数值。消息框可以使用自定义对象的方式,也可以直接调用定制好的静态函数弹窗。我们先介绍消息框的普通成员函数和其
主要属性,然后介绍静态函数。<br>
(1)普通成员函数<br>
QMessageBox 基类是 QDialog 对话框类,QMessageBox 构造函数如下:<br>
<div class="code">QMessageBox(QWidget * parent = 0)<br>
QMessageBox(Icon icon, const QString & title, const QString &
text, StandardButtons buttons = NoButton, QWidget * parent = 0,
Qt::WindowFlags f = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint)</div>
第一个是默认构造函数,只有 parent 父窗口参数。第二个是带有详细参数的构造函数,参数 icon 是消息框显示的图标;title
是标题栏文本;text 是基本文本,显示展示给用户的信息内容;buttons 是消息框显示的按钮,可以通过 |
位或操作指定多个按钮,构造函数里的NoButton 是指不设置按钮,默认用 Ok 按钮;parent 是父窗口参数; f
是窗口标志位,Qt::Dialog 代表是对话框标志,窗口右上角没有最大化、最小化按钮,Qt::MSWindowsFixedSizeDialogHint
是指 Windows 的固定尺寸对话框的边框风格。<br>
<br>
针对消息框的按钮使用,有多种添加、设置和访问函数。<br>
首先是添加按钮函数:<br>
<div class="code">void addButton(QAbstractButton * button,
ButtonRole role)<br>
QPushButton * addButton(const QString & text,
ButtonRole role)<br>
QPushButton * addButton(StandardButton button)</div>
第一个添加按钮函数,参数为自定义的按钮对象指针,role 是按钮的角色类型。<br>
第二个添加按钮函数,根据文本新建按钮添加到消息框,并返回新建的按钮对象指针。<br>
第三个添加按钮函数,根据标志位数值,添加标准按钮,并返回新建的按钮对象指针。<br>
按钮角色是枚举类型,具体如下表:<br>
<br>
<table class="tabel">
<tbody>
<tr class="d1">
<td style="width: 220px;" align="center"><b>QMessageBox::ButtonRole</b></td>
<td style="width: 100px;" align="center"><b>数值</b></td>
<td align="center"><b> 描述</b></td>
</tr>
<tr>
<td>QMessageBox::InvalidRole</td>
<td> -1</td>
<td> 不可用角色,该按钮不可用。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::AcceptRole</td>
<td> 0</td>
<td> 接收角色,点击该按钮导致对话框被接受,例如 OK 按钮。 </td>
</tr>
<tr>
<td>QMessageBox::RejectRole</td>
<td> 1 </td>
<td> 拒绝角色,点击该按钮导致对话框被拒绝,例如 Cancel 按钮。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::DestructiveRole</td>
<td> 2 </td>
<td> 放弃角色,点击该按钮导致放弃变更并关闭对话框,例如 放弃变更、不保存就退出。 </td>
</tr>
<tr>
<td>QMessageBox::ActionRole</td>
<td> 3 </td>
<td> 动作角色,点击该按钮导致对话框元素做变动动作。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::HelpRole</td>
<td> 4</td>
<td> 帮助角色,点击该按钮请求帮助。 </td>
</tr>
<tr>
<td>QMessageBox::YesRole</td>
<td> 5</td>
<td> 同意角色,表示 Yes 意思的按钮。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::NoRole</td>
<td> 6</td>
<td> 否定角色,表示 No 意思的按钮。 </td>
</tr>
<tr>
<td>QMessageBox::ApplyRole</td>
<td> 8</td>
<td> 应用角色,应用当前的变更操作。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::ResetRole</td>
<td> 7</td>
<td> 重置角色,将对话框元素重置为默认值。 </td>
</tr>
</tbody>
</table>
<br>
点击不同角色的按钮,对话框的动作不同,并且影响对话框返回值,要根据实际需要配置按钮角色。<br>
消息框定义了很多标准按钮,可以通过 | 位或运算符设置多个标准按钮,标准按钮的标志位如下表所示:<br>
<br>
<table class="tabel">
<tbody>
<tr class="d1">
<td style="width: 220px;" align="center"><b>QMessageBox::StandardButtons</b></td>
<td style="width: 100px;" align="center"><b>数值</b></td>
<td align="center"><b> 描述</b></td>
</tr>
<tr>
<td>QMessageBox::Ok</td>
<td> 0x00000400 </td>
<td> OK 按钮,定义为 AcceptRole 角色。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::Open</td>
<td> 0x00002000 </td>
<td> Open 按钮,定义为 AcceptRole 角色。 </td>
</tr>
<tr>
<td>QMessageBox::Save</td>
<td> 0x00000800 </td>
<td> Save 按钮,定义为 AcceptRole 角色。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::Cancel</td>
<td> 0x00400000 </td>
<td> Cancel 按钮,定义为 RejectRole 角色。 </td>
</tr>
<tr>
<td>QMessageBox::Close</td>
<td> 0x00200000 </td>
<td> Close 按钮,定义为 RejectRole 角色。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::Discard</td>
<td> 0x00800000 </td>
<td> Discard 或 Don't Save 按钮,依赖系统平台,定义为 DestructiveRole 角色。 </td>
</tr>
<tr>
<td>QMessageBox::Apply</td>
<td> 0x02000000 </td>
<td> Apply 按钮,定义为 ApplyRole 角色。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::Reset</td>
<td> 0x04000000 </td>
<td> Reset 按钮,定义为 ResetRole 角色。 </td>
</tr>
<tr>
<td>QMessageBox::RestoreDefaults</td>
<td> 0x08000000 </td>
<td> Restore Defaults 按钮,定义为 ResetRole 角色。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::Help</td>
<td> 0x01000000 </td>
<td> Help 按钮,定义为 HelpRole 角色。</td>
</tr>
<tr>
<td>QMessageBox::SaveAll</td>
<td> 0x00001000 </td>
<td> Save All 按钮,定义为 AcceptRole 角色。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::Yes</td>
<td> 0x00004000 </td>
<td> Yes 按钮,定义为 YesRole 角色。 </td>
</tr>
<tr>
<td>QMessageBox::YesToAll</td>
<td> 0x00008000 </td>
<td> Yes to All 按钮,定义为 YesRole 角色。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::No</td>
<td> 0x00010000 </td>
<td> No 按钮,定义为 NoRole 角色。 </td>
</tr>
<tr>
<td>QMessageBox::NoToAll</td>
<td> 0x00020000 </td>
<td> No to All 按钮,定义为 NoRole 角色。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::Abort</td>
<td> 0x00040000 </td>
<td> Abort 按钮,定义为 RejectRole 角色。 </td>
</tr>
<tr>
<td>QMessageBox::Retry</td>
<td> 0x00080000 </td>
<td> Retry 按钮,定义为 AcceptRole 角色。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::Ignore</td>
<td> 0x00100000 </td>
<td> Ignore 按钮,定义为 AcceptRole 角色。 </td>
</tr>
<tr>
<td>QMessageBox::NoButton</td>
<td> 0x00000000 </td>
<td> 不可用按钮 </td>
</tr>
</tbody>
</table>
<br>
根据标志位,可以一次设置多个标准按钮,如下函数:<br>
<div class="code">void setStandardButtons(StandardButtons
buttons) //设置标准按钮<br>
StandardButtons standardButtons()
const //获取设置好的标准按钮标志位<br>
StandardButton standardButton(QAbstractButton * button)
const //根据按钮对象指针获取按钮对应的标准按钮标志位</div>
setStandardButtons() 函数根据参数里标志位一次设置多个按钮,standardButtons() 则返回设置好的按钮标志位。<br>
standardButton() 是根据参数里的按钮对象指针,返回该按钮对应的标准按钮标志位,如果对象不是标准按钮,返回
QMessageBox::NoButton 。<br>
注意不要调用 msgBox.setStandardButtons(QMessageBox::NoButton) ,消息框显示时会没有任何按钮,消息框可能
出现关不掉的情况,程序界面会卡住无法操作,一般默认用 QMessageBox::Ok 按钮即可。<br>
<br>
按钮角色可以读取,如下函数:<br>
<div class="code">ButtonRole buttonRole(QAbstractButton *
button) const<br>
</div>
按钮角色得在添加按钮 addButton() 时指定,目前没有直接修改按钮角色的函数。<br>
从消息框卸载按钮使用如下函数:<br>
<div class="code">void removeButton(QAbstractButton *
button)<br>
</div>
注意 removeButton() 函数只是将消息框的按钮卸下,并不会删除该按钮对象。<br>
如果需要修改按钮角色,可以先卸载按钮,再用新的角色添加给消息框。<br>
获取消息框已设置的所有按钮对象列表,使用如下函数:<br>
<div class="code">QList<QAbstractButton *> buttons()
const</div>
<br>
当消息框有多个按钮时,可以指定默认按钮(默认对应 Enter 键)、退出按钮(对应 Esc/Escape 键),如下函数:<br>
<div class="code">void setDefaultButton(QPushButton *
button) //根据按钮指针设置默认按钮<br>
void setDefaultButton(StandardButton button)
//根据标准按钮标志位设 置默认按钮<br>
QPushButton * defaultButton() const
//获取默认按钮对象指针<br>
void setEscapeButton(QAbstractButton *
button) //根据按钮指针设置退出按钮<br>
void setEscapeButton(StandardButton
button) //根据标准按钮标志位设置退出按钮<br>
QAbstractButton * escapeButton()
const
//获取退出按钮对象指针</div>
注意检查参数和返回值的有效性,避免操作空指针。<br>
<br>
根据用户的操作,可以获取用户点击的按钮对象,或者返回 0 (NULL):<br>
<div class="code">QAbstractButton * clickedButton() const<br>
</div>
用户如果点击了按钮,就返回点击的按钮对象;如果没有设置退出键时用户按下键盘 Esc 键,返回 0 。<br>
在未调用 exec() 显示消息框时, clickedButton() 也返回 0 。<br>
消息框还可以添加一个复选框,比如用于设置是否在文件关闭前自动保存修改内容,注意复选框对象要自己新建,消息框默认并没有复选框。设置和获取复选框 对象函数如
下:<br>
<div class="code">QCheckBox * checkBox()
const //获取复选框对象指针,默认为 NULL<br>
void setCheckBox(QCheckBox * cb) //手动新建复选框对象
cb,添加给消息框<br>
</div>
checkBox() 默认是 NULL,消息框自己不会创建复选框,必须自己手动新建一个复选框对象,用setCheckBox( cb )
函数设置复选框对象。<br>
一个典型的消息框对象使用如下代码示例:<br>
<div class="code"> QMessageBox msgBox;<br>
msgBox.setText(tr("文本已经修改。")); //基本文本设置<br>
msgBox.setInformativeText(tr("是否保存修改后的文本?"));
//信息文本设置<br>
msgBox.setDetailedText(tr("勾选上面复选框,设置总是自动保存修改的文本。")); //详细文本设置<br>
msgBox.setStandardButtons(QMessageBox::Save |
QMessageBox::Discard | QMessageBox::Cancel);<br>
msgBox.setDefaultButton(QMessageBox::Save);<br>
//补充复选框<br>
QCheckBox *pCheckBox = new
QCheckBox(tr("关闭前总是自动保存修改文本。"));<br>
msgBox.setCheckBox(pCheckBox);<br>
int ret = msgBox.exec();<br>
if(NULL != msgBox.checkBox())<br>
{<br>
bool bAlwaysSave =
msgBox.checkBox()->isChecked();<br>
if(bAlwaysSave)<br>
{<br>
qDebug()<<tr("关闭前总是自动保存修改文本。");<br>
}<br>
else<br>
{<br>
qDebug()<<tr("关闭前不自动保存修改文本。");<br>
}<br>
}<br>
</div>
显示效果如下图所示:<br>
<center> <img src="images/ch11/ch11-05-01.png" alt="msgboxshow1"></center>
消息框第一行显示的是基本文本,第二行是信息文本,当设置了详细文本后,消息框自动添加“Show Details”按钮,点击该按钮就会展开下方的文本框显示详
细文本。<br>
添加复选框之后,复选框显示在第三行,可以访问复选框对象,获取用户是否选中了复选框,以进行后续操作。<br>
消息框的三种文本访问和设置函数如下:<br>
<div class="code">QString text() const //获取基本文
本<br>
void setText(const QString & text) //设置基本文本<br>
QString informativeText() const //获取信息文本<br>
void setInformativeText(const QString &
text) //设置信息文本<br>
QString detailedText() const
//获取详细文本<br>
void setDetailedText(const QString &
text) //设置详细文本<br>
</div>
设置详细文本后,消息框自动添加“Show Details”按钮,如果没有设置详细文本,消息框就没有“Show Details”按钮。<br>
消息框文本可以设置文本格式,如下函数:<br>
<div class="code">Qt::TextFormat textFormat()
const //获取文本格式<br>
void setTextFormat(Qt::TextFormat format)
//设置文本格式<br>
</div>
Qt::TextFormat 枚举类型内容比较简单,三种取值如下表所示:<br>
<br>
<table class="tabel">
<tbody>
<tr class="d1">
<td style="width: 220px;" align="center"><b>Qt::TextFormat 枚举常量</b></td>
<td style="width: 140px;" align="center"><b>数值</b></td>
<td align="center"><b> 描述</b></td>
</tr>
<tr>
<td>Qt::PlainText</td>
<td> 0</td>
<td> 设置的文本理解为纯文本,不解析 HTML 语法。 </td>
</tr>
<tr class="d1">
<td>Qt::RichText</td>
<td> 1 </td>
<td> 设置的文本理解为丰富文本,按照 HTML 语法解析呈现内容。 </td>
</tr>
<tr>
<td>Qt::AutoText</td>
<td> 2</td>
<td> 自动判断文本类型,如果 Qt::mightBeRichText() 函数返回 true,当做丰富文本呈现,否则都当成纯文本。</td>
</tr>
</tbody>
</table>
<br>
默认文本格式为 Qt::AutoText ,自动判断设置文本类型。<br>
注意 setTextFormat() 函数只影响基本文本,不影响信息文本和详细文本。<br>
信息文本默认总是自动文本格式 Qt::AutoText,详细文本默认总是纯文本格式 Qt::PlainText。<br>
<br>
消息框的文本还可以设置文本交互操作的标志位,如下函数:<br>
<div class="code">Qt::TextInteractionFlags
textInteractionFlags() const
//获取文本交互标志位,默认值依赖 style 风格<br>
void setTextInteractionFlags(Qt::TextInteractionFlags
flags) //设置文本交互标志位<br>
</div>
Qt::TextInteractionFlags 标志位定义文本的交互性,比如是否可以复制,是否可以编辑,如下表所示:<br>
- setTextInteractionFlags() 只影响基本文本,不影响信息文本和详细文本。<br>
消息框除了显示文本,还可以选择图标配合显示,表示消息的类别,比如普通消息、警告消息、严重错误消息等类型。<br>
图标支持使用 QMessageBox 自带图标或者自定义图标,如下函数:<br>
<div class="code">void setIcon(Icon)
//设置 QMessageBox 自带的图标<br>
Icon icon() const //获取所用的
QMessageBox 自带图标<br>
void setIconPixmap(const QPixmap &
pixmap) //设置自定义图片作为图标<br>
QPixmap iconPixmap() const //获取设置的自定义图标的图片<br>
</div>
默认情况下, iconPixmap() 是空的 null ,没有自定义图片文件。如果需要用自定义图片,根据图片文件名构建 QPixmap
对象,设置给消息框即可。<br>
icon() 默认情况下返回 0,就是 QMessageBox::NoIcon ,QMessageBox 自带的图标如下表所示:<br>
<br>
<table class="tabel">
<tbody>
<tr class="d1">
<td style="width: 220px;" align="center"><b>QMessageBox::Icon 枚举类型</b></td>
<td style="width: 120px;" align="center"><b>数值</b></td>
<td align="center"><b> 描述</b></td>
<td align="center"><b> 图标示例</b></td>
</tr>
<tr>
<td>QMessageBox::NoIcon</td>
<td> 0</td>
<td> 消息框没有任何图标。 </td>
<td> 无 </td>
</tr>
<tr class="d1">
<td> QMessageBox::Question</td>
<td> 4 </td>
<td> 询问图标,说明提示的消息是向用户询问。 </td>
<td> <img src="images/ch11/iconquestion.png" alt="iconquestion"> </td>
</tr>
<tr>
<td>QMessageBox::Information</td>
<td> 1</td>
<td> 信息图标,说明提示的消息是告诉用户信息。</td>
<td> <img src="images/ch11/iconinformation.png" alt="iconinformation">
</td>
</tr>
<tr class="d1">
<td>QMessageBox::Warning</td>
<td> 2 </td>
<td> 警告图标,说明提示的消息是警告信息,但目前程序能够处理。 </td>
<td> <img src="images/ch11/iconwarning.png" alt="iconwarning"> </td>
</tr>
<tr>
<td>QMessageBox::Critical</td>
<td> 3</td>
<td> 严重错误图标,说明程序发生了严重错误,很可能崩溃。</td>
<td> <img src="images/ch11/iconcritical.png" alt="iconcritical"> </td>
</tr>
</tbody>
</table>
<br>
上表图标示例仅供参考,实际运行时图标依赖系统平台的风格,不同操作系统显示图标不一样。<br>
消息框还可以修改窗口标题栏文本,设置模态或非模态显示,如下函数设置:<br>
<div class="code">void setWindowTitle(const QString &
title) //设置消息框标题栏文本<br>
QString windowTitle()
const //获取消息框标题栏文本<br>
void setWindowModality(Qt::WindowModality
windowModality) //设置消息框显示模态类型<br>
Qt::WindowModality windowModality() const
//获取消息框的显示模态类型<br>
</div>
窗口的显示模态类型见 11.1.1 节QWidget 类的介绍,主要就是非模态 Qt::NonModal、Qt::WindowModal
窗口级模态、Qt::ApplicationModal 应用级模态三种。<br>
消息框还有一个特别的 open() 函数,可以指定接受信号的对象和槽函数:<br>
<div class="code">void open(QObject * receiver, const char
* member)<br>
</div>
该函数打开消息框,并将 finished() 或 buttonClicked() 信号关联到参数里对象槽函数,如果槽函数有一个指针参数,那么关联
buttonClicked() 信号,否则关联 finished() 信号到槽函数。<br>
注意消息窗口关闭后,自动解除信号和 open() 参数里对象的槽函数关联。<br>
<br>
消息框类本级的槽函数只有 1 个,如下函数:<br>
<div class="code">virtual int exec() //模态显示消息框<br>
</div>
消息框类本级的信号也只有 1 个,如下信号:<br>
<div class="code">void buttonClicked(QAbstractButton *
button) //点击按钮信号,参数里是被点击的按钮对象<br>
</div>
<br>
消息框主要从基类 QDialog 和 QWidget 继承各种信号和槽函数,详见 11.1 节和 11.2 节内容。<br>
<br>
(2)静态成员函数<br>
消息框可以通过定义对象、调用普通成员函数方式使用,也可以通过最简单的静态成员函数来使用。<br>
静态成员函数仅需一句代码即可完成调用,非常便捷。<br>
针对询问、信息、警告、严重错误四种情况,对应的静态成员函数如下:<br>
<div class="code">StandardButton question(QWidget *
parent, const QString & title, const QString & text,
StandardButtons buttons = StandardButtons( Yes | No ), StandardButton
defaultButton = NoButton)<br>
StandardButton information(QWidget * parent, const
QString & title, const QString & text, StandardButtons buttons =
Ok, StandardButton defaultButton = NoButton)<br>
StandardButton warning(QWidget * parent, const QString
& title, const QString & text, StandardButtons buttons = Ok,
StandardButton defaultButton = NoButton)<br>
StandardButton critical(QWidget * parent, const QString
& title, const QString & text, StandardButtons buttons = Ok,
StandardButton defaultButton = NoButton)<br>
</div>
上面静态函数的参数都很类似,title 是标题文本,text 是基本文本,buttons 是设置的标准按钮,询问时为 Yes 和 No
按钮,其他情况默认为 Ok 按钮,defaultButton 默认 NoButton;返回值就是用户点击的标准按钮数值。<br>
静态函数调用非常简单,如下示例:<br>
<div class="code">int ret = QMessageBox::warning(this, tr("My Application"),<br>
tr("The document has been modified.\n"<br>
"Do you want to save your changes?"),<br>
QMessageBox::Save | QMessageBox::Discard<br>
| QMessageBox::Cancel,<br>
QMessageBox::Save);<br>
</div>
窗口标题文本 "My Application",基本文本 "The document has been modified.\nDo you want
to save your changes?" ,显示三个按钮 Save 、Discard、Cancel,默认焦点是 Save 按钮。<br>
<br>
另外,消息框还提供了两个用于显示本程序信息和 Qt 版本信息的静态函数:<br>
<div class="code">void about(QWidget * parent, const
QString & title, const QString & text) <br>
void aboutQt(QWidget * parent, const QString & title
= QString())<br>
</div>
about() 函数用于显示本程序的信息,title 是标题栏文本,text 一般为自定义的本程序介绍文本,比如 "某某软件 1.0
版本,版权归某某所有。" <br>
aboutQt() 函数显示关于所用 Qt 版本的信息,title 是窗口标题文本,窗口内的消息文本是 Qt 自行设置的,包括 Qt
版本信息、许可证信息等等。<br>
<br>
QMessageBox 类的内容介绍到这,下面我们通过一个简单文本编辑器示例熟悉消息框的使用。<br>
我们打开 QtCreator,新建一个 Qt Widgets Application 项目,在新建项目的向导里填写:<br>
①项目名称 simpletextedit,创建路径 D:\QtProjects\ch11,点击下一步;<br>
②套件选择里面选择全部套件,点击下一步;<br>
③基类选择 QWidget,注意修改主窗口类名为 WidgetSimpleTextEdit,然后点击下一步;<br>
④项目管理不修改,点击完成。<br>
由于使用 Qt 通用对话框,就不再需要额外新建自己的子窗口类和UI文件。<br>
我们打开 widgetsimpletextedit.ui 界面文件,拖入控件:<br>
<center> <img src="images/ch11/ch11-05-02.png" alt="ui1" width="800"></center>
第一行控件是“文件名”标签,单行编辑器 lineEditFileName,“浏览”按钮 pushButtonBrowser。<br>
第二行是纯文本编辑器 plainTextEdit,编辑器右侧还有 5 个按钮,<br>
“打开文件”按钮 pushButtonOpenFile,“保存文件”按钮 pushButtonSaveFile,“关闭文件”按钮
pushButtonCloseFile,“关于本程序”按钮 pushButtonAboutThis,“关于Qt”按钮
pushButtonAboutQt。<br>
第一行控件按照水平布局器排列,第二行先将右边 5 个按钮按照垂直布局器排列,再与纯文本编辑器一起按水平布局器排列。窗口整体按照垂直布局器排列,窗口大小
640 * 480 。<br>
<br>
完成界面布局后,依次右击节目 6 个按钮,为按钮添加 <strong>clicked()</strong> 信号的槽函数。<br>
然后右击纯文本编辑器 plainTextEdit ,为纯文本编辑器添加<b> textChanged() </b>信号的槽函数。<br>
槽函数添加完成后,我们保存并关闭 ui 文件,回到代码编辑界面。<br>
我们首先编辑头文件 widgetsimpletextedit.h 内容:<br>
<div class="code"><span style=" color:#000080;">#ifndef</span><span style=" color:#c0c0c0;">
</span>WIDGETSIMPLETEXTEDIT_H
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">#define</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">WIDGETSIMPLETEXTEDIT_H</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;"><QWidget></span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;"><QFile></span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;"><QFileDialog></span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">namespace</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">Ui</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">class</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">WidgetSimpleTextEdit</span><span style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">}</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">class</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">WidgetSimpleTextEdit</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">:</span><span style=" color:#c0c0c0;"> </span><span style=" color:#808000;">public</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QWidget</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000080;">Q_OBJECT</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">public</span><span style=" color:#000000;">:</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">explicit</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">WidgetSimpleTextEdit</span><span
style=" color:#000000;">(</span><span style=" color:#800080;">QWidget</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">*</span>parent<span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">0</span><span style=" color:#000000;">);</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">~</span><span style=" font-style:italic; color:#000000;">WidgetSimpleTextEdit</span><span
style=" color:#000000;">();</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">private</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#808000;">slots</span><span style=" color:#000000;">:</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">void</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">on_pushButtonBrowser_clicked</span><span
style=" color:#000000;">();</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">void</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">on_pushButtonOpenFile_clicked</span><span
style=" color:#000000;">();</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">void</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">on_pushButtonSaveFile_clicked</span><span
style=" color:#000000;">();</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">void</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">on_pushButtonCloseFile_clicked</span><span
style=" color:#000000;">();</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">void</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">on_pushButtonAboutThis_clicked</span><span
style=" color:#000000;">();</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">void</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">on_pushButtonAboutQt_clicked</span><span
style=" color:#000000;">();</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">void</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">on_plainTextEdit_textChanged</span><span
style=" color:#000000;">();</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">private</span><span style=" color:#000000;">:</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">Ui</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">WidgetSimpleTextEdit</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">*</span><span style=" color:#800000;">ui</span><span
style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//文件对象</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QFile</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_file</span><span
style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//保存文本是否被修改的状态</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">bool</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_bTextIsChanged</span><span
style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//根据编辑框内容写入文件</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">void</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">SaveTextFile</span><span
style=" color:#000000;">();</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">};</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">#endif</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">//</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">WIDGETSIMPLETEXTEDIT_H</span></pre>
</div>
我们添加了 QFile 和 QFileDialog 的头文件引用。<br>在类内容,6 个按钮的槽函数和纯文本编辑器的槽函数是通过界面编辑操作添加的。<br>
然后手动添加了成员文件对象 m_file,标志位 m_bTextIsChanged 表示文本是否被修改。<br>
添加了函数 SaveTextFile() ,用于将编辑器内容写入到文件中。<br><br>
接下来我们分段编辑源文件 widgetsimpletextedit.cpp 内容,首先是头文件包含和构造函数:<br>
<div class="code"><span style=" color:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">"widgetsimpletextedit.h"</span>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">"ui_widgetsimpletextedit.h"</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;"><QDebug></span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;"><QMessageBox></span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#800080;">WidgetSimpleTextEdit</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">WidgetSimpleTextEdit</span><span style=" color:#000000;">(</span><span
style=" color:#800080;">QWidget</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">*</span><span style=" color:#000000;">parent</span><span
style=" color:#000000;">)</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">:</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QWidget</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">parent</span><span
style=" color:#000000;">),</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">ui</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">new</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">Ui</span><span style=" color:#000000;">::</span><span style=" color:#800080;">WidgetSimpleTextEdit</span><span
style=" color:#000000;">)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">ui</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setupUi</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">this</span><span style=" color:#000000;">);</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//保存文本是否被修改的状态</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_bTextIsChanged</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#808000;">false</span><span style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">}</span></pre>
</div>
添加了 QDebug 和 QMessageBox 头文件包含。<br>
在构造函数里面添加了初始化代码,m_bTextIsChanged 默认为 false。<br>
然后是析构函数代码:<br>
<div class="code"><span style=" color:#800080;">WidgetSimpleTextEdit</span><span
style=" color:#000000;">::~</span><span style=" font-style:italic; color:#000000;">WidgetSimpleTextEdit</span><span
style=" color:#000000;">()</span>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//判断文本修改状态</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_bTextIsChanged</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//提示用户保存修改后的文本</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">int</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nRet</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">question</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">this</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span
style=" color:#008000;">"保存文件询问"</span><span style=" color:#000000;">),</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">tr</span><span
style=" color:#000000;">(</span><span style=" color:#008000;">"是否保存文件编辑的内容?"</span><span
style=" color:#000000;">),</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">Yes</span><span style=" color:#000000;">|</span><span
style=" color:#800080;">QMessageBox</span><span style=" color:#000000;">::</span><span
style=" color:#800080;">No</span><span style=" color:#000000;">,</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">Yes</span><span style=" color:#000000;">);</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">Yes</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">==</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nRet</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//保存一下文件</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">SaveTextFile</span><span
style=" color:#000000;">();</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">}</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">}</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//关闭文件</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_file</span><span
style=" color:#000000;">.</span><span style=" font-style:italic; color:#000000;">close</span><span
style=" color:#000000;">();</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//清空编辑框</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">ui</span><span
style=" color:#000000;">-></span><span style=" color:#800000;">plainTextEdit</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">clear</span><span
style=" color:#000000;">();</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//删除界面</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">delete</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">ui</span><span style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">}</span></pre></div>
在析构函数里,我们添加了对 m_bTextIsChanged 标志位的检查,如果为 true,说明文本修改了但未保存,<br>弹出消息框提示用户是否需要保存文件编辑内容,如果用户选择 Yes 按钮,那么调用 SaveTextFile() 保存文件。<br>然后关闭文件,清空纯文本编辑器,最后删除界面。<br><br>
接下来是“浏览”按钮对应槽函数代码:<br>
<div class="code"><span style=" color:#008000;">//浏览文件</span>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">void</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">WidgetSimpleTextEdit</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">on_pushButtonBrowser_clicked</span><span
style=" color:#000000;">()</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//文件名</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strFileName</span><span
style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strFileName</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">QFileDialog</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">getOpenFileName</span><span style=" color:#000000;">(</span><span
style=" color:#808000;">this</span><span style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span style=" color:#008000;">"选择文本文件"</span><span
style=" color:#000000;">),</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">tr</span><span
style=" color:#000000;">(</span><span style=" color:#008000;">""</span><span style=" color:#000000;">),</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span style=" color:#008000;">"Text</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">files</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">(*.txt);;All</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">files</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">(*)"</span><span style=" color:#000000;">));</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">strFileName</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">isEmpty</span><span
style=" color:#000000;">())</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//没有文件名</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">return</span><span
style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">}</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//正常文件名</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">ui</span><span
style=" color:#000000;">-></span><span style=" color:#800000;">lineEditFileName</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setText</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">strFileName</span><span
style=" color:#000000;">);</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">}</span></pre>
</div>
该函数先定义字符串对象 strFileName,然后调用 QFileDialog::getOpenFileName() 获取要打开的文件名,<br>然后判断文件名是否为空,如果为空就不处理返回;<br>文件名不为空,就将获取的文件名显示到文件名编辑器里。<br><br>
接下来是“打开文件”按钮对应槽函数代码:<br>
<div class="code"><span style=" color:#008000;">//打开文件</span>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">void</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">WidgetSimpleTextEdit</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">on_pushButtonOpenFile_clicked</span><span
style=" color:#000000;">()</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//获取文件名</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strFileName</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800000;">ui</span><span style=" color:#000000;">-></span><span
style=" color:#800000;">lineEditFileName</span><span style=" color:#000000;">-></span><span
style=" color:#000000;">text</span><span style=" color:#000000;">().</span><span
style=" color:#000000;">trimmed</span><span style=" color:#000000;">();</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//判断文件名是否为空</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strFileName</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">isEmpty</span><span
style=" color:#000000;">()</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">warning</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">this</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span
style=" color:#008000;">"文件名检查"</span><span style=" color:#000000;">),</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span
style=" color:#008000;">"文件名为空,请先浏览选择文本文件。"</span><span style=" color:#000000;">));</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">return</span><span
style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">}</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//判断文本修改状态</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_bTextIsChanged</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//提示用户保存修改后的文本</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">int</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nRet</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">question</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">this</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span
style=" color:#008000;">"保存文件询问"</span><span style=" color:#000000;">),</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">tr</span><span
style=" color:#000000;">(</span><span style=" color:#008000;">"是否保存上一个文件编辑的内容?"</span><span
style=" color:#000000;">),</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">Yes</span><span style=" color:#000000;">|</span><span
style=" color:#800080;">QMessageBox</span><span style=" color:#000000;">::</span><span
style=" color:#800080;">No</span><span style=" color:#000000;">,</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">Yes</span><span style=" color:#000000;">);</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">Yes</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">==</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nRet</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//保存一下文件</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">SaveTextFile</span><span
style=" color:#000000;">();</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">}</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">}</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//关闭旧文件</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_file</span><span
style=" color:#000000;">.</span><span style=" font-style:italic; color:#000000;">close</span><span
style=" color:#000000;">();</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//清空编辑框</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">ui</span><span
style=" color:#000000;">-></span><span style=" color:#800000;">plainTextEdit</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">clear</span><span
style=" color:#000000;">();</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//尝试打开文件</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_file</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">setFileName</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strFileName</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">);</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">bool</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">bRet</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_file</span><span
style=" color:#000000;">.</span><span style=" font-style:italic; color:#000000;">open</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QIODevice</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">ReadWrite</span><span
style=" color:#000000;">|</span><span style=" color:#800080;">QIODevice</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">Text</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">);</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//判断是否打开</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">!</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">bRet</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//没有打开,报错</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">warning</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">this</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span
style=" color:#008000;">"文件打开报错"</span><span style=" color:#000000;">),</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">tr</span><span
style=" color:#000000;">(</span><span style=" color:#008000;">"指定文件无法打开,请检查文件是否存在,是否有读写权限。"</span><span
style=" color:#000000;">));</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//没有文件,没有编辑</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_bTextIsChanged</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#808000;">false</span><span style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//修改保存按钮文本,不带</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">*</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">ui</span><span
style=" color:#000000;">-></span><span style=" color:#800000;">pushButtonSaveFile</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setText</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span
style=" color:#008000;">"保存文件"</span><span style=" color:#000000;">));</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">return</span><span
style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">}</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//文件成功打开</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QByteArray</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">baData</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800000;">m_file</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">readAll</span><span style=" color:#000000;">();</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//转为</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">QString</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QString</span><span