-
Notifications
You must be signed in to change notification settings - Fork 7
/
ch05-03.htm
2826 lines (1884 loc) · 179 KB
/
ch05-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>ch05-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">5.3 丰富文本编辑控件 </div>
<br>
对于多行普通文本编辑,Qt 提供 QPlainTextEdit 类,对于更为复杂的丰富文本编辑,Qt 提供了 QTextEdit 类,QTextEdit
有一个便于浏览丰富文本的派生类 QTextBrowser,相当于是 QTextEdit
只读版本,并另外做了一些打开网页链接的扩展功能。QPlainTextEdit 与 QTextEdit
采用的技术是差不多的,只是对普通文本编辑做了优化。本节重点介绍丰富文本编辑控件 QTextEdit
,学习编写一个简易的文本编辑器,然后介绍QTextBrowser 控件的扩展功能,根据 QTextBrowser 做一个简易的 HTML 查看器。<br>
<br>
<div class="os2">5.3.1 QTextEdit 类</div>
<br>
通过 Qt 助手可以直接索引查看到 QTextEdit 类的说明,这里讲一下帮助文档里面的内容。QTextEdit
是一个高级的所见即所得(WYSIWYG)浏览器/编辑器,支持丰富文本格式,类似 HTML 风格的标记。它被优化用于编辑大型文档和快速相应用户的输入。<br>
<br>
QTextEdit 基于段落和字符工作,段落是格式化的字符串,一般以换行符作为段落分隔标志,比如 C++ 字符串的 "\r\n" ,HTML 语言的
"<br>" 。QTextEdit
显示段落内部的文本时,会自动根据控件宽度,将段落内的长文本根据单词间隔进行自动换行(word-wrapped),类似 Windows
记事本里的自动换行功 能。<br>
<br>
QTextEdit 内部使用 QTextDocument 类管理文档,一篇文档可以有 0
个或多个段落组成,文本的对齐模式由其所属的段落对齐模式确定。对于段落内的文本字符,又可以有自己的字符格式,比如加粗、倾斜、字体、文字颜色、文字背景色等等。段落仅
仅是对文档组成部分的分隔和形容,并没有对应的 Qt
类。实际上编辑器内使用文本光标表示当前编辑位置,通常根据光标指示位置来编辑文本,文本光标是有具体的类,即 QTextCursor。<br>
<br>
需要注意的是,QTextEdit 不仅仅是一个编辑控件,更大程度上它是一个比较完备而又复杂的视图 + 文档体系,如下图所示:<br>
<center><img src="images/ch05/ch05-03-01.png" alt="textedit"></center>
基于 QTextEdit 可以开发出一个功能完备的丰富文本编辑程序,上图就是Qt 库自带编辑器主窗口程序例子,位于
C:\Qt\Qt5.4.0\Examples\Qt-5.4\widgets\richtext\textedit,感兴趣的读者可以提前去看看,以后还会专门讲这个复
杂例子,本节主要介绍简单的功能。<br>
<br>
QTextEdit 可以显示图片、列表和表格,如果文档太大,QTextEdit 自带滚动条,可以显示很多页的文档。QTextEdit
既可以编辑普通文本(plain text),也可以编辑丰富文本(rich text)。QTextEdit 支持的丰富文本格式是 HTML 4
标记语言的一个子集,在 Qt 助手里索引 Supported HTML Subset,可以查看具体支持哪些标记。<br>
<br>
因为 QTextEdit 和 QTextBrowser 都是仅支持 HTML 4 标记语言的子集,对于网页显示功能是不完备的,如果需要真正的网页浏览器功
能,那么建议使用更为强大的 Qt WebKit,在 Qt 助手索引里输入 Qt WebKit Widgets ,可以查看相应的文档。<br>
<br>
QTextEdit 同时具有两大功能,既可以作为显示控件使用,也可以作为丰富文本编辑器使用。QTextEdit
这两种用途是同时具备的,不需要什么设置,下面介绍仅仅是从函数功能分类来讲,在作为显示用途时,编辑器方面的函数管用,作为编辑器用途时,显示用的函数也是管用
的。<br>
<br>
作为显示控件时,可以使用三个函数设置需要显示的文本:<br>
<div class="code">void setHtml(const QString & text) </div>
<div class="code">void setPlainText(const QString & text)</div>
<div class="code">void setText(const QString & text)</div>
setHtml() 函数用于设置显示丰富的 HTML 网页文本,setPlainText() 函数用于设置显示普通的无格式文本。setText()
函数是通用的槽函数,它自动根据 text 内容猜测文本是不是 HTML 标记语言的,如果是 HTML
文本就显示丰富文本,如果不是那就当作普通无格式的文本显示。<br>
<br>
段落内的文本默认是根据控件宽度自动换行的,这样就不会用到水平的滚动条。对于长文本,如果不希望自动换行,那么可用 setLineWrapMode()
函数改变自动换行的特性,枚举常量 QTextEdit::NoWrap
就是不自动换行。如果是不自动换行,那么长的文本会导致水平滚动条显现,可以通过拖动水平滚动条查看长文本。另外,QTextEdit 自带查找函数
find() ,可以查找并高亮显示相应的文本。<br>
<br>
QTextEdit 作为编辑器使用时,常规的复制、粘贴、剪切、撤销、重做等功能毫无疑问都是默认支持的,不要编写额外代码,QTextEdit
自己就有这些完备的功能,右击它就能看到对应的编辑菜单。对于光标指示的当前文本字符,可以通过函数设置丰富的文本格式,下面列一个简表:<br>
<br>
<table class="tabel">
<tbody>
<tr class="d1">
<td style="width: 200px;" align="center"><b>槽函数</b></td>
<td style="width: 474px;" align="center"> 描述 </td>
</tr>
<tr>
<td><b>setFontItalic(bool)</b></td>
<td> 设置斜体字。 </td>
</tr>
<tr class="d1">
<td><b>setFontWeight(int)</b></td>
<td> 设置粗体字。 </td>
</tr>
<tr>
<td><b>setFontUnderline(bool)</b></td>
<td> 设置文字下划线。 </td>
</tr>
<tr class="d1">
<td><b>setFontFamily(QString)</b></td>
<td> 设置字体家族,如 "宋体"、"黑体"、"文泉驿黑体"。 </td>
</tr>
<tr>
<td><b>setFontPointSize(qreal)</b></td>
<td> 设置字号大小,如 9,12,16,48 等 。 </td>
</tr>
<tr class="d1">
<td><b>setTextColor(QColor)</b></td>
<td> 设置文字颜色。 </td>
</tr>
<tr>
<td><b>setTextBackgroundColor(QColor)</b></td>
<td> 设置文字背景色 。 </td>
</tr>
<tr class="d1">
<td><b>setCurrentFont(QFont)</b></td>
<td> 设置综合字体格式,QFont 类封装了上面所有格式。 </td>
</tr>
</tbody>
</table>
<br>
需要注意的是 setFontFamily(QString) 设置的才是如 "宋体"、"黑体"、"文泉驿黑体" 等字体家族,而
setCurrentFont(QFont) 是设置所有的综合字体,QFont 涵盖文字字符的所有格式,之前的斜体、粗体、字体家族、字体颜色等等,全包含在
QFont 内部。如果只希望设置某一方面的字体特性,可以用前面散装的快捷函数如
setFontItalic(bool)、setFontWeight(bool) 等,如果希望设置文本所有的综合字体格式,那么用最后的
setCurrentFont(QFont)。<br>
上面设置字体格式的函数全是槽函数,因此可以直接与其他控件的状态信号关联,比如列举字体家族的控件 QFontComboBox 的信号 <br>
<div class="code">void QFontComboBox::currentIndexChanged(const QString
& text)</div>
可以直接关联到 setFontFamily(QString) 槽函数。<br>
<br>
除了文本字符本身的格式,段落对齐是采用另外的槽函数:<br>
<div class="code">void QTextEdit::setAlignment(Qt::Alignment a)</div>
<br>
上面的字体设置函数影响的就是文本光标指示的单词或高亮选中的文本。光标和高亮选中的文本通过 QTextCursor
类管理,针对选中的文本,可以获取该文本片段的字体格式,也可以进行复制、粘贴、剪切、删除等操作。获取当前的光标对象使用函数:<br>
<div class="code">QTextCursor QTextEdit::textCursor() const</div>
因为文档里不同文字有各种各样的格式,当光标指向某个单词或选中文本片段时,会触发当前字符格式变化的信号:<br>
<div class="code">void QTextEdit::currentCharFormatChanged(const
QTextCharFormat & f)</div>
QTextCharFormat 与 QFont 不是继承关系,QTextCharFormat 内部包含有 QFont
成员变量,QTextCharFormat 与 QFont 有类似的各种字体格式函数,但 QTextCharFormat
更为强大,包含的更多的细节设置,比如文字在垂直方向的对齐。从 currentCharFormatChanged()
信号的参数里,可以提取当前选中的文本片段的各种字体格式,从而实时感知文档内部不同文本片段的丰富格式。与这个信号对应的设置函数是:<br>
<div class="code">void QTextEdit::setCurrentCharFormat(const
QTextCharFormat & format)</div>
但是 setCurrentCharFormat
不是槽函数,仅仅是公有成员函数。设置字体格式一般用之前列表里的多个快捷函数来设置。用户编辑文本时,新增加的文本也是根据
currentCharFormat 自动设置的。<br>
<br>
当 QTextEdit 内文本内容发生变化时,不论是用户手动增删还是快捷函数改变字体,都会触发文本变化信号:<br>
<div class="code">void QTextEdit::textChanged()</div>
这个信号没有带文本参数,需要用其他公有函数获取文本,获取完整的丰富文本的函数为:<br>
<div class="code">QString QTextEdit::toHtml() const</div>
获取无格式的普通文本的函数为:<br>
<div class="code">QString QTextEdit::toPlainText() const</div>
<br>
关于 QTextEdit 类的介绍暂时讲这么多,以后主窗口程序章节会专门讲 Qt
自带的丰富文本编辑器例子,到时候介绍更多的内容。接下来示范一个简化版的文本编辑器例子,可能丑陋了点,主要学习一下感知文本格式的变化和通过按钮修改选中文本的格式。<br>
<br>
<div class="os2">5.3.2 简易文本编辑器示例</div>
<br>
因为暂时还没学到用图标,我们这节先用文字按钮来表示字体格式。我们在按钮一节讲过单选按钮和复选框有选中状态和非选中状态等,普通的按压按钮也是可以有两种状态
的,通过基类 QAbstractButton 函数可以设置按压按钮的两种状态(Qt 设计师界面选中按钮,在右下角属性编辑器选中 checkable
属性也可以):<br>
<div class="code">void setCheckable(bool)</div>
<br>
二态按压按钮按下去之后不会自动弹起,按下去的状态就是 true,处于弹起状态就是 false。<br>
二态按压按钮的当前状态也可以通过 isChecked() 函数获取,或者在关联信号时,选择如下两个信号之一:<br>
<div class="code">void QAbstractButton::clicked(bool)</div>
<div class="code">void QAbstractButton::toggled(bool)</div>
信号 clicked(bool) 只在图形界面用户点击按钮时才触发,如果通过程序代码调用函数
setDown()、setChecked()、toggle() 改变按钮状态,不会触发 clicked(bool) 信号。<br>
而第二个信号 toggled(bool) 无论是用户点击改变,还是程序调用函数来改变,都会触发 toggled(bool) 信号。<br>
<br>
对于文本字体格式的粗体、斜体、下划线等,也是 bool
类型的两种状态,例子中就用二态按压按钮来表示。文本格式的按钮有两种用途,一是根据文档内光标移动,智能感知不同文本片段的格式变化,即接收
QTextEdit::currentCharFormatChanged<em></em>(QTextCharFormat)
信号;二是选中一段文本,点击格式按钮会修改选中文本的格式,即接收 QAbstractButton::clicked(bool)
信号。介绍完基本的知识,下面开始这个简易编辑器例子学习。<br>
<br>
打开 QtCreator,新建一个 Qt Widgets Application 项目,在新建项目的向导里填写:<br>
①项目名称 simpleeditor,创建路径 D:\QtProjects\ch05,点击下一步;<br>
②套件选择里面选择全部套件,点击下一步;<br>
③基类选择 QWidget,点击下一步;<br>
④项目管理不修改,点击完成。<br>
建好项目之后,打开窗体 widget.ui 文件,进入设计模式,把窗体大小设为 410*300,就是拉宽 10 像素,按照下图拖入控件:<br>
<center><img src="images/ch05/ch05-03-02.png" alt="ui" width="800"></center>
上图第一行是 5 个按压按钮,默认大小 75*23,按钮文本就如图上所示的设置, 5 个按钮的 objectName 依次为:<br>
pushButtonBold、pushButtonItalic、pushButtonUnderline、pushButtonColor、
pushButtonBGColor。<br>
<br>
第二行控件依次是 1 个标签,文本 "字号";1 个单行编辑控件,objectName 为 lineEditFontSize;<br>
1个标签,文本 "字体";1 个 Font Combo Box,objectName 为 fontComboBox,这个控件会自动枚举系统里的字体家族。<br>
第二行的控件高度统一为 22,方便对齐,控件长度可以用默认的,或者手动调整一下。<br>
<br>
第三行,就是占面积最大的 QTextEdit ,丰富文本编辑控件,objectName 为默认的 textEdit。<br>
注意调整各个控件的位置,看起来和上图类似对齐的就行了。<br>
<br>
先说一下界面的用途,第一行的 5 个按钮加上第二行的 单行编辑控件、字体枚举组合框,共 7 个,对应上面小节表格中前 7 个快捷函数,表格中最后一个综合
字体函数例子没用上。<br>
"粗体"、"斜体"、"下划线" 3 个用于二态按钮显示,等会会在代码里面设置为二态按钮。<br>
"前景色" 和 "背景色" 按钮会弹出颜色选取对话框,用户选择颜色后,会影响丰富文本编辑控件里选中的文本片段颜色。<br>
字号编辑控件和字体家族枚举组合框也是类似的,影响丰富文本编辑控件里选中的字体大小和家族。<br>
<br>
界面就是图上的那些,下面要为界面实现功能代码。我们要从 QtCreator 设计模式为各个控件的添加槽函数。<br>
① 为 "粗体"、"斜体"、"下划线" 3 个二态按钮都添加 clicked(bool) 信号对应的槽函数:<br>
<center><img src="images/ch05/ch05-03-03.png" alt="slot1" width="600"></center>
② 为 "前景色"、"背景色" 两个普通按钮添加普通的 clicked() 信号对应的槽函数:<br>
<center><img src="images/ch05/ch05-03-04.png" alt="slot2" width="600"></center>
③ 为字号单行编辑控件添加完成编辑的信号 editingFinished() 对应的槽函数:<br>
<center><img src="images/ch05/ch05-03-05.png" alt="slot3" width="600"></center>
editingFinished() 信号会在单行编辑控件失去输入焦点,或者用户在单行编辑控件里按回车键时触发。<br>
另外,字体枚举组合框不需要我们添加槽函数,等会在代码里直接调用 connect 函数。<br>
④ 为丰富文本编辑控件添加感应文字格式变化的信号 currentCharFormatChanged(QTextCharFormat) 对应的槽函数:<br>
<center><img src="images/ch05/ch05-03-06.png" alt="slot4" width="600"></center>
⑤ 再为丰富文本编辑控件添加文本内容发生变化的时信号 textChanged() 对应的槽函数:<br>
<center><img src="images/ch05/ch05-03-07.png" alt="slot5" width="600"></center>
添加好上述控件信号对应的槽函数之后,保存界面文件,然后进入 QtCreator 代码编辑模式,编写需要的功能代码。<br>
我们打开头文件 widget.h ,为头文件添加一行包含:<br>
<div class="code"><span style=" color:#000080;">#ifndef</span><span style=" color:#c0c0c0;">
</span>WIDGET_H
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">#define</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">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;"><QTextCharFormat></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_pushButtonBold_clicked</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">bool</span><span style=" color:#c0c0c0;"> </span>checked<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_pushButtonItalic_clicked</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">bool</span><span style=" color:#c0c0c0;"> </span>checked<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_pushButtonUnderline_clicked</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">bool</span><span style=" color:#c0c0c0;"> </span>checked<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_pushButtonColor_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_pushButtonBGColor_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_lineEditFontSize_editingFinished</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_textEdit_currentCharFormatChanged</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">const</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QTextCharFormat</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">&</span>format<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_textEdit_textChanged</span><span
style=" color:#000000;">();</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">private</span><span style=" color:#000000;">:</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">Ui</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">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:#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>
<QTextCharFormat> 是实时感知到的文本片段格式,on_textEdit_currentCharFormatChanged
槽函数用到它,因此需要提前包含该头文件。<br>
widget.h 其他代码都是自动生成的,不需要更改。<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;"><QIntValidator></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:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;"><QFont></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:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;"><QColorDialog></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:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;"><QBrush></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:#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="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//设置二态按钮</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">ui</span><span
style=" color:#000000;">-></span><span style=" color:#800000;">pushButtonBold</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setCheckable</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">true</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:#800000;">ui</span><span
style=" color:#000000;">-></span><span style=" color:#800000;">pushButtonItalic</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setCheckable</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">true</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:#800000;">ui</span><span
style=" color:#000000;">-></span><span style=" color:#800000;">pushButtonUnderline</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setCheckable</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">true</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:#008000;">//字体点阵大小,这里设置下限</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">0,上限</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">72</span></pre>
<pre style=" margin-top:0px; 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;">//0</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;">QIntValidator</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">*</span><span style=" color:#000000;">vali</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#808000;">new</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QIntValidator</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;">72</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;">lineEditFontSize</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setValidator</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">vali</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;">9</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">pt</span></pre>
<pre style=" margin-top:0px; 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;">lineEditFontSize</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:#800080;">QString</span><span
style=" color:#000000;">(</span><span style=" color:#008000;">""</span><span style=" color:#000000;">).</span><span
style=" color:#000000;">setNum</span><span style=" color:#000000;">(</span><span
style=" color:#000080;">9</span><span style=" color:#000000;">)</span><span style=" color:#c0c0c0;"> </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:#000000;">connect</span><span
style=" color:#000000;">(</span><span style=" color:#800000;">ui</span><span style=" color:#000000;">-></span><span
style=" color:#800000;">fontComboBox</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">SIGNAL</span><span
style=" color:#000000;">(</span>currentIndexChanged<span style=" color:#000000;">(</span><span
style=" color:#800080;">QString</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:#c0c0c0;"> </span><span style=" color:#808000;">SLOT</span><span
style=" color:#000000;">(</span>setFontFamily<span style=" color:#000000;">(</span><span
style=" color:#800080;">QString</span><span style=" color:#000000;">)));</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//丰富文本编辑框初始内容</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">ui</span><span
style=" color:#000000;">-></span><span style=" color:#800000;">textEdit</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setHtml</span><span
style=" color:#000000;">(</span><span style=" color:#008000;">"<b>粗体字的行<br></b>"</span></pre>
<pre style=" margin-top:0px; 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;">"<i>斜体字的行<br></i>"</span></pre>
<pre style=" margin-top:0px; 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;">"<u>下划线的行<br></u>"</span></pre>
<pre style=" margin-top:0px; 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;">"<font</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">style=\"color:red;\">文本前景色<br></font>"</span></pre>
<pre style=" margin-top:0px; 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;">"<font</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">style=\"background:yellow;\">文字背景色<br></font>"</span></pre>
<pre style=" margin-top:0px; 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;">"<font</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">style=\"font-size:18pt;\">字号大小变化的行<br></font>"</span></pre>
<pre style=" margin-top:0px; 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;">"<font</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">style=\"font-family:黑体;\">字体家族变化的行<br></font>"</span></pre>
<pre style=" margin-top:0px; 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;">//html</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">字号有</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">pt(PointSize)</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">和</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">px(PixelSize)</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">两种形式,例子代码适用于</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">pt</span></pre>
<pre style=" margin-top:0px; 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;">1</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">英寸</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">==</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">72pt(点)</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">==</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">96px</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">(像素),网页中最常用到的:9pt</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">==</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">12px</span></pre>
<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>
<QIntValidator> 包含整数验证器类,用于限定字号的范围。<br>
<QFont> 包含综合字体格式类,用于感知或设置文本格式。<br>
<QColorDialog> 包含颜色设置对话框,提供给用户选取文字前景色或者背景色。<br>
<QBrush> 包含画刷类,在进行颜色感知时,需要通过前景色画刷和背景色画刷来感知。<br>
<br>
在构造函数里,首先是 3 个二态按钮的设置,通过调用 setCheckable(true) ,将普通按压按钮变成我们需要的二态按钮。<br>
<br>
接下来是字号设置单行编辑控件的代码,新建的整数验证器 vali ,限定字号从 0 到 72 。这里的 0
号大小的字,是指不知道字体大小的情况,不是没有字。然后将单行编辑控件初始显示的数字字符设置为 9。<br>
<br>
接下来是关于字体枚举组合框 ui->fontComboBox 的信号关联,字体枚举组合框的
currentIndexChanged(QString) 信号会在字体家族变化时发出,参数就是字体家族的名字字符串。这个信号直接由丰富文本编辑控件的
setFontFamily(QString) 槽函数接收。字体组合框的用途就是枚举系统里的字体供用户挑选,选中的文本变化就发送
currentIndexChanged(QString) 信号。字体组合框基类是普通组合框
QComboBox,这个下一节还会再讲普通组合框。本节学会构造函数里的 connect 就够了。<br>
<br>
构造函数最后部分是通过丰富文本编辑控件的 setHtml() 设置初始显示的丰富文本。这段 HTML 文本总共 7 行:<br>
第 1 行粗体字,第 2 行是斜体字,第 3 行是文字下划线,第 4 行是红色文字,<br>
第 5 行是黄色背景的文字,第 6 行是 18 号点阵大小的文字,第 7 行是黑体的文字。<br>
HTML 字符串里的反斜杠 \ 是 C++ 字符串里的转义字符,用于输入其后紧随的双引号 " ,<br>
例子中实际的 HTML 代码没有反斜杠,只有双引号。<br>
<br>
这里解释一下关于 HTML 字号大小的两种形式,一种是以 pt 为单位(点,PointSize),另一种以 px 为单位(像素,PixelSize)。<br>
pt 和 px 意义不同,pt 点阵是屏幕尺寸划分的度量单元,而像素是屏幕的物理发光单元,一般情况下对应公式为:<br>
<div class="code">1 英寸 == 72pt(点) == 96px (像素)</div>
按照这个公式,9 pt 对应 12 px。<br>
1 英寸等于 72 pt 这个公式是恒成立的,而屏幕像素的对应关系是可以变的。<br>
1 英寸 96 像素是一般的桌面操作系统里对应关系,这个是可以修改的,通过网页搜索“DPI设置” 就可以查到修改方法。<br>
例子里面的字号大小都是指 pt。<br>
<br>
构造函数代码介绍完了,下面来看看 3 个二态按钮的槽函数代码:<br>
<div class="code"><span style=" color:#008000;">//粗体</span>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">void</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">Widget</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">on_pushButtonBold_clicked</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">bool</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">checked</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;">if</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">checked</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;">setFontWeight</span><span
style=" color:#000000;">(</span><span style=" color:#800080;">QFont</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">Bold</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:#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></pre>
<pre style=" margin-top:0px; 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;">setFontWeight</span><span
style=" color:#000000;">(</span><span style=" color:#800080;">QFont</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">Normal</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:#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:#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:#808000;">void</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">Widget</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">on_pushButtonItalic_clicked</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">bool</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">checked</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:#800000;">textEdit</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setFontItalic</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">checked</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:#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:#808000;">void</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">Widget</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">on_pushButtonUnderline_clicked</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">bool</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">checked</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:#800000;">textEdit</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setFontUnderline</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">checked</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>
QTextEdit 类的快捷函数里面,没有直接根据 bool参数设置 Bold 粗体的函数,而是通过 int 参数的 setFontWeight()
函数。<br>
所以需要根据 checked 参数判断应该是设置粗体 QFont::Bold 还是普通字体 QFont::Normal。<br>
关于字体笔画权重 FontWeight,还有其他枚举常量,只是不常用,一般只用到例子里的两个枚举常量。<br>
斜体设置和下划线设置的槽函数就很简单了,直接调用丰富文本编辑控件的槽函数即可。<br>
这些设置函数会自动对丰富文本编辑控件里选中的文本生效,而不要通过 QTextEdit 内部的文档或光标来操作,<br>
那是因为我们的例子比较简单,只用到了 QTextEdit 封装好的快捷函数。<br>
<br>
接下来编写设置文字前景色和背景色的槽函数:<br>
<div class="code"><span style=" color:#008000;">//文字前景色设置</span>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">void</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">Widget</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">on_pushButtonColor_clicked</span><span
style=" color:#000000;">()</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QColor</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">clr</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QColorDialog</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">getColor</span><span
style=" color:#000000;">(</span><span style=" color:#800080;">Qt</span><span style=" color:#000000;">::</span><span
style=" color:#800080;">black</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:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">clr</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">isValid</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:#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;">setTextColor</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">clr</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;">strSS</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;">"color:</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;">clr</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">name</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;">pushButtonColor</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setStyleSheet</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">strSS</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>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><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:#808000;">void</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">Widget</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">on_pushButtonBGColor_clicked</span><span
style=" color:#000000;">()</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QColor</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">bgclr</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">QColorDialog</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">getColor</span><span style=" color:#000000;">(</span><span
style=" color:#800080;">Qt</span><span style=" color:#000000;">::</span><span style=" color:#800080;">white</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:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">bgclr</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">isValid</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:#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;">setTextBackgroundColor</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">bgclr</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;">strSSBG</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;">"background:</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;">bgclr</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">name</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;">pushButtonBGColor</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setStyleSheet</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">strSSBG</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>
这两个函数代码结构是一样的,只是设置前景色和背景色的函数、以及按钮的 Qt Style Sheet 代码稍微有区别。我们以设置文字前景色的函数为例讲解。<br>
QColorDialog 是专门用于获取颜色的对话框,可以通过定义对话框实例的方式使用,或者用静态函数:<br>
<div class="code">QColor getColor(const QColor & initial = Qt::white,
QWidget * parent = 0, const QString & title = QString(),
ColorDialogOptions options = 0)</div>
这个静态函数自动弹出颜色对话框,提供给用户取色,然后返回一个 QColor
对象。如果用户选取了颜色(用户点击确定按钮),那么返回对象是可用的,否则返回的颜色对象处于不可用状态(用户点击取消按钮)。可以用颜色对象的函数
isValid() 获知返回值的状态。<br>
QColorDialog 的静态函数 getColor 有多个参数,第一个 initial 是颜色对话框里取色的初始值,第二个是父窗口指针,<br>
第三格式颜色对话框的标题字符串,第四个是颜色对话框的选项。一般可以不填参数或者只填初始颜色值,其他的可以不管。<br>
<br>
例子中 on_pushButtonColor_clicked() 函数获取颜色 clr 之后,先通过 clr.isValid()
函数判断,如果用户选择了可用的颜色,那就执行相关代码,如果没有可用颜色,那就啥都不干。<br>
<br>
对于有可用颜色的情况,先设置 QTextEdit 对象的文字颜色,通过 setTextColor() 函数即可。<br>
另外,对界面的 "前景色" 按钮做了颜色同步,就是设置该按钮的样式表字符串 setStyleSheet() 。<br>
QColor 类有获取颜色名的函数 name() ,可以很方便地取得颜色的 HTML/CSS 名称,比如 "#00FF00",这个颜色名称对 Qt
Style Sheet 也是一样的。因此可以很方便地构造前景色的 QSS 字符串 tr("color: %1").arg(clr.name()) 。这里
tr() 函数不是拿来做翻译,就是封装为 QString 而已。<br>
<br>
对于设置背景色的槽函数,代码结构是类似的,只不过通过 setTextBackgroundColor() 函数设置丰富文本的背景色,<br>
而按钮的背景色 QSS 是 tr("background: %1").arg(bgclr.name()) 。<br>
<br>
接下来是关于字号大小设置的槽函数:<br>
<div class="code"><span style=" color:#008000;">//字号修改好了,设置选中文本的字号</span>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">void</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">Widget</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">on_lineEditFontSize_editingFinished</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;">int</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nFontSize</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;">lineEditFontSize</span><span style=" color:#000000;">-></span><span
style=" color:#000000;">text</span><span style=" color:#000000;">().</span><span
style=" color:#000000;">toInt</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;">setFontPointSize</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nFontSize</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>
我们将编辑框里的字符串转换为 int 数值,然后设置丰富文本编辑控件的点阵大小即可。因为关联的 editingFinished()
信号,在图形界面修改字号时,按一下回车键或者输入焦点离开单行编辑控件时才会触发编辑完成的信号。<br>
<br>
对于枚举字体家族的组合框,我们没有额外添加对应的槽函数,而是在构造函数里直接调用了 connect 函数,省了许多事。<br>
<br>
以上的函数都是对丰富文本编辑控件里选中内容进行格式编辑修改的槽函数,并没有实时感知功能。下面来看看丰富文本编辑控件的文本字符格式感知信号对应的槽函数:<br>
<div class="code"><span style=" color:#008000;">//根据光标位置或选中文本感知字体格式</span>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">void</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">Widget</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">on_textEdit_currentCharFormatChanged</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">const</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QTextCharFormat</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">&</span><span
style=" color:#000000;">format</span><span style=" color:#000000;">)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//粗体检测</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">format</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">fontWeight</span><span
style=" color:#000000;">()</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">==</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QFont</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">Bold</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;">pushButtonBold</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setChecked</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:#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></pre>
<pre style=" margin-top:0px; 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;">pushButtonBold</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setChecked</span><span
style=" color:#000000;">(</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:#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;">pushButtonItalic</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setChecked</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">format</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">fontItalic</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;">pushButtonUnderline</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setChecked</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">format</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">fontUnderline</span><span
style=" color:#000000;">()</span><span style=" color:#c0c0c0;"> </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;">QBrush</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">brushText</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">format</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">foreground</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;">brushText</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">!=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">Qt</span><span style=" color:#000000;">::</span><span style=" color:#800080;">NoBrush</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">)</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:#800080;">QColor</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">clrText</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">brushText</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">color</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;">strSS</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;">"color:</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:#c0c0c0;"> </span><span
style=" color:#000000;">clrText</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">name</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;">pushButtonColor</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setStyleSheet</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strSS</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:#808000;">else</span><span
style=" color:#008000;">//没有前景色画刷</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">Qt::NoBrush</span></pre>
<pre style=" margin-top:0px; 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;">pushButtonColor</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setStyleSheet</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:#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;">QBrush</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">brushBG</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">format</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">background</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;">brushBG</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">!=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">Qt</span><span style=" color:#000000;">::</span><span style=" color:#800080;">NoBrush</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">)</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:#800080;">QColor</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">clrBG</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">brushBG</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">color</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;">strSSBG</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;">"background:</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;">clrBG</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">name</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;">pushButtonBGColor</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setStyleSheet</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">strSSBG</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><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">Qt::NoBrush</span></pre>
<pre style=" margin-top:0px; 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;">pushButtonBGColor</span><span
style=" color:#000000;">-></span><span style=" color:#000000;">setStyleSheet</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="-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><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">QFont</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">的函数,不要用</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">QTextCharFormat</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;">//QTextCharFormat</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=" margin-top:0px; 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;">QFont</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">curFont</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">format</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">font</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;">QFont</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:#808000;">int</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nFontSize</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">curFont</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">pointSize</span><span style=" color:#000000;">();</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;">px</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">形式,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">QFont::pointSize()</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">函数返回</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">-1</span></pre>
<pre style=" margin-top:0px; 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:#000080;">1</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">==</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nFontSize</span><span
style=" color:#c0c0c0;"> </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;">pt</span><span
style=" color:#c0c0c0;"> </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;">px</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:#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;">px</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">,换算成</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">pt</span></pre>
<pre style=" margin-top:0px; 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;">nFontSize</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">(</span><span style=" color:#808000;">int</span><span style=" color:#000000;">)(</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">curFont</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">pixelSize</span><span
style=" color:#000000;">()</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">*</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000080;">9.0</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">/</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">12.0</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;">lineEditFontSize</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:#800080;">QString</span><span
style=" color:#000000;">(</span><span style=" color:#008000;">""</span><span style=" color:#000000;">).</span><span