-
Notifications
You must be signed in to change notification settings - Fork 7
/
ch03-03.htm
2653 lines (1713 loc) · 156 KB
/
ch03-03.htm
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>ch03-03</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">3.3 使用 QString</div>
<br>
本节介绍 QString 的常见使用,包含 C++ 基本类型和 QString 的互相转换、QString 涉及的运算符、QString
子串查询和操作、利用 QTextStream 对 QString 做输入输出操作等,最后通过一个示例集成测试函数,展示 QString
用法。本节内容较多,可分几次尝试代码,凡是原理性质的内容需要理解,而罗列性质的内容不用死记的,可以到用的时候查看文档。本节示例代码下载地址在最后的小节
3.3.6 里,可以先下载示例对照学习。<br>
<br>
<div class="os2">3.3.1 QString和QChar简介</div>
<br>
QString 是由一系列 16 bit 字符 QChar 组成的字符串,以 NULL 字符结尾(末尾的 NULL 不计入字符串长度)。QChar
是一个 Unicode 4.0 标准的字符,对于超过 16bit 范围的国际码字符,QString 里采用相邻的一对 QChar 来表示。QString
使用的其实是 UTF-16 的双字节编码,tr 函数就是将 UTF-8 变长编码的字符串转成 QString 运行时的内码。UTF-8
编码是属于通用的存储交换格式,但这种编码的缺点就是一个字符的长度不固定,这对字符串操作效率是有影响的,因为得先确定每个字符的长度。因此 QString
采用固定长度字符单元的 UTF-16 编码,这对程序运行时字符串比较、查询操作效率更高。上一节 3.2.4 表格中 utf16() 和
unicode() 函数都没有用 to 前缀,因为这两个函数没有做转换,它们返回的就是 QString 运行时的内码,同 data()
函数。tr 函数不仅可以用于支持国际化翻译,并且能自动将字符串的存储交换格式 UTF-8 转换成运行时的 UTF-16 内码,返回转换过后得到的
QString 对象。<br>
<br>
字符串之间经常有手动复制或者通过函数参数、函数返回值等复制操作,QString 为了优化内存使用效率,避免出现大量相同内容的字符串副本,QString
对复制过程采用隐式共享机制(implicit sharing),比如执行字符串对象 str1 = str2
时,如果这两个对象字符串内容都没有后续改变,那么它们会指向同一块字符串数据,而如果其中之一发生改变,字符串数据块的复制过程才会发生,这样能最大程度地节省内存,而
且在传 QString 类型参数或返回值时,避免了大量数据块的复制过程,优化了程序运行效率。<br>
<br>
QString 内码是 UTF-16,而标准 C++ 的字符串是 UTF-8 编码的,Qt 针对标准 C++ 字符串也提供了 QByteArray
类,用于操作 UTF-8 编码以及其他本地化字符串(如 GBK、Big5)、字节数组(不以 NULL 结尾的纯数据)等,QByteArray
类下一节讲解。<br>
<br>
<div class="os2">3.3.2 基本类型与字符串互相转换</div>
<br>
在编程时经常会出现把数值如 800 转成字符串 "800",或者反过来把字符串转成数值等情况,本小节罗列 C++ 基本的数值类型和 Qt
对这些类型的别称,然后展示这些基本类型和 QString 对象的互相转换,并编写一些测试函数来示范效果。<br>
<br>
<table class="tabel">
<tbody>
<tr class="d1">
<td style="width: 180px;" align="center"><b>基本类型</b></td>
<td style="width: 180px;" align="center"><b>Qt别称</b></td>
<td style="width: 120px;" align="center"><b>转入函数</b></td>
<td style="width: 120px;" align="center"><b>转出函数</b></td>
<td align="center"><b>描述</b></td>
</tr>
<tr>
<td style="width: 180px;"><b>short</b></td>
<td style="width: 180px;"><b>qint16</b></td>
<td style="width: 120px;"><b>arg或setNum</b></td>
<td style="width: 120px;"><b>toShort</b></td>
<td> 2 字节长度,有符号短整型。 </td>
</tr>
<tr class="d1">
<td style="width: 180px;"><b>unsigned short</b></td>
<td style="width: 180px;"><b>ushort、quint16</b></td>
<td style="width: 120px;"><b>arg或setNum</b></td>
<td style="width: 120px;"><b>toUShort</b></td>
<td> 2 字节长度,无符号短整型。 </td>
</tr>
<tr>
<td style="width: 180px;"><b>int</b></td>
<td style="width: 180px;"><b>qint32</b></td>
<td style="width: 120px;"><b>arg或setNum</b></td>
<td style="width: 120px;"><b>toInt</b></td>
<td> 4 字节长度,有符号整型。 </td>
</tr>
<tr class="d1">
<td style="width: 180px;"><b>unsigned int</b></td>
<td style="width: 180px;"><b>uint、quint32</b></td>
<td style="width: 120px;"><b>arg或setNum</b></td>
<td style="width: 120px;"><b>toUInt</b></td>
<td> 4 字节长度,无符号整型。 </td>
</tr>
<tr>
<td style="width: 180px;"><b>long</b></td>
<td style="width: 180px;"><b>无</b></td>
<td style="width: 120px;"><b>arg或setNum</b></td>
<td style="width: 120px;"><b>toLong</b></td>
<td> 有符号长整型,对于 32 位编程 long 是 4 字节长度,对于 64 位编程是 8 字节长度。 </td>
</tr>
<tr class="d1">
<td style="width: 180px;"><b>unsigned long</b></td>
<td style="width: 180px;"><b>ulong</b></td>
<td style="width: 120px;"><b>arg或setNum</b></td>
<td style="width: 120px;"><b>toULong</b></td>
<td> 无符号长整型,对于 32 位编程 unsigned long 是 4 字节长度,对于 64 位编程是 8 字节长度。 </td>
</tr>
<tr>
<td style="width: 180px;"><b>long long</b></td>
<td style="width: 180px;"><b>qlonglong、qint64</b></td>
<td style="width: 120px;"><b>arg或setNum</b></td>
<td style="width: 120px;"><b>toLongLong</b></td>
<td> 8 字节长度,有符号长长整型。 </td>
</tr>
<tr class="d1">
<td style="width: 180px;"><b>unsigned long long</b></td>
<td style="width: 180px;"><b>qulonglong、quint64</b></td>
<td style="width: 120px;"><b>arg或setNum</b></td>
<td style="width: 120px;"><b>toULongLong</b></td>
<td> 8 字节长度,无符号长长整型。 </td>
</tr>
<tr>
<td style="width: 180px;"><b>float</b></td>
<td style="width: 180px;"><b>默认情况下无</b></td>
<td style="width: 120px;"><b>arg或setNum</b></td>
<td style="width: 120px;"><b>toFloat</b></td>
<td> 4 字节长度,单精度浮点数。 </td>
</tr>
<tr class="d1">
<td style="width: 180px;"><b>double</b></td>
<td style="width: 180px;"><b>默认情况对应 qreal</b></td>
<td style="width: 120px;"><b>arg或setNum</b></td>
<td style="width: 120px;"><b>toDouble</b></td>
<td> 8 字节长度,双精度浮点数。 </td>
</tr>
</tbody>
</table>
<br>
这些基本的数值类型转为 QString 对象都是使用重载的 arg 或 setNum 函数,而 QString
对象转出为其他类型使用单独命名的函数。Qt 对这些类型的别称都定义在头文件 <QtGlobal> 里面,由于其他绝大多数 Qt
头文件都包含了该全局头文件,所以通常不需要自己手动去包含它的。对于上表需要说明的两点:一是 long 和 ulong
长度是根据操作系统和编译器确定的,32 位编程就是 32 位,64 位编程就是 64 位;二是实数 qreal 默认情况下都是对应 double
,例外情况是在编译 Qt 类库本身时配置了 -qreal float 选项参数,这种例外情况极少,通常都不用管的。<br>
首先来介绍一下转入函数,对于整数类型,setNum 函数声明是完全类似的,以 int 为例:<br>
<div class="code"> QString & setNum(int n, int base = 10)</div>
第一个参数就是需要转换的整数,第二个参数是转换之后的目标字符串进制基数,比如转成十六进制字符串、八进制字符串等,默认是转成十进制的字符串。setNum
函数设置好字符串内容后返回 QString 对象自身的引用。<br>
<br>
对于浮点数类型,setNum 函数声明有些区别,以 double 为例:<br>
<div class="code"> QString & QString::setNum(double n, char format =
'g', int precision = 6)</div>
第一个参数是需要转换的浮点数,第二个是转换之后的目标字符串格式('e', 'E', 'f', 'g' ,
'G'),第三个是目标字符串显示的浮点数精度,默认是 6 。浮点数的格式是与 C 语言类似的,如下所述:<br>
<ul>
<li>'e':科学计数法,小写 e,如 [-]9.9e[±]999。</li>
<li>'E':科学计数法,大写 E,如 [-]9.9E[±]999。</li>
<li>'f':定点数显示,[-]9.9。</li>
<li> 'g':自动选择用科学计数法或定点数显示,哪种方式最简洁就用哪个,科学计数法的 e 小写。</li>
<li> 'G':自动选择用科学计数法或定点数显示,哪种方式最简洁就用哪个,科学计数法的 E 大写。</li>
</ul>
setNum 函数示范代码:<br>
<div class="code"> <span style=" color:#808000;">void</span><span style=" color:#c0c0c0;">
</span><span style=" color:#000000;">Test_setNum</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:#800080;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strTest</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;">//to</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">Hex</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">string</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;">short</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">numHex</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">127</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;">strTest</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">setNum</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">numHex</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">16</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:#000080;">qDebug</span><span
style=" color:#000000;">()<<</span><span style=" color:#008000;">"Hex:</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">"</span><span style=" color:#000000;"><<</span><span
style=" color:#000000;">strTest</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;">//to</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">Oct</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">string</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;">numOct</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">63</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;">strTest</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">setNum</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">numOct</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">8</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:#000080;">qDebug</span><span
style=" color:#000000;">()<<</span><span style=" color:#008000;">"Oct:</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">"</span><span style=" color:#000000;"><<</span><span
style=" color:#000000;">strTest</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;">//to</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">normal</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">Dec</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">string</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;">long</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">numDec</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">800</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;">strTest</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">setNum</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">numDec</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:#000080;">qDebug</span><span
style=" color:#000000;">()<<</span><span style=" color:#008000;">"Normal:</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">"</span><span style=" color:#000000;"><<</span><span
style=" color:#000000;">strTest</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;">//to</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">float</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">string</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;">float</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">numFixed</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">123.78999</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;">strTest</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">setNum</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">numFixed</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">'f'</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">3</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:#000080;">qDebug</span><span
style=" color:#000000;">()<<</span><span style=" color:#008000;">"Fixed:</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">"</span><span style=" color:#000000;"><<</span><span
style=" color:#000000;">strTest</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;">//to</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">scientific</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">double</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">string</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;">double</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">numScientific</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">456.78999</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;">strTest</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">setNum</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">numScientific</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">'e'</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">6</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:#000080;">qDebug</span><span
style=" color:#000000;">()<<</span><span style=" color:#008000;">"Scientific:</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">"</span><span style=" color:#000000;"><<</span><span
style=" color:#000000;">strTest</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>
这个测试函数运行结果就不贴出来了,读者自己手动去试试看。<br>
<br>
接下来重点介绍 arg 函数,这是最常用也是最具特色的。arg
函数无所不包,它的参数可以是数值类型、字符串类型,并且可以串联,格式化参数里还可以指定顺序、重复使用参数等等。对于数值类型,它的声明与 setNum
比较类似,以 int 和 double 为例:<br>
<div class="code"> QString arg(int a, int fieldWidth = 0, int base = 10,
QChar fillChar = QLatin1Char( ' ' )) const </div>
<div class="code">QString arg(double a, int fieldWidth = 0, char format =
'g', int precision = -1, QChar fillChar = QLatin1Char( ' ' )) const</div>
注意 arg 函数声明末尾的 const,这个函数不会改变字符串对象本身的内容,而是会返回一个全新的 QString
对象,所以使用这个函数时,必须用它的返回值。<br>
对于整数类型,它的声明多出来两个:fieldWidth 是指生成的目标字符串宽度,0 表示自动设置长度,最后的 fillChar
是填充字符,如果设置的域宽比较大,多余的空位就会使用这个填充字符填满。<br>
对于浮点数类型,多出来的 fieldWidth 也是生成的目标字符串宽度,fillChar 也是填充字符。默认的填充字符是空格,QLatin1Char
代表一个字节长度的拉丁字符,与 ASCII 码字符差不多。QLatin1Char 有对应的类
QLatin1String,因为仅支持单字节拉丁字符,不支持国际化,它应用的比较少。 <br>
arg 函数比 setNum 函数功能更强大,可以设置目标字符串宽度和填充字符。arg
函数还可以用字符串作为参数,可以将一个字符串填充到另一个里面,比如下面这个函数声明:<br>
<div class="code">QString arg(const QString & a, int fieldWidth = 0,
QChar fillChar = QLatin1Char( ' ' )) const</div>
这个声明和数值类型声明差不多,也可以设置目标字符串宽度和填充字符。<br>
函数声明介绍到这,下面看看这个函数该怎么用。arg 函数的使用方式很特别,它的串联方式也很灵活,来看看示例代码:<br>
<div class="code"> <span style=" color:#808000;"></span><span style=" color:#808000;">void</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">Test_arg</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><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">strResult</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">存储</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">arg</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:#800080;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strResult</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;">//Dec</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;">long</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">numDec</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">800</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;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strMod</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">QObject</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span style=" color:#008000;">"Normal:</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">%1"</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;">strResult</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">strMod</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span style=" color:#000000;">numDec</span><span
style=" color:#000000;">);</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//%1是占位符,第一个arg函数参数变量转后的字符串填充到</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">%1</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:#000080;">qDebug</span><span
style=" color:#000000;">()<<</span><span style=" color:#008000;">"Mod:</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">"</span><span style=" color:#000000;"><<</span><span
style=" color:#000000;">strMod</span><span style=" color:#000000;"><<</span><span
style=" color:#008000;">"</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">\t</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">Result:</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">"</span><span style=" color:#000000;"><<</span><span
style=" color:#000000;">strResult</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;">//Oct</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;">numOct</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">63</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;">strResult</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">QObject</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span style=" color:#008000;">"Oct:</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">%1"</span><span style=" color:#000000;">).</span><span
style=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span style=" color:#000000;">numOct</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">4</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">8</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QChar</span><span
style=" color:#000000;">(</span><span style=" color:#008000;">'0'</span><span style=" color:#000000;">));</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//numOct转换后为4字符域宽,8进制,填充0</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;">qDebug</span><span
style=" color:#000000;">()<<</span><span style=" color:#000000;">strResult</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;">//Hex</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;">short</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">numHex</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">127</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;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strPrefix</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">QObject</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span style=" color:#008000;">"0x"</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;">strResult</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">QObject</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span style=" color:#008000;">"Hex:</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">%1%2"</span><span
style=" color:#000000;">).</span><span style=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span
style=" color:#000000;">strPrefix</span><span style=" color:#000000;">).</span><span
style=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span style=" color:#000000;">numHex</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">0</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">16</span><span
style=" color:#000000;">);</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//串联:第一个arg函数参数填充到%1,第二个arg填充到%2</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;">qDebug</span><span
style=" color:#000000;">()<<</span><span style=" color:#000000;">strResult</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;">//double</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;">double</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">numReal</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">123.78999</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;">strResult</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">QObject</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span style=" color:#008000;">"Fixed:</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">%1</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">\t</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">Scientific:</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">%2"</span><span style=" color:#000000;">).</span><span
style=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span style=" color:#000000;">numReal</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">0</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">'f'</span><span
style=" color:#000000;">).</span><span style=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span
style=" color:#000000;">numReal</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000080;">0</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">'e'</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000080;">3</span><span style=" color:#000000;">);</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:#000080;">qDebug</span><span
style=" color:#000000;">()<<</span><span style=" color:#000000;">strResult</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:#808000;">int</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">one</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">1</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;">int</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">two</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">2</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;">int</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">three</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">3</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;">strResult</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">QObject</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span style=" color:#008000;">"%1</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">小于</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">%2,%1</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">小于</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">%3,%3</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">大于</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">%2</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">。"</span><span
style=" color:#000000;">).</span><span style=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span
style=" color:#000000;">one</span><span style=" color:#000000;">).</span><span style=" color:#000000;">arg</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">two</span><span style=" color:#000000;">).</span><span
style=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span style=" color:#000000;">three</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:#000080;">qDebug</span><span
style=" color:#000000;">()<<</span><span style=" color:#000000;">strResult</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>
上面都是通过 tr 函数封装了一个临时的 QString 对象,然后调用该临时对象的 arg
函数实现数值类型转成格式化字符串,填充到占位符里面。这个工作原理与 sprintf 等 C 语言函数类似,sprintf 函数使用 %n 、%s
之类的格式占位符,QString 的实现方式不一样,它使用 % 加数字的占位方式,%1 对应后面串联的第一个 arg 函数,%2 对应后面串联的第二个
arg 函数,以此类推。具体的 %1 或 %2 等替换后的格式,由对应的 arg 函数来决定,QString 里有非常多的重载 arg 函数,每个
arg 函数对应一个类型,因此 %1 既可以填充数值类型转化后的格式化字符串,也可以填充其他原有的字符串。下面逐个解释一下各个 arg 函数意义:<br>
<div class="code"><span style=" color:#c0c0c0;"><span style=" color:#808000;"></span></span><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">
long</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">numDec</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000080;">800</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:#c0c0c0;"> </span><span style=" color:#800080;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strMod</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">QObject</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span style=" color:#008000;">"Normal:</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">%1"</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;">strResult</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">strMod</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span style=" color:#000000;">numDec</span><span
style=" color:#000000;">);</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//%1是占位符,第一个arg函数参数变量转后的字符串填充到</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">%1</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:#000080;">qDebug</span><span
style=" color:#000000;">()<<</span><span style=" color:#008000;">"Mod:</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">"</span><span style=" color:#000000;"><<</span><span
style=" color:#000000;">strMod</span><span style=" color:#000000;"><<</span><span
style=" color:#008000;">"</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">\t</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">Result:</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">"</span><span style=" color:#000000;"><<</span><span
style=" color:#000000;">strResult</span><span style=" color:#000000;">;</span></pre>
<span style=" color:#000000;"></span><span style=" color:#c0c0c0;"></span></div>
这是最简单的形式,tr函数生成的 strMod 对象里面只有一个占位符 %1 ,arg 函数会将整数 numDec 转成十进制数字符串,然后根据
strMod 构造一个新的字符串对象,并将十进制数字符串填充到占位符 %1 位置。原本的 strMod 不会改变,arg
函数会返回全新的字符串对象,然后复制给了 strResult。qDebug 打印的结果就是:<br>
<span style=" font-family:'Courier'; font-size:10pt; color:#aa00aa;">Mod:
"Normal: %1" Result:
"Normal: 800"</span><br>
<div class="code"><span style=" color:#c0c0c0;"> </span><span style=" color:#808000;">
int</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">numOct</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000080;">63</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:#c0c0c0;"> </span><span style=" color:#000000;">strResult</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">QObject</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span style=" color:#008000;">"Oct:</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">%1"</span><span style=" color:#000000;">).</span><span
style=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span style=" color:#000000;">numOct</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">4</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">8</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QChar</span><span
style=" color:#000000;">(</span><span style=" color:#008000;">'0'</span><span style=" color:#000000;">));</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//numOct转换后为4字符域宽,8进制,填充0</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;">qDebug</span><span
style=" color:#000000;">()<<</span><span style=" color:#000000;">strResult</span><span
style=" color:#000000;">;</span></pre>
</div>
这里 arg 函数是将普通数字 63 用八进制数来显示,要转换的数值是 numOct,设置 numOct 转换后的子串至少 4
字符宽度,用八进制显示,空位用字符 '0' 填充。qDebug 打印的结果就是:<br>
<span style=" font-family:'Courier'; font-size:10pt; color:#aa00aa;">"Oct:
0077"</span><br>
<div class="code"><span style=" color:#c0c0c0;"> </span><span style=" color:#808000;">
short</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">numHex</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000080;">127</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:#c0c0c0;"> </span><span style=" color:#800080;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strPrefix</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">QObject</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span style=" color:#008000;">"0x"</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;">strResult</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">QObject</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span style=" color:#008000;">"Hex:</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">%1%2"</span><span
style=" color:#000000;">).</span><span style=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span
style=" color:#000000;">strPrefix</span><span style=" color:#000000;">).</span><span
style=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span style=" color:#000000;">numHex</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">0</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">16</span><span
style=" color:#000000;">);</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//串联:第一个arg函数参数填充到%1,第二个arg填充到%2</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;">qDebug</span><span
style=" color:#000000;">()<<</span><span style=" color:#000000;">strResult</span><span
style=" color:#000000;">;</span></pre>
</div>
这里使用了串联的两个 arg 函数,第一个 arg 函数是填充原有字符串 strPrefix 到 %1 位置,第二个 arg 函数填充 numHex
转换后的十六进制字符串到 %2 位置。第二个 arg 函数参数里的 0 是指不限制域宽,转换后的十六进制字符串该多长就多长,参数 16
是十六进制的意思。占位符本身是没有格式信息的,填充的具体内容由后面串联的 arg
函数决定,想填充原有字符串就填充原有的字符串,想填充转换后的数字字符串,那就填充数字字符串,非常方便。qDebug 打印的结果为:<br>
<span style=" font-family:'Courier'; font-size:10pt; color:#aa00aa;">"Hex:
0x7f"</span><br>
<div class="code"><span style=" color:#c0c0c0;"> </span><span style=" color:#808000;">
double</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">numReal</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000080;">123.78999</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:#c0c0c0;"> </span><span style=" color:#000000;">strResult</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">QObject</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span style=" color:#008000;">"Fixed:</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">%1</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">\t</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">Scientific:</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">%2"</span><span style=" color:#000000;">).</span><span
style=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span style=" color:#000000;">numReal</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">0</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">'f'</span><span
style=" color:#000000;">).</span><span style=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span
style=" color:#000000;">numReal</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000080;">0</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">'e'</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000080;">3</span><span style=" color:#000000;">);</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:#000080;">qDebug</span><span
style=" color:#000000;">()<<</span><span style=" color:#000000;">strResult</span><span
style=" color:#000000;">;</span></pre>
</div>
这里展示的是浮点数转成字符串,第一个 arg 函数将 numReal 以定点数形式('f')转成字符串,0 代表不限制宽度,并填充到 %1 位置,没有设
置显示精度(默认为 6 位)。第二个 arg 函数将 numReal 以科学计数法形式('e')转成字符串,0 代表不限制宽度,3 代表显示精度为 3
位。qDebug 打印的结果为:<br>
<span style=" font-family:'Courier'; font-size:10pt; color:#aa00aa;">"Fixed:
123.789990 Scientific: 1.238e+02"</span><br>
<div class="code"><span style=" color:#c0c0c0;"> </span><span style=" color:#808000;">
int</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">one</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000080;">1</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:#c0c0c0;"> </span><span style=" color:#808000;">int</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">two</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">2</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;">int</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">three</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">3</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;">strResult</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">QObject</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span style=" color:#008000;">"%1</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">小于</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">%2,%1</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">小于</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">%3,%3</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">大于</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">%2</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">。"</span><span
style=" color:#000000;">).</span><span style=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span
style=" color:#000000;">one</span><span style=" color:#000000;">).</span><span style=" color:#000000;">arg</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">two</span><span style=" color:#000000;">).</span><span
style=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span style=" color:#000000;">three</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:#000080;">qDebug</span><span
style=" color:#000000;">()<<</span><span style=" color:#000000;">strResult</span><span
style=" color:#000000;">;</span></pre>
</div>
最后一段示例比较有意思,如果是 C 语言的 sprintf 要填充 6 个整型数,那必须用 6 个 %n ,不管有没有重复的。这里仅仅用了
%1、%2、%3,后面对应三个 arg 函数,每个 arg
函数都将参数里的变量转成数字字符串,并填充到正确的位置,而且可以重复填充。占位符的顺序也可以是乱的,规律就是第一个 arg 函数填充所有的 %1
,第二个 arg 函数填充所有的 %2 ,第三个 arg 函数填充所有的 %3 ,以此类推。因此 qDebug 打印的结果就是:<br>
<span style=" font-family:'Courier'; font-size:10pt; color:#aa00aa;">"1 小于
2,1 小于 3,3 大于 2 。"</span><br>
<br>
这正是我们希望看到的结果,可见 arg 函数的灵活性是传统 C 语言 sprintf 等无法比拟的,而且也更安全。学会 arg
函数用法,可应对各种复杂的格式化字符串转换。<br>
<br>
接下来简单看看 QString 的转出函数,可以将数字字符串转成各种类型的数值变量。对于整数类型,它们的函数声明都是类似的,以 int 为例:<br>
<div class="code">int QString::toInt(bool * ok = 0, int base = 10) const</div>
toInt 函数第一个参数 ok 接收一个 bool 变量的指针,用于反馈转换过程是否成功,第二个参数 base 是字符串对象里数字的进制基数,默认的
10 代表十进制,也可以设置二进制、八进制和十六进制等等。如果将 base 设置为 0,那么 toInt 函数将自动识别字符串对象里面的进制标识,对于
"0" 打头的自动按八进制转换,对于 "0x" 打头的自动按十六进制转换,其他情况都按十进制转换。<br>
如果转换出错,ok 指向的变量会设置为 false,返回值为 0 。<br>
<br>
对于浮点数字符串的转换,函数声明有些差异:<br>
<div class="code">double QString::toDouble(bool * ok = 0) const</div>
这个不能指定进制基数,都是十进制的,支持定点数字符串和浮点数字符串转成数值。参数 ok 接收一个 bool 变量的指针,用于反馈转换过程是否成功。<br>
如果转换失败,ok 指向的变量会设置为 false,返回值为 0。<br>
下面示范 QString 对象的转出函数:<br>
<div class="code"><span style=" color:#808000;">void</span><span style=" color:#c0c0c0;">
</span><span style=" color:#000000;">Test_toValue</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:#808000;">bool</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">bok</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;">//dec</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;">strDec</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">QObject</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span style=" color:#008000;">"800"</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;">int</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nDec</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strDec</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">toInt</span><span
style=" color:#000000;">(&</span><span style=" color:#000000;">bok</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">10</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:#000080;">qDebug</span><span
style=" color:#000000;">()<<</span><span style=" color:#000000;">nDec</span><span
style=" color:#000000;"><<</span><span style=" color:#008000;">"\t"</span><span
style=" color:#000000;"><<</span><span style=" color:#000000;">bok</span><span
style=" color:#000000;">;</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:#008000;">//Hex</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;">strHex</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">QObject</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span style=" color:#008000;">"FFFF"</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;">nDec</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">strHex</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">toInt</span><span style=" color:#000000;">(&</span><span
style=" color:#000000;">bok</span><span style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">10</span><span style=" color:#000000;">);</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:#000080;">qDebug</span><span
style=" color:#000000;">()<<</span><span style=" color:#000000;">nDec</span><span
style=" color:#000000;"><<</span><span style=" color:#008000;">"\t"</span><span
style=" color:#000000;"><<</span><span style=" color:#000000;">bok</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;">short</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nHexShort</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">strHex</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">toShort</span><span style=" color:#000000;">(&</span><span
style=" color:#000000;">bok</span><span style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">16</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:#000080;">qDebug</span><span
style=" color:#000000;">()<<</span><span style=" color:#000000;">nHexShort</span><span
style=" color:#000000;"><<</span><span style=" color:#008000;">"\t"</span><span
style=" color:#000000;"><<</span><span style=" color:#000000;">bok</span><span
style=" color:#000000;">;</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//FFFF正整数太大,超出范围,转换失败,没有负号</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">-</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">的都算正数。</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:#800080;">ushort</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nHexUShort</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">strHex</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">toUShort</span><span style=" color:#000000;">(&</span><span
style=" color:#000000;">bok</span><span style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">16</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:#000080;">qDebug</span><span
style=" color:#000000;">()<<</span><span style=" color:#000000;">nHexUShort</span><span
style=" color:#000000;"><<</span><span style=" color:#008000;">"\t"</span><span
style=" color:#000000;"><<</span><span style=" color:#000000;">bok</span><span
style=" color:#000000;">;</span><span style=" color:#008000;">//成功</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:#800080;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strOct</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">QObject</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span style=" color:#008000;">"0077"</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;">int</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nOct</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strOct</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">toInt</span><span
style=" color:#000000;">(&</span><span style=" color:#000000;">bok</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:#000080;">qDebug</span><span
style=" color:#000000;">()<<</span><span style=" color:#000000;">nOct</span><span
style=" color:#000000;"><<</span><span style=" color:#008000;">"\t"</span><span
style=" color:#000000;"><<</span><span style=" color:#000000;">bok</span><span
style=" color:#000000;">;</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//字符</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">0</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">打头自动按八进制转</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:#800080;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strHexWithPre</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">QObject</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span style=" color:#008000;">"0xFFFF"</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;">int</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nHexWithPre</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">strHexWithPre</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">toInt</span><span style=" color:#000000;">(&</span><span
style=" color:#000000;">bok</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:#000080;">qDebug</span><span
style=" color:#000000;">()<<</span><span style=" color:#000000;">nHexWithPre</span><span
style=" color:#000000;"><<</span><span style=" color:#008000;">"\t"</span><span
style=" color:#000000;"><<</span><span style=" color:#000000;">bok</span><span
style=" color:#000000;">;</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//字符</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">0x</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">打头自动按十六进制转</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;">int</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nDecAuto</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">strDec</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">toInt</span><span style=" color:#000000;">(&</span><span
style=" color:#000000;">bok</span><span style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">0</span><span style=" color:#000000;">);</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">//"800"</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:#000080;">qDebug</span><span
style=" color:#000000;">()<<</span><span style=" color:#000000;">nDecAuto</span><span
style=" color:#000000;"><<</span><span style=" color:#008000;">"\t"</span><span
style=" color:#000000;"><<</span><span style=" color:#000000;">bok</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:#800080;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strFixed</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">QObject</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span style=" color:#008000;">"123.78999"</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;">double</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">dblFixed</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">strFixed</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">toDouble</span><span style=" color:#000000;">(&</span><span
style=" color:#000000;">bok</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:#000080;">qDebug</span><span
style=" color:#000000;">()<<</span>fixed<span style=" color:#000000;"><<</span><span
style=" color:#000000;">dblFixed</span><span style=" color:#000000;"><<</span><span
style=" color:#008000;">"\t"</span><span style=" color:#000000;"><<</span><span
style=" color:#000000;">bok</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;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strScientific</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">QObject</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span style=" color:#008000;">"1.238e-5"</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;">double</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">dblScientific</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">strScientific</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">toDouble</span><span style=" color:#000000;">(&</span><span
style=" color:#000000;">bok</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:#000080;">qDebug</span><span
style=" color:#000000;">()<<</span>scientific<span style=" color:#000000;"><<</span><span
style=" color:#000000;">dblScientific</span><span style=" color:#000000;"><<</span><span
style=" color:#008000;">"\t"</span><span style=" color:#000000;"><<</span><span
style=" color:#000000;">bok</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>
上面代码的运行结果这里不贴出来了,读者自己动手去试试。对于两个浮点数打印的行,里面带有流操作子,类似标准 C++ 控制台输出对象 cout
的操作子,fixed 是指按定点数显示,scientific 是指按科学计数法显示。<br>
<br>
<div class="os2">3.3.3 字符串运算符</div>
<br>
QString 重载了多个对字符串有清晰意义的运算符,之前见过赋值运算符,可以将一个 QString 对象赋值给另一个 QString
对象。还有其他的比 较运算符和中括号运算符,先将其罗列如下:<br>
<br>
<table class="tabel">
<tbody>
<tr class="d1">
<td style="width: 120px;" align="center"><b> operator </b></td>
<td align="center"><b> 描述 </b></td>
</tr>
<tr>
<td style="width: 120px;"><b> = </b></td>
<td> 赋值运算符,遵循隐式共享规则,在赋值的两个对象有变化时才真正复制数据块。 </td>
</tr>
<tr class="d1">
<td style="width: 120px;"><b> += </b></td>
<td> 追加。将运算符左边和右边字符串拼接后,赋值给左边对象。 </td>
</tr>
<tr>
<td style="width: 120px;"><b> < </b></td>
<td> 小于号。左边字符串字典序比右边的靠前时,表达式为真。 </td>
</tr>
<tr class="d1">
<td style="width: 120px;"><b> <= </b></td>
<td> 小于等于。左边字符串字典序比右边的靠前或相同时,表达式为真。 </td>
</tr>
<tr>
<td style="width: 120px;"><b> == </b></td>
<td> 等于。二者字典序是一致的时候为真。 </td>
</tr>
<tr class="d1">
<td style="width: 120px;"><b> != </b></td>
<td> 不等于。二者字典序不一样的时候为真。 </td>
</tr>
<tr>
<td style="width: 120px;"><b> > </b></td>
<td> 大于。左边字符串字典序比右边的靠后时,表达式为真。 </td>
</tr>
<tr class="d1">
<td style="width: 120px;"><b> >= </b> </td>
<td> 大于等于。左边字符串字典序比右边的靠后或相同时,表达式为真。 </td>
</tr>
<tr>
<td style="width: 120px;"><b> [] </b></td>
<td> 类似数组取数的中括号,从指定位置取出 QChar 字符,另外还可以修改指定位置的 QChar 字符。 </td>
</tr>
<tr class="d1">
<td style="width: 120px;"><b> + </b> </td>
<td> 拼接。这是个友元函数,将两个字符串拼接后返回全新的字符串对象。 </td>
</tr>
</tbody>
</table>
<br>
上面运算符的意义是一目了然的,主要解释一下赋值运算符 = 的隐式共享(Implicit
Sharing),在执行赋值时,真正的字符串数据拷贝没有发生,这是为了优化运行效率,避免大量数据的拷贝。隐式共享实现方式就是对数据块做引用计数,多一个对象赋值或
参数、返回值拷贝时,引用次数加 1,这个赋值过程只需要设置一下数据指针和增加引用计数,不会真的拷贝大量数据,这种拷贝称为浅拷贝(shallow
copy)。<br>
在赋值的一个字符串发生变化,要做写入修改时,这个要发生变化的字符串会重新分配一块内存,将旧的数据拷贝到新的内存空间,并对其做相应的写入修改,这个过程叫深
拷贝(deep copy),也可称为 copy-on-write(写时拷贝)。深拷贝会将旧的数据块引用计数减
1,然后将变化的字符串数据指向新空间,新空间引用计数加 1。<br>
如果发生字符串超出生命期销毁或清空,那么对应的数据引用计数减 1,当引用计数减到 0 时,数据块空间才会真的被释放。<br>
Qt 对象能够赋值或传参数、返回值的,一般都是采用隐式共享机制,所以 Qt
的参数和返回值传递运行效率是很高的。这也将信号和槽机制传递参数的效率大大提升了。面向对象的高级编程语言一般都支持类似的功能,比如 Java 和
Python 的垃圾回收机制,也是类似的。<br>
<br>
下面通过简单示例展示运算符的使用:<br>
<div class="code">
<style type="text/css">
p, li { white-space: pre-wrap; }
</style> <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:#000000;">Test_operator</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><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;">strE1</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strE2</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strE3</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;">strE1</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">QObject</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span style=" color:#008000;">"abcd"</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;">strE2</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">strE1</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;">strE3</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">strE2</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:#000080;">qDebug</span><span
style=" color:#000000;">()<<</span><span style=" color:#000000;">strE1</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">data_ptr</span><span
style=" color:#000000;">()<<</span><span style=" color:#008000;">"\t"</span><span
style=" color:#000000;"><<</span><span style=" color:#000000;">strE2</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">data_ptr</span><span
style=" color:#000000;">()<<</span><span style=" color:#008000;">"\t"</span><span
style=" color:#000000;"><<</span><span style=" color:#000000;">strE3</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">data_ptr</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;">strE2</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">append</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QObject</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span
style=" color:#008000;">"1234"</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:#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:#000080;">qDebug</span><span
style=" color:#000000;">()<<</span><span style=" color:#000000;">strE1</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">data_ptr</span><span
style=" color:#000000;">()<<</span><span style=" color:#008000;">"\t"</span><span
style=" color:#000000;"><<</span><span style=" color:#000000;">strE2</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">data_ptr</span><span
style=" color:#000000;">()<<</span><span style=" color:#008000;">"\t"</span><span
style=" color:#000000;"><<</span><span style=" color:#000000;">strE3</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">data_ptr</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>