-
Notifications
You must be signed in to change notification settings - Fork 7
/
ch09-03.htm
3077 lines (2006 loc) · 198 KB
/
ch09-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>ch09-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">9.3 关联容器:QMap、QMultiMap</div>
<br>
本节介绍两种基于红黑树的关联容器,单映射 QMap、多映射 QMultiMap。
前面介绍的顺序容器通常存储大量连续序号的元素,而关联容器则存储离散的“不正常序号”的元素。 数组和顺序容器的序号都是从 0
开始,逐渐递增,而关联容器没有正常的序号, 单映射和单哈希映射通过重载中括号[]运算符函数,它们的元素序号可以是各种奇葩的数值类型,
比如用字符串、图片、颜色、日期、字体等数据作为序号,像 map["红色"] = 0xFF0000 就是单映射的例子。 本节分为两个小节内容,依次介绍
QMap、QMultiMap ,并各安排一个示例使用这些关联容器。 <br>
<br>
<div class="os2">9.3.1 单映射 QMap </div>
<br>
QMap 模板类通常将数据按照 key - value(键-值) 配对的形式存储,程序中以 key 作为序号来查询对应的 value,比如
map["红色"] = 0xFF0000 。QMap 通常将一个 key 映射为一个 value,而 QMultiMap 通常将 一个 key
映射为多个 value,这就是单映射和多映射的区别。<br>
QMap 模板类 key 和 value 的类型可以是C++和Qt常用的数据类型,也可以使用自定义类型,<b>但是注意key 和 value
都必须是可赋值类型!</b><br>
QMap 使用红黑树存储 key - value 配对数据( 一对 key - value 后文统称为映射中一个元素,或者红黑树的一个节点),排序时按照
key 来比较大小,因此对 key 类型有额外要求:<br>
<b>key 类型必须提供全局的双参数 operator<() 比较数值大小。</b><br>
<br>
下面我们分类列举QMap模板类的功能函数:<br>
(1)构造函数<br>
<div class="code"> QMap() //默认构造函数<br>
QMap(std::initializer_list<std::pair<Key, T>
> list) //初始化列表构造函数<br>
QMap(const QMap<Key, T> & other) //复制构造函数<br>
QMap(QMap<Key, T> && other)
//移动构造函数<br>
QMap(const std::map<Key, T> & other)
//根据标准库的映射构造 QMap<br>
~QMap() //析构函数</div>
最常用是默认构造函数,注意使用模板类时要带 key 和 value 的类型,例如定义姓名和年龄的映射:<br>
<div class="code">QMap<QString, int> nameAge;</div>
QString 作为 key 类型,保存姓名,int 作为 value 类型,保存年龄。<br>
C++11特性支持初始化列表构造和移动构造,比如:<br>
<div class="code"> QMap<QString, int> nameAge{
{"Alice", 20},<br>
{"Bob", 22},<br>
{"Cell", 19} };<br>
qDebug()<<nameAge;<br>
QMap<QString, int> nameAgeOther = std::move(
nameAge );<br>
qDebug()<<nameAge;<br>
qDebug()<<nameAgeOther;</div>
上面一段代码的输出如下所示:<br>
<div class="output"> QMap(("Alice", 20)("Bob", 22)("Cell", 19))<br>
QMap()<br>
QMap(("Alice", 20)("Bob", 22)("Cell", 19))<br>
</div>
<br>
移动构造函数会将 nameAge 内所有元素都转移给 nameAgeOther,因此 nameAge 最后变成空的,而 nameAgeOther
会包含原本 nameAge 拥有的元素。<br>
<br>
std::map 是标准库的映射模板类,包含在 <map> 头文件中,使用方法与 QMap 类似,如果需要从标准库的 map 构造 QMap
对象,可以参照下面代码编写:<br>
<div class="code"> std::map<QString , int>
astdMap{ {"Alice", 20},<br>
{"Bob", 22},<br>
{"Cell", 19} };<br>
QMap<QString, int> nameAge( astdMap );</div>
注意 std::map 和 QMap 的 key、value 类型必须要一致,才能这样构造。<br>
<br>
(2)添加函数<br>
本小节的元素是指一对 key - value ,添加元素后,QMap 内部的红黑树会新加一个节点。<br>
向 QMap 添加新元素有三种方式,第一种通过两个 insert() 函数:<br>
<div class="code">iterator insert(const Key & key,
const T & value) //直接插入一对 key - value<br>
iterator insert(const_iterator pos, const Key & key,
const T & value)//在建议的迭代器 pos 位置插入一对 key - value</div>
第一个 insert() 函数不关心元素插入位置,直接添加到红黑树中;<br>
第二个 insert() 函数是建议在迭代器 pos 位置插入元素,但是建议的位置不一定有效,红黑树是按照 key
大小排序的,实际插入的位置是按照排序规则确定的,pos 位置不一定有效,所以一般插入元素推荐用第一个 insert() 函数。<br>
<br>
向 QMap 添加新元素第二种方式,就是运算符重载函数 operator[]() :<br>
<div class="code">T & QMap::operator[](const Key & key)</div>
上面两种插入元素方式举例:<br>
<div class="code"> QMap<QString, int> nameAge;<br>
nameAge.insert( "Alice", 20 );<br>
nameAge["Bob"] = 22;<br>
nameAge["Cell"] = 19;</div>
中括号的方式用起来更像数组,语法也相对简单一些。<b><br>
如果 key - value 配对已经存储在映射对象里面了,那么继续用相同 key 调用 insert() 或 operator[]() ,<br>
由于单映射的特性,结果就是 key 不变,而 key 对应的 value 会被新的 value 覆盖。</b><br>
<b> insert() 或 operator[]() 在映射对象里没有 key 时添加新元素,而当已存在 key 时用新value覆盖旧的value。</b><br>
<br>
向 QMap 添加新元素的第三种方式是 insertMulti() 函数,即红黑树会插入一对多映射节点,放到后面介绍,一般不建议使用。<br>
<br>
(3)移除和删除函数<br>
从映射对象卸下一个元素,但不释放空间,使用下面函数:<br>
<div class="code">T take(const Key & key)</div>
返回值 T 是 value 类型的数值。如果映射对象里根本没有指定 key,那么返回值是 value 类型默认构造函数生成的对象。<br>
如果不需要返回值,直接从映射对象删除指定 key 及其 value,使用下面函数:<br>
<div class="code">int remove(const Key & key)</div>
返回值是删除元素的个数,如果返回值为 0,说明映射里没有该 value;如果为 1 ,说明正好删除了一对 key - value
;如果返回值大于1,说明程序之前使用 insertMulti() 函数为一个 key 添加了多个 value 值(QMap 允许一对多映射,多个
key-value 元素 的 key 值相同,但一般不建议这样做)。<br>
如果希望清空所有元素,那么使用如下函数:<br>
<div class="code">void clear()<br>
</div>
<br>
(4)访问和查询函数<br>
查询映射对象内是否包含 key 键:<br>
<div class="code">bool contains(const Key & key) const</div>
查询映射对象内所有元素数目:<br>
<div class="code">int count() const<br>
int size() const</div>
统计 key 对应的 value 值数量,使用下面函数:<br>
<div class="code">int count(const Key & key) const</div>
如果映射对象不存在 key 键,那么返回值为 0,如果存在一对 key-value ,那么返回值为 1;如果程序之前使用 insertMulti()
函数为一个 key 添加了多个 value,那么返回值是多个 value 值的数量。<br>
判断映射对象是否全空,没有元素,使用下面两个函数都可以:<br>
<div class="code">bool empty() const
//STL风格<br>
bool isEmpty() const //Qt风格</div>
<br>
获取映射中第一个 value 值,使用下面函数:<br>
<div class="code">T & first()<br>
const T & first() const</div>
获取映射中第一个 key 键,使用下面函数:<br>
<div class="code">const Key & firstKey() const</div>
获取映射中最后一个 value 值,使用下面函数:<br>
<div class="code">T & last()<br>
const T & last() const</div>
获取映射中最后一个 key 键,使用下面函数:<br>
<div class="code">const Key & lastKey() const</div>
<br>
根据已知 value 值反查归属的 key 键:<br>
<div class="code">const Key key(const T & value, const
Key & defaultKey = Key()) const</div>
反查耗时比较长,需要逐个遍历元素,注意多个 key 对应的 value 可能一样,所以上面函数只返回第一个匹配的 key。如果找不到就返回默认构造的
Key() 。<br>
如果要根据 value 反查所有匹配的 key 键列表,使用下面函数:<br>
<div class="code">QList<Key> keys(const T &
value) const</div>
如果需要获取映射所有元素的 key 值列表,使用下面函数:<br>
<div class="code">QList<Key> keys() const</div>
注意 insertMulti() 函数可能导致多个 key-value 元素的 key 值一样,keys()
获取的键值是可能重复的。如果希望获取不重复出现的 key 列表,使用下面函数:<br>
<div class="code">QList<Key> uniqueKeys() const</div>
根据 key 查询对应 value ,使用下面函数:<br>
<div class="code">const T value(const Key & key, const
T & defaultValue = T()) const</div>
如果没有找到 key-value 元素,那么返回 T() 值,就是 value 类型默认构造值。<br>
存在一对多映射的情况下,可以用下面函数获取 key 对应的多个 value 值列表:<br>
<div class="code">QList<T> values(const Key &
key) const</div>
如果要获取映射中所有元素的 value 值列表,使用下面函数:<br>
<div class="code">QList<T> values() const</div>
<br>
(5)交换函数<br>
将映射对象自身的元素与另一个映射对象中的元素全部互换,使用下面函数:<br>
<div class="code">void swap(QMap<Key, T> &
other)</div>
swap() 函数效率非常高,并且从不失败。<br>
<br>
(6)运算符函数<br>
对于运算符函数,我们以下面两个映射来举例说明:<br>
<div class="code"> QMap<QString, int> m1;<br>
m1["Alice"] = 20;<br>
QMap<QString, int> m2;<br>
m2["Bob"] = 22;<br>
m2["Cell"] = 19;</div>
运算符使用示范如下表所示:<br>
<br>
<table class="tabel">
<tbody>
<tr class="d1">
<td style="width: 400px;" align="center"><b>运算符函数</b></td>
<td style="width: 200px;" align="center"><span style="font-weight: bold;">举
例</span></td>
<td align="center"><b> 描述</b></td>
</tr>
<tr>
<td>bool operator!=(const QMap<Key, T> & other) const</td>
<td> m1 != m2;</td>
<td> 两个映射的元素不一样,不等号判断结果为 true。 </td>
</tr>
<tr class="d1">
<td>bool operator==(const QMap<Key, T> & other) const</td>
<td> m1 == m2;</td>
<td> 两个映射的元素不一样,等于号判断结果为 false。</td>
</tr>
<tr>
<td>QMap<Key, T> & operator=(const QMap<Key, T> &
other)</td>
<td> m1 = m2;</td>
<td> 将 m2 所有元素复制给 m1,执行后二者相等。 </td>
</tr>
<tr class="d1">
<td>QMap<Key, T> & operator=(QMap<Key, T> &&
other)</td>
<td> m1 = std::move(m2);</td>
<td> 将 m2 中所有元素移动给m1,m2自己清空。 </td>
</tr>
<tr>
<td>T & operator[](const Key & key)</td>
<td> m2["Cell"]= 18;</td>
<td style="height: 16px;"> 修改了 "Cell" 对应的value值。 </td>
</tr>
<tr class="d1">
<td>const T operator[](const Key & key) const</td>
<td style="height: 16px;"> qDebug()<< m1["Alice"];</td>
<td> 打印 "Alice" 对应的常量值。 </td>
</tr>
</tbody>
</table>
<br>
(7)迭代器函数<br>
映射类也定义了 STL 风格和 Qt 命名风格的迭代器:<br>
<div class="code">class const_iterator
//STL风格只读迭代器<br>
class iterator
//STL风格读写迭代器<br>
typedef ConstIterator //Qt 风格只读迭代器<br>
typedef
Iterator //Qt风格读写迭代器</div>
STL 风格迭代器使用示范:<br>
<div class="code">QMap<QString, int>::const_iterator i =
map.constBegin();<br>
while (i != map.constEnd()) {<br>
cout << i.key() << ": " << i.value()
<< endl;<br>
++i;<br>
}<br>
</div>
获取映射的头部元素、尾部假想元素的迭代器函数列举如下:<br>
<div class="code">iterator begin()
//指向头部的读写迭代器<br>
const_iterator begin() const
//指向头部的只读迭代器<br>
const_iterator cbegin() const
//指向头部的只读迭代器<br>
const_iterator constBegin() const
//指向头部的只读迭代器,Qt风格<br>
iterator end()
//指向尾部后面假想元素的读写迭代器<br>
const_iterator end() const
//指向尾部后面假想元素的只读迭代器<br>
const_iterator cend() const
//指向尾部后面假想元素的只读迭代器<br>
const_iterator constEnd() const
//指向尾部后面假想元素的只读迭代器,Qt风格</div>
注意 *end() 返回的迭代器通常只用来做不等于判断,它指向的东西根本不存在, *end() 仅用于越界判断。<br>
虽然获取头部、尾部迭代器的函数多,其实功能类似,起了一堆名字是方便兼容 STL 风格函数命名。<br>
查找指定 key 键对应的迭代器位置,使用下面函数:<br>
<div class="code">iterator find(const Key &
key) //根据指定key查找所在位置的读写迭代器<br>
const_iterator find(const Key & key)
const
//根据指定key查找所在位置的只读迭代器,STL风格<br>
const_iterator constFind(const Key & key)
const //根据指定key查找所在位置的只读迭代器,Qt风格</div>
如果存在 key 键的一对多映射,返回排在最前面的 key 节点迭代器,相同 key 的多个节点会排在该迭代器之后连续位置;<br>
如果找不到包含指定 key 的元素,那么查找函数返回 end() 尾部虚假元素迭代器,注意判断几个 *find 函数的返回值。<br>
根据读写迭代器指定位置,可以删除该位置元素:<br>
<div class="code">iterator erase(iterator pos)</div>
删除后返回指向下一元素的迭代器,注意可能是 end() 位置。<br>
<br>
QMap 在绝大多数情况下,都是用于一对一映射,但是它也提供了一对多映射的接口函数:<br>
<div class="code"> //插入指定键值对,如果之前有该键的元素,那么不会替换旧元素,直接增加新的元素,造成一对多映射<br>
iterator insertMulti(const Key & key, const T &
value) //插入一对多映射元素<br>
iterator insertMulti(const_iterator pos, const Key &
key, const T & value) //在建议的 pos 位置附近插入一对多映射元素</div>
insertMulti() 会直接向映射添加键值对,并且不会替换旧的相同键值节点,所以会造成多个节点拥有相同的键值,一般不建议这样调用,Qt 专门封装了
QMultiMap 类用于处理一对多映射。<br>
<br>
由于 insertMulti() 会造成一对多映射,红黑树是排序树,同样的 key 键节点是按照排序相邻的,迭代器位置是连续排布的,可以获取一个 key
对应的多个元素迭代器范围:<br>
<div class="code">QPair<iterator, iterator>
equal_range(const Key & key) //返回一对迭代器<br>
//QPair<iterator, iterator>
中,第一个迭代器是排在最前面的key键节点位置,第二个是排在最后的key键节点位置<br>
</div>
<br>
根据红黑树的排序特性,QMap 还提供了查找 key 下边界和上边界的迭代器函数:<br>
<div class="code">iterator lowerBound(const Key &
key) //查找 key 下边界的读写迭代器<br>
const_iterator lowerBound(const Key & key) const
//查找 key 下边界的只读迭代器<br>
iterator upperBound(const Key &
key) //查找 key 上边界的读写迭代器<br>
const_iterator upperBound(const Key & key) const
//查找 key 上边界的只读迭代器 </div>
key 下边界的意思是按照从小到大顺序,找到红黑树中首个满足如下条件的 keyLB 节点: <br>
keyLB >= key<br>
如果映射存在 key 节点,那么返回第一个键值等于 key 的节点迭代器;<br>
如果没有 key 节点,那么返回首个大于 key 的节点迭代器。举例如下:<br>
<div class="code">QMap<int, QString> map;<br>
map.insert(1, "one");<br>
map.insert(5, "five");<br>
map.insert(10, "ten");<br>
<br>
map.lowerBound(0); // returns iterator to
(1, "one")<br>
map.lowerBound(1); // returns iterator to
(1, "one")<br>
map.lowerBound(2); // returns iterator to
(5, "five")<br>
map.lowerBound(10); // returns iterator to (10,
"ten")<br>
map.lowerBound(999); // returns end()</div>
key 上边界的意思是按照从小到大顺序,找到红黑树中首个满足如下条件的节点 keyUB:<br>
keyUB > key<br>
上边界总是返回大于 key 的最邻近节点迭代器。<br>
举例如下:<br>
<div class="code">QMap<int, QString> map;<br>
map.insert(1, "one");<br>
map.insert(5, "five");<br>
map.insert(10, "ten");<br>
<br>
map.upperBound(0); // returns iterator to
(1, "one")<br>
map.upperBound(1); // returns iterator to
(5, "five")<br>
map.upperBound(2); // returns iterator to
(5, "five")<br>
map.upperBound(10); // returns end()<br>
map.upperBound(999); // returns end()</div>
<br>
(8)容器类型转换函数<br>
Qt 提供了映射类,STL 也有自己映射类,两种映射类互相转换的函数如下:<br>
<div class="code">std::map<Key, T> toStdMap()
const //转为 STL 映射类<br>
QMap(const std::map<Key, T> & other) //构造函数,根据 STL 映射构造 QMap 映射<br>
</div>
QMap 另外支持两个映射合并,将参数 other 映射的元素全部复制添加给自己,如果两个映射都包含相同的 key,那么合并类似
insertMulti() 造成一对多映射:<br>
<div class="code">QMap<Key, T> & unite(const
QMap<Key, T> & other)</div>
insertMulti() 和 unite() 会无脑添加新元素,即使新的键值对与旧节点完全一样,比如:<br>
<div class="code"> QMap<QString, int> m1;<br>
m1["Alice"] = 20;<br>
QMap<QString, int> m2;<br>
m2["Alice"] = 20;<br>
<br>
m1.unite( m2 );<br>
m1.insertMulti("Alice",20 );<br>
<br>
qDebug()<<m1<<endl<<m2;</div>
打印结果是 m1 包含三个相同节点:<br>
<div class="output"> QMap(("Alice", 20)("Alice", 20)("Alice", 20)) <br>
QMap(("Alice", 20))<br>
</div>
<br>
(9)其他内容<br>
映射类也支持数据串行化,进行数据流输入和输出,但注意前提是 key 和 value 的类型都必须支持串行化:
<div class="code">QDataStream &
operator<<(QDataStream & out, const QMap<Key, T>
& map) //串行化输出<br>
QDataStream & operator>>(QDataStream & in,
QMap<Key, T> & map)
//串行化输入</div>
<br>
关于 QMap,这里再提醒两个注意事项:<br>
<b>① 不能使用 map[key] 这种形式查找映射里是否包含键 key 的元素,因为 operator[](const Key & key)
函数在找不到 key 元素时,自动调用 value 类默认构造函数为映射添加 key-value 新元素。</b><br>
应该用 contains(key) 来判断是否包含该 key 元素,或者用 map.value( key ) 函数查找值,value()
函数不会为映射添加新元素,虽然 value() 函数找不到时也会返回 value 类型默认构造的值,但不会改变映射内容。<br>
<br>
<b>② 一般不建议使用 QMap 的 insertMulti() 和 unite() 函数进行多重映射添加或映射合并,Qt 单独提供了
QMultiMap 表示多重映射,在程序中尽量让 QMap 保持一对一映射,避免代码的误解。</b><br>
<br>
下面开始本小节的示例程序:<br>
我们打开 QtCreator,新建一个 Qt Widgets Application 项目,在新建项目的向导里填写:<br>
①项目名称 nameage,创建路径 D:\QtProjects\ch09,点击下一步;<br>
②套件选择里面选择全部套件,点击下一步;<br>
③基类选择 QWidget,点击下一步;<br>
④项目管理不修改,点击完成。<br>
我们打开 widget.ui 界面文件,按照下图拖入控件:<br>
<center> <img src="images/ch09/ch09-03-01.png" alt="ui" width="800"></center>
我们首先将窗口宽度设为 480,高度 300,这样能放下四个并排的按钮,然后逐个拖入控件:<br>
第一行是标签控件,文本为 "姓名-年龄映射" 。<br>
第二行为列表控件,对象名 listWidget。<br>
第三行控件为:标签 "姓名",单行编辑器 lineEditName,标签 "年龄",旋钮编辑框 spinBoxAge,第三行控件按水平布局器排列。<b>注
意设置旋钮编辑框的 sizePolicy 属性,将水平策略设置为 Expanding。</b><br>
第四行为四个按钮:"添加元素" 按钮 pushButtonAdd,"删除匹配姓名元素" 按钮 pushButtonDel,"查找匹配姓名元素" 按钮
pushButtonFindName,"查找匹配年龄元素" 按钮 pushButtonFindAge,四个按钮也使用水平布局排列。<br>
第五行是丰富文本编辑框,对象名 textEdit,该编辑框默认文本设置为 "结果显示" 。<br>
窗口整体使用垂直布局,窗口大小 480*300 。<br>
<br>
控件布局设置好之后,我们依次右键点击 4 个按钮,在右键菜单选择“转到槽...”,弹出如下对话框:<br>
<center><img src="images/slots/buttonclicked.png" alt="slot"></center>
选择信号 clicked() ,点击 OK 按钮,为 4 个按钮都添加对应的槽函数。<br>
槽函数添加完成后,我们保存界面文件,关闭界面文件,然后对头文件 widget.h 进行编辑:<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:#000080;">#ifndef</span><span style=" color:#c0c0c0;"> </span>WIDGET_H</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;">#define</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">WIDGET_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;"><QMap></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:#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;">Widget</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;">Widget</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;">Widget</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;">Widget</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_pushButtonAdd_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_pushButtonDel_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_pushButtonFindName_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_pushButtonFindAge_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:#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;">Widget</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;">QMap</span><span
style=" color:#000000;"><</span><span style=" color:#800080;">QString</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#808000;">int</span><span
style=" color:#000000;">></span><span style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_mapNameAge</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;">showNameAgeMap</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;">WIDGET_H</span></pre>
</div>
在文件开头,我们添加了 QMap 类的头文件包含;<br>Widget 类内部有 4 个槽函数是通过右键菜单添加的;<br>Widget 类末尾添加了映射类对象 m_mapNameAge,key 类型为 QString,value 类型为 int;<br>然后添加了 showNameAgeMap() 函数,专门枚举映射对象的内容,显示到界面的列表控件。<br>
<br>接下来我们分段编辑源文件 widget.cpp 的内容,首先是构造函数和析构函数部分:<br>
<div class="code"><span style=" color:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">"widget.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_widget.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=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#800080;">Widget</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">Widget</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;">Widget</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=" 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;">spinBoxAge</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setRange</span><span
style=" color:#000000;">(</span><span style=" color:#000080;">0</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000080;">600</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;">textEdit</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setReadOnly</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">true</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_mapNameAge</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">insert</span><span
style=" color:#000000;">(</span><span style=" color:#008000;">"Alice"</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">20</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;">m_mapNameAge</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">insert</span><span
style=" color:#000000;">(</span><span style=" color:#008000;">"Bob"</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">23</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;">m_mapNameAge</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">insert</span><span
style=" color:#000000;">(</span><span style=" color:#008000;">"Cass"</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">23</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;">m_mapNameAge</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">insert</span><span
style=" color:#000000;">(</span><span style=" color:#008000;">"Daff"</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">21</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;">showNameAgeMap</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:#800080;">Widget</span><span style=" color:#000000;">::~</span><span
style=" font-style:italic; color:#000000;">Widget</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:#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>
文件开头添加了调试信息类和消息框类的头文件包含。<br>构造函数里添加了几行代码,首先将年龄控件 spinBoxAge 的取值范围设置为 0~600 ;<br>设置文本编辑框 textEdit 为只读模式,避免用户修改结果编辑框内容;<br>然后为成员变量 m_mapNameAge 添加了四个键值对;<br>构造函数最后调用 showNameAgeMap() 将映射对象的内容显示到列表控件。<br>
析构函数的代码没有修改,下面我们来编辑 showNameAgeMap() 函数代码:<br>
<div class="code"><span style=" color:#808000;">void</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">Widget</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">showNameAgeMap</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:#800000;">ui</span><span
style=" color:#000000;">-></span><span style=" color:#800000;">listWidget</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">clear</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;">//获取所有</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">key</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;">QList</span><span
style=" color:#000000;"><</span><span style=" color:#800080;">QString</span><span
style=" color:#000000;">></span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">listKeys</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800000;">m_mapNameAge</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">keys</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;">nCount</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">listKeys</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">count</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;">for</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">int</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">i</span><span style=" color:#000000;">=</span><span style=" color:#000080;">0</span><span
style=" color:#000000;">;</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">i</span><span
style=" color:#000000;"><</span><span style=" color:#000000;">nCount</span><span
style=" color:#000000;">;</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">i</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><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">key-value</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;">curText</span><span
style=" color:#c0c0c0;"> </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;">"%1\t%2"</span><span
style=" color:#000000;">).</span><span style=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">listKeys</span><span
style=" color:#000000;">[</span><span style=" color:#000000;">i</span><span style=" color:#000000;">]</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">).</span><span style=" color:#000000;">arg</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_mapNameAge</span><span
style=" color:#000000;">[</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">listKeys</span><span
style=" color:#000000;">[</span><span style=" color:#000000;">i</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;">);</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;">listWidget</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">addItem</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">curText</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:#000000;">}</span></pre></div>
showNameAgeMap() 内部先清空了列表控件内容;<br>获取 m_mapNameAge 映射所有的键列表;<br>所有键的数量也就是映射中元素的个数,将数量保存到 nCount ;<br>然后循环遍历所有的键、值内容,构造文本,添加给 listWidget 。<br>showNameAgeMap() 是通过获取所有键列表的方式遍历映射对象,也可以直接用迭代器方式遍历,在上面迭代器函数介绍部分有示范代码。<br>
<br>接下来逐个编辑按钮槽函数的代码,首先是“添加”按钮的槽函数:<br>
<div class="code"><span style=" color:#808000;">void</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">Widget</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">on_pushButtonAdd_clicked</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;">key</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;">strName</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;">lineEditName</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:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strName</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;">"检查姓名key"</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:#800000;">m_mapNameAge</span><span
style=" color:#000000;">[</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strName</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">]</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;">spinBoxAge</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">value</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;">showNameAgeMap</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:#800000;">textEdit</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setText</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;">);</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>
该槽函数首先获取姓名编辑框的字符串,存到 strName ,并判断字符串是否为空,空字符串不处理直接返回;<br>对于非空字符串,继续后面代码;<br>使用中括号的方式,为映射 m_mapNameAge 添加键值对;<br>然后调用 showNameAgeMap() 显示添加新元素后的映射到列表控件;<br>最后在结果显示编辑框里,显示已添加新元素的信息。<br>
<br>“删除匹配姓名元素”按钮槽函数代码如下:<br>
<div class="code"><span style=" color:#808000;">void</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">Widget</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">on_pushButtonDel_clicked</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;">key</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;">strName</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;">lineEditName</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:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strName</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;">"检查姓名key"</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><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">key</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:#800000;">m_mapNameAge</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">contains</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strName</span><span
style=" color:#c0c0c0;"> </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:#800000;">ui</span><span
style=" color:#000000;">-></span><span style=" color:#800000;">textEdit</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setText</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;">);</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><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">key</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;">m_mapNameAge</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">remove</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strName</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:#000000;">showNameAgeMap</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:#800000;">textEdit</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setText</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;">);</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>调用 m_mapNameAge.contains() 函数判断映射里是否存在键 strName ,如果不存在就不处理;<br>如果存在键 strName ,那么调用 m_mapNameAge.remove() 删除匹配的元素;<br>最后更新列表控件的显示,并将已经删除元素的信息到结果编辑框。<br><br>添加和删除操作修改了映射 m_mapNameAge 的内容,因此需要调用 showNameAgeMap() 更新列表控件的显示,后面的查询按钮操作没有修改映射内容,就不需要更新列表控件。<br>
<br>“查找匹配姓名元素”按钮的槽函数代码如下:<br>
<div class="code"><span style=" color:#808000;">void</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">Widget</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">on_pushButtonFindName_clicked</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;">key</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;">strName</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;">lineEditName</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:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strName</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;">"检查姓名key"</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><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">key</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:#800000;">m_mapNameAge</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">contains</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strName</span><span
style=" color:#c0c0c0;"> </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:#800000;">ui</span><span
style=" color:#000000;">-></span><span style=" color:#800000;">textEdit</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setText</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;">);</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><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">key</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">只有一个</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">value</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:#c0c0c0;"> </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:#c0c0c0;"> </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"</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=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span style=" color:#000000;">strName</span><span
style=" color:#000000;">).</span><span style=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_mapNameAge</span><span
style=" color:#000000;">[</span><span style=" color:#000000;">strName</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:#800000;">ui</span><span
style=" color:#000000;">-></span><span style=" color:#800000;">textEdit</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setText</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strResult</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:#000000;">}</span></pre></div>
该槽函数类似地先获取姓名字符串,并判断是否为空,空字符串不处理,非空字符串才进行后面处理;<br>调用 m_mapNameAge.contains() 判断映射是否包含键 strName ,<br>如果不包含键就显示 "指定姓名不存在。" 的信息到结果编辑框;<br>如果包含,就显示已找到的信息到结果编辑框。<br>本示例是一对一的映射,映射中所有 key 都是唯一的,所以一个 key 只有一个 value,不需要考虑多重映射。<br>
<br>最后是“查找匹配年龄元素”按钮的槽函数代码:<br>
<div class="code"><span style=" color:#808000;">void</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">Widget</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">on_pushButtonFindAge_clicked</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;">int</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nAge</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;">spinBoxAge</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">value</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;">QList</span><span
style=" color:#000000;"><</span><span style=" color:#800080;">QString</span><span
style=" color:#000000;">></span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">listNames</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_mapNameAge</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">keys</span><span style=" color:#000000;">(</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nAge</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:#800080;">QString</span><span
style=" color:#c0c0c0;"> </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:#c0c0c0;"> </span><span style=" color:#808000;">int</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nCount</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">listNames</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">count</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:#000000;">nCount</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:#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:#000000;">strResult</span><span
style=" color:#c0c0c0;"> </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;">}</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;">else</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;">{</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;">tr</span><span style=" color:#000000;">(</span><span style=" color:#008000;">"找到了匹配年龄的元素</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">%1</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">个,列举如下:\r\n"</span><span style=" color:#000000;">).</span><span
style=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">nCount</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;">for</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">int</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">i</span><span style=" color:#000000;">=</span><span style=" color:#000080;">0</span><span
style=" color:#000000;">;</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">i</span><span
style=" color:#000000;"><</span><span style=" color:#000000;">nCount</span><span
style=" color:#000000;">;</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">i</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;">strResult</span><span
style=" color:#c0c0c0;"> </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;">"%1\t%2\r\n"</span><span
style=" color:#000000;">).</span><span style=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span
style=" color:#000000;">listNames</span><span style=" color:#000000;">[</span><span
style=" color:#000000;">i</span><span style=" color:#000000;">]).</span><span style=" color:#000000;">arg</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">nAge</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