- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章python3+PyQt5 实现Rich文本的行编辑方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本文通过python3+pyqt5实现《python qt gui 快速编程》这本书13章程序rich文本的行编辑,可以通过鼠标右键选择对文本进行加粗,斜体,下划线,删除线,上标,下标等编辑.
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
|
#!/usr/bin/env python3
import
platform
import
sys
import
html
from
pyqt5.qtcore
import
qsize, qt,pyqtsignal
from
pyqt5.qtgui
import
qcolor, qfont,qfontmetrics, qicon, qkeysequence, qpixmap,qtextcharformat
from
pyqt5.qtwidgets
import
qaction,qapplication,qmenu,qtextedit
class
richtextlineedit(qtextedit):
returnpressed
=
pyqtsignal()
(bold, italic, underline, strikeout, monospaced, sans, serif,
nosuperorsubscript, subscript, superscript)
=
range
(
10
)
def
__init__(
self
, parent
=
none):
super
(richtextlineedit,
self
).__init__(parent)
self
.monofamily
=
"courier"
self
.sansfamily
=
"helvetica"
self
.seriffamily
=
"times"
self
.setlinewrapmode(qtextedit.nowrap)
self
.settabchangesfocus(true)
self
.setverticalscrollbarpolicy(qt.scrollbaralwaysoff)
self
.sethorizontalscrollbarpolicy(qt.scrollbaralwaysoff)
fm
=
qfontmetrics(
self
.font())
h
=
int
(fm.height()
*
(
1.4
if
platform.system()
=
=
"windows"
else
1.2
))
self
.setminimumheight(h)
self
.setmaximumheight(
int
(h
*
1.2
))
self
.settooltip(
"press <b>ctrl+m</b> for the text effects "
"menu and <b>ctrl+k</b> for the color menu"
)
def
toggleitalic(
self
):
self
.setfontitalic(
not
self
.fontitalic())
def
toggleunderline(
self
):
self
.setfontunderline(
not
self
.fontunderline())
def
togglebold(
self
):
self
.setfontweight(qfont.normal
if
self
.fontweight() > qfont.normal
else
qfont.bold)
def
sizehint(
self
):
return
qsize(
self
.document().idealwidth()
+
5
,
self
.maximumheight())
def
minimumsizehint(
self
):
fm
=
qfontmetrics(
self
.font())
return
qsize(fm.width(
"wwww"
),
self
.minimumheight())
def
contextmenuevent(
self
, event):
self
.texteffectmenu()
def
keypressevent(
self
, event):
if
event.modifiers() & qt.controlmodifier:
handled
=
false
if
event.key()
=
=
qt.key_b:
self
.togglebold()
handled
=
true
elif
event.key()
=
=
qt.key_i:
self
.toggleitalic()
handled
=
true
elif
event.key()
=
=
qt.key_k:
self
.colormenu()
handled
=
true
elif
event.key()
=
=
qt.key_m:
self
.texteffectmenu()
handled
=
true
elif
event.key()
=
=
qt.key_u:
self
.toggleunderline()
handled
=
true
if
handled:
event.accept()
return
if
event.key()
in
(qt.key_enter, qt.key_return):
self
.returnpressed.emit()
event.accept()
else
:
qtextedit.keypressevent(
self
, event)
def
colormenu(
self
):
pixmap
=
qpixmap(
22
,
22
)
menu
=
qmenu(
"colour"
)
for
text, color
in
(
(
"&black"
, qt.black),
(
"b&lue"
, qt.blue),
(
"dark bl&ue"
, qt.darkblue),
(
"&cyan"
, qt.cyan),
(
"dar&k cyan"
, qt.darkcyan),
(
"&green"
, qt.green),
(
"dark gr&een"
, qt.darkgreen),
(
"m&agenta"
, qt.magenta),
(
"dark mage&nta"
, qt.darkmagenta),
(
"&red"
, qt.red),
(
"&dark red"
, qt.darkred)):
color
=
qcolor(color)
pixmap.fill(color)
action
=
menu.addaction(qicon(pixmap), text,
self
.setcolor)
action.setdata(color)
self
.ensurecursorvisible()
menu.exec_(
self
.viewport().maptoglobal(
self
.cursorrect().center()))
def
setcolor(
self
):
action
=
self
.sender()
if
action
is
not
none
and
isinstance
(action, qaction):
color
=
qcolor(action.data())
if
color.isvalid():
self
.settextcolor(color)
def
texteffectmenu(
self
):
format
=
self
.currentcharformat()
menu
=
qmenu(
"text effect"
)
for
text, shortcut, data, checked
in
(
(
"&bold"
,
"ctrl+b"
, richtextlineedit.bold,
self
.fontweight() > qfont.normal),
(
"&italic"
,
"ctrl+i"
, richtextlineedit.italic,
self
.fontitalic()),
(
"strike &out"
, none, richtextlineedit.strikeout,
format
.fontstrikeout()),
(
"&underline"
,
"ctrl+u"
, richtextlineedit.underline,
self
.fontunderline()),
(
"&monospaced"
, none, richtextlineedit.monospaced,
format
.fontfamily()
=
=
self
.monofamily),
(
"&serifed"
, none, richtextlineedit.serif,
format
.fontfamily()
=
=
self
.seriffamily),
(
"s&ans serif"
, none, richtextlineedit.sans,
format
.fontfamily()
=
=
self
.sansfamily),
(
"&no super or subscript"
, none,
richtextlineedit.nosuperorsubscript,
format
.verticalalignment()
=
=
qtextcharformat.alignnormal),
(
"su&perscript"
, none, richtextlineedit.superscript,
format
.verticalalignment()
=
=
qtextcharformat.alignsuperscript),
(
"subs&cript"
, none, richtextlineedit.subscript,
format
.verticalalignment()
=
=
qtextcharformat.alignsubscript)):
action
=
menu.addaction(text,
self
.settexteffect)
if
shortcut
is
not
none:
action.setshortcut(qkeysequence(shortcut))
action.setdata(data)
action.setcheckable(true)
action.setchecked(checked)
self
.ensurecursorvisible()
menu.exec_(
self
.viewport().maptoglobal(
self
.cursorrect().center()))
def
settexteffect(
self
):
action
=
self
.sender()
if
action
is
not
none
and
isinstance
(action, qaction):
what
=
action.data()
if
what
=
=
richtextlineedit.bold:
self
.togglebold()
return
if
what
=
=
richtextlineedit.italic:
self
.toggleitalic()
return
if
what
=
=
richtextlineedit.underline:
self
.toggleunderline()
return
format
=
self
.currentcharformat()
if
what
=
=
richtextlineedit.monospaced:
format
.setfontfamily(
self
.monofamily)
elif
what
=
=
richtextlineedit.serif:
format
.setfontfamily(
self
.seriffamily)
elif
what
=
=
richtextlineedit.sans:
format
.setfontfamily(
self
.sansfamily)
if
what
=
=
richtextlineedit.strikeout:
format
.setfontstrikeout(
not
format
.fontstrikeout())
if
what
=
=
richtextlineedit.nosuperorsubscript:
format
.setverticalalignment(
qtextcharformat.alignnormal)
elif
what
=
=
richtextlineedit.superscript:
format
.setverticalalignment(
qtextcharformat.alignsuperscript)
elif
what
=
=
richtextlineedit.subscript:
format
.setverticalalignment(
qtextcharformat.alignsubscript)
self
.mergecurrentcharformat(
format
)
def
tosimplehtml(
self
):
htmltext
=
""
black
=
qcolor(qt.black)
block
=
self
.document().begin()
while
block.isvalid():
iterator
=
block.begin()
while
iterator !
=
block.end():
fragment
=
iterator.fragment()
if
fragment.isvalid():
format
=
fragment.charformat()
family
=
format
.fontfamily()
color
=
format
.foreground().color()
text
=
html.escape(fragment.text())
if
(
format
.verticalalignment()
=
=
qtextcharformat.alignsubscript):
text
=
"<sub>{0}</sub>"
.
format
(text)
elif
(
format
.verticalalignment()
=
=
qtextcharformat.alignsuperscript):
text
=
"<sup>{0}</sup>"
.
format
(text)
if
format
.fontunderline():
text
=
"<u>{0}</u>"
.
format
(text)
if
format
.fontitalic():
text
=
"<i>{0}</i>"
.
format
(text)
if
format
.fontweight() > qfont.normal:
text
=
"<b>{0}</b>"
.
format
(text)
if
format
.fontstrikeout():
text
=
"<s>{0}</s>"
.
format
(text)
if
color !
=
black
or
family:
attribs
=
""
if
color !
=
black:
attribs
+
=
' color="{0}"'
.
format
(color.name())
if
family:
attribs
+
=
' face="{0}"'
.
format
(family)
text
=
"<font{0}>{1}</font>"
.
format
(attribs,text)
htmltext
+
=
text
iterator
+
=
1
block
=
block.
next
()
return
htmltext
if
__name__
=
=
"__main__"
:
def
printout(lineedit):
print
(
str
(lineedit.tohtml()))
print
(
str
(lineedit.toplaintext()))
print
(
str
(lineedit.tosimplehtml()))
app
=
qapplication(sys.argv)
lineedit
=
richtextlineedit()
lineedit.returnpressed.connect(
lambda
:printout(lineedit))
lineedit.show()
lineedit.setwindowtitle(
"richtextedit"
)
app.exec_()
|
以上这篇python3+pyqt5 实现rich文本的行编辑方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我.
原文链接:https://blog.csdn.net/xiaoyangyang20/article/details/68924261 。
最后此篇关于python3+PyQt5 实现Rich文本的行编辑方法的文章就讲到这里了,如果你想了解更多关于python3+PyQt5 实现Rich文本的行编辑方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我一直在网上搜索,但没有找到明确的解释。我试图找出 rich:autocomplete 和 rich:suggestionbox 功能之间的区别。 有人能帮忙吗? 谢谢。 最佳答案 是一个 richf
谁能告诉我 rich:column 和 rich:columns 之间的区别? 最佳答案 当您想动态创建列时,您正在使用rich:columns 例如:
我使用 Richfaces 并有一个带有嵌套 rich:tooltip-s 的 rich:datatable。您可以想象生成的 HTML 看起来像这样: Table 1.1: A record
我使用 Richfaces、Seam 和 JSF,我想要如下内容: 我已经使用像这样的 rich:subtable 在一定程度上管理了它: Company Name
我们有以下丰富的:ComponentControl: #{item.hasDocuments} 的值已成功从 bean 传递为 true 或 false。 这是上下文菜单,它应该有条件地显示该项
我正在尝试使用谷歌代码片段测试工具测试以下代码片段,但我得到的只是“此页面不包含作者身份或丰富的代码片段标记”并且它变得非常烦人任何想法可能是什么问题?? Linas Located at Plaz
我对 rich 库有疑问。安装好后,我在代码中写到: from rich import print d = { "employee1": "telephone: 123-456-789 -",
我们有一个带图片的推送通知,我们面临的问题是在某些设备上没有显示图片。 具体如下:在运行 iOS 11.4.1 的 iPhone 8 中,只有推送的文本显示,没有图像。在 12.1 上运行的 iPho
我们正在使用Elasticsearch 6.8.4和Flink 1.0.18。 我们在Elasticsearch中有一个包含1个分片和1个副本的索引,我想创建自定义输入格式,以使用具有超过1个输入分割
Closed. This question needs to be more focused。它当前不接受答案。 想要改善这个问题吗?更新问题,使它仅关注editing this post的一个问题。
我想在grails rich ui插件的dateChooser功能中设置默认时间。 richui:dateChooser name =“fromdate” value =“$ {old?.fromda
我正在使用 JSF2.0、Spring-Webflow2.3.1 和 richfaces4.2.3。我有一个 rich:extendedDataTable 它将显示数据列表(里面没有按钮)。我需要实现
我正在研究如何将 silverlight 合并到我现有的应用程序中,并且不断遇到最新的流行语“富人岛”。什么意思? 谢谢! 最佳答案 安island of richness是页面上的(相对)小区域,包
我正在尝试在 android 中实现“Rich Edittext”,我想应用“粗体、斜体、下划线、项目符号跨度、数字缩进跨度等...”效果。因此,不知何故我已经完成了粗体、斜体和其他功能,但我无法完成
我正在使用 JSF 1.2,在我正在使用的网页之一中。我想设置列的宽度。
最近我了解到 http://schema.org我将它实现到我的网站。 我有几个问题: 为什么我在丰富网页摘要测试工具中看到价格标签,但在 Google 结果中却看不到。看看结果:http://www
我正在构建一个包含动态列数的丰富数据表。在我看来,这不是什么大事,但我想从几个小时开始就得到答案。问题是当我想将数据表中的迭代变量用于嵌套循环时。在嵌套循环中,我尝试为每一行创建相同的动态列数。当我显
是否可以在 subTable 组件或列中使用 forEach 循环,在 Richfaces 中3.3?我需要呈现动态列数,但没有成功。 例子:
为什么 rich: comboBox 使用值显示在列表中而不是标签中,就像我显示描述和选择 id 一样。 我这样做了 最佳答案 rich:combobox 实现了它的设计目的(带有
我有 rich:dataTable 有多个可排序列。 ... ... etc. 排序工作正常。但是,加载页面时,表格始终按第一列排序。如何设置“默认”排序列? (例如,带有 user.sn 的那个
我是一名优秀的程序员,十分优秀!