- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章python3+PyQt5泛型委托详解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
自定义委托可以让我们对视图中出现的数据项的外观和行为进行完全控制。如果有很多模型,可能会希望不是全部的大多数模型能够仅用一个自定义委托,如果不能这么做,那么对于这些自定义委托,将很有可能存在大量重复代码。为了使得维护工作变得轻松,更好的方法为不要为每个模型创建一个自定义委托,而是用一系列的通用组件来共同构成一个委托。本文通过Python3+pyqt5实现了python Qt GUI 快速编程的16章的泛型委托例子.
/home/yrd/eric_workspace/chap16/richtextlineedit.py 。
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
|
#!/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_()
|
/home/yrd/eric_workspace/chap16/genericdelegates.py 。
。
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
|
#!/usr/bin/env python3
from
PyQt5.QtCore
import
(QDate, QSize, Qt)
from
PyQt5.QtWidgets
import
(QApplication, QDateEdit, QLineEdit,
QSpinBox, QStyledItemDelegate,QStyle)
from
PyQt5.QtGui
import
QColor,QTextDocument
import
richtextlineedit
class
GenericDelegate(QStyledItemDelegate):
def
__init__(
self
, parent
=
None
):
super
(GenericDelegate,
self
).__init__(parent)
self
.delegates
=
{}
def
insertColumnDelegate(
self
, column, delegate):
delegate.setParent(
self
)
self
.delegates[column]
=
delegate
def
removeColumnDelegate(
self
, column):
if
column
in
self
.delegates:
del
self
.delegates[column]
def
paint(
self
, painter, option, index):
delegate
=
self
.delegates.get(index.column())
if
delegate
is
not
None
:
delegate.paint(painter, option, index)
else
:
QStyledItemDelegate.paint(
self
, painter, option, index)
def
createEditor(
self
, parent, option, index):
delegate
=
self
.delegates.get(index.column())
if
delegate
is
not
None
:
return
delegate.createEditor(parent, option, index)
else
:
return
QStyledItemDelegate.createEditor(
self
, parent, option,
index)
def
setEditorData(
self
, editor, index):
delegate
=
self
.delegates.get(index.column())
if
delegate
is
not
None
:
delegate.setEditorData(editor, index)
else
:
QStyledItemDelegate.setEditorData(
self
, editor, index)
def
setModelData(
self
, editor, model, index):
delegate
=
self
.delegates.get(index.column())
if
delegate
is
not
None
:
delegate.setModelData(editor, model, index)
else
:
QStyledItemDelegate.setModelData(
self
, editor, model, index)
class
IntegerColumnDelegate(QStyledItemDelegate):
def
__init__(
self
, minimum
=
0
, maximum
=
100
, parent
=
None
):
super
(IntegerColumnDelegate,
self
).__init__(parent)
self
.minimum
=
minimum
self
.maximum
=
maximum
def
createEditor(
self
, parent, option, index):
spinbox
=
QSpinBox(parent)
spinbox.setRange(
self
.minimum,
self
.maximum)
spinbox.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
return
spinbox
def
setEditorData(
self
, editor, index):
value
=
index.model().data(index, Qt.DisplayRole)
editor.setValue(value)
def
setModelData(
self
, editor, model, index):
editor.interpretText()
model.setData(index, editor.value())
class
DateColumnDelegate(QStyledItemDelegate):
def
__init__(
self
, minimum
=
QDate(),
maximum
=
QDate.currentDate(),
format
=
"yyyy-MM-dd"
, parent
=
None
):
super
(DateColumnDelegate,
self
).__init__(parent)
self
.minimum
=
minimum
self
.maximum
=
maximum
self
.
format
=
format
def
createEditor(
self
, parent, option, index):
dateedit
=
QDateEdit(parent)
#dateedit=QDateTimeEdit(parent)
dateedit.setDateRange(
self
.minimum,
self
.maximum)
dateedit.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
dateedit.setDisplayFormat(
self
.
format
)
dateedit.setCalendarPopup(
True
)
return
dateedit
def
setEditorData(
self
, editor, index):
value
=
index.model().data(index, Qt.DisplayRole)
#if value.isNull:
editor.setDate(value)
#editor.setDisplayFormat(self.format)
def
setModelData(
self
, editor, model, index):
model.setData(index, editor.date())
def
paint(
self
, painter, option, index):
text
=
index.model().data(index, Qt.DisplayRole).toString(
self
.
format
)
palette
=
QApplication.palette()
document
=
QTextDocument()
document.setDefaultFont(option.font)
if
option.state & QStyle.State_Selected:
document.setHtml(
"<font color={0}>{1}</font>"
.
format
(palette.highlightedText().color().name(),text))
else
:
document.setHtml(text)
painter.save()
color
=
(palette.highlight().color()
if
option.state & QStyle.State_Selected
else
QColor(index.model().data(index,
Qt.BackgroundColorRole)))
painter.fillRect(option.rect, color)
painter.translate(option.rect.x(), option.rect.y())
document.drawContents(painter)
painter.restore()
class
PlainTextColumnDelegate(QStyledItemDelegate):
def
__init__(
self
, parent
=
None
):
super
(PlainTextColumnDelegate,
self
).__init__(parent)
def
createEditor(
self
, parent, option, index):
lineedit
=
QLineEdit(parent)
return
lineedit
def
setEditorData(
self
, editor, index):
value
=
index.model().data(index, Qt.DisplayRole)
editor.setText(value)
def
setModelData(
self
, editor, model, index):
model.setData(index, editor.text())
class
RichTextColumnDelegate(QStyledItemDelegate):
def
__init__(
self
, parent
=
None
):
super
(RichTextColumnDelegate,
self
).__init__(parent)
def
paint(
self
, painter, option, index):
text
=
index.model().data(index, Qt.DisplayRole)
palette
=
QApplication.palette()
document
=
QTextDocument()
document.setDefaultFont(option.font)
if
option.state & QStyle.State_Selected:
document.setHtml(
"<font color={0}>{1}</font>"
.
format
(palette.highlightedText().color().name(),text))
else
:
document.setHtml(text)
painter.save()
color
=
(palette.highlight().color()
if
option.state & QStyle.State_Selected
else
QColor(index.model().data(index,
Qt.BackgroundColorRole)))
painter.fillRect(option.rect, color)
painter.translate(option.rect.x(), option.rect.y())
document.drawContents(painter)
painter.restore()
def
sizeHint(
self
, option, index):
text
=
index.model().data(index).toString()
document
=
QTextDocument()
document.setDefaultFont(option.font)
document.setHtml(text)
return
QSize(document.idealWidth()
+
5
,
option.fontMetrics.height())
def
createEditor(
self
, parent, option, index):
lineedit
=
richtextlineedit.RichTextLineEdit(parent)
return
lineedit
def
setEditorData(
self
, editor, index):
value
=
index.model().data(index, Qt.DisplayRole)
editor.setHtml(value)
def
setModelData(
self
, editor, model, index):
model.setData(index, editor.toSimpleHtml())
|
/home/yrd/eric_workspace/chap16/carhirelog.pyw 。
。
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
|
#!/usr/bin/env python3
import
bisect
import
os
import
platform
import
sys
from
PyQt5.QtCore
import
(QAbstractTableModel, QDate, QModelIndex,
QVariant, Qt,pyqtSignal)
from
PyQt5.QtWidgets
import
(QApplication, QMainWindow,
QShortcut, QTableView)
from
PyQt5.QtGui
import
QKeySequence
import
genericdelegates
(LICENSE, CUSTOMER, HIRED, MILEAGEOUT, RETURNED, MILEAGEBACK,
NOTES, MILEAGE, DAYS)
=
range
(
9
)
class
CarHireLog(
object
):
def
__init__(
self
, license, customer, hired, mileageout,
returned
=
QDate(), mileageback
=
0
, notes
=
""):
self
.license
=
license
# plain text
self
.customer
=
customer
# plain text
self
.hired
=
hired
# QDate
self
.mileageout
=
mileageout
# int
self
.returned
=
returned
# QDate
self
.mileageback
=
mileageback
# int
self
.notes
=
notes
# HTML
def
field(
self
, column):
if
column
=
=
LICENSE:
return
self
.license
elif
column
=
=
CUSTOMER:
return
self
.customer
elif
column
=
=
HIRED:
return
self
.hired
elif
column
=
=
MILEAGEOUT:
return
self
.mileageout
elif
column
=
=
RETURNED:
return
self
.returned
elif
column
=
=
MILEAGEBACK:
return
self
.mileageback
elif
column
=
=
NOTES:
return
self
.notes
elif
column
=
=
MILEAGE:
return
self
.mileage()
elif
column
=
=
DAYS:
return
self
.days()
assert
False
def
mileage(
self
):
return
(
0
if
self
.mileageback
=
=
0
else
self
.mileageback
-
self
.mileageout)
def
days(
self
):
return
(
0
if
not
self
.returned.isValid()
else
self
.hired.daysTo(
self
.returned))
def
__hash__(
self
):
return
super
(CarHireLog,
self
).__hash__()
def
__eq__(
self
, other):
if
self
.hired !
=
other.hired:
return
False
if
self
.customer !
=
other.customer:
return
False
if
self
.license !
=
other.license:
return
False
return
id
(
self
)
=
=
id
(other)
def
__lt__(
self
, other):
if
self
.hired < other.hired:
return
True
if
self
.customer < other.customer:
return
True
if
self
.license < other.license:
return
True
return
id
(
self
) <
id
(other)
class
CarHireModel(QAbstractTableModel):
dataChanged
=
pyqtSignal(QModelIndex,QModelIndex)
def
__init__(
self
, parent
=
None
):
super
(CarHireModel,
self
).__init__(parent)
self
.logs
=
[]
# Generate fake data
import
gzip
import
random
import
string
surname_data
=
gzip.
open
(os.path.join(
os.path.dirname(__file__),
"surnames.txt.gz"
)).read()
surnames
=
surname_data.decode(
"utf8"
).splitlines()
years
=
(
"06 "
,
"56 "
,
"07 "
,
"57 "
,
"08 "
,
"58 "
)
titles
=
(
"Ms "
,
"Mr "
,
"Ms "
,
"Mr "
,
"Ms "
,
"Mr "
,
"Dr "
)
notetexts
=
(
"Returned <font color=red><b>damaged</b></font>"
,
"Returned with <i>empty fuel tank</i>"
,
"Customer <b>complained</b> about the <u>engine</u>"
,
"Customer <b>complained</b> about the <u>gears</u>"
,
"Customer <b>complained</b> about the <u>clutch</u>"
,
"Returned <font color=darkred><b>dirty</b></font>"
,)
today
=
QDate.currentDate()
for
i
in
range
(
250
):
license
=
[]
for
c
in
range
(
5
):
license.append(random.choice(string.ascii_uppercase))
license
=
("".join(license[:
2
])
+
random.choice(years)
+
"".join(license[
2
:]))
customer
=
random.choice(titles)
+
random.choice(surnames)
hired
=
today.addDays(
-
random.randint(
0
,
365
))
mileageout
=
random.randint(
10000
,
30000
)
notes
=
""
if
random.random() >
=
0.2
:
days
=
random.randint(
1
,
21
)
returned
=
hired.addDays(days)
mileageback
=
(mileageout
+
(days
*
random.randint(
30
,
300
)))
if
random.random() >
0.75
:
notes
=
random.choice(notetexts)
else
:
returned
=
QDate()
mileageback
=
0
log
=
CarHireLog(license, customer, hired, mileageout,
returned, mileageback, notes)
bisect.insort(
self
.logs, log)
def
rowCount(
self
, index
=
QModelIndex()):
return
len
(
self
.logs)
def
columnCount(
self
, index
=
QModelIndex()):
return
9
def
data(
self
, index, role):
if
not
index.isValid():
return
QVariant()
if
role
=
=
Qt.DisplayRole:
log
=
self
.logs[index.row()]
value
=
log.field(index.column())
if
(index.column()
in
(MILEAGEBACK, MILEAGE, DAYS)
and
value
=
=
0
):
return
0
return
value
if
(role
=
=
Qt.TextAlignmentRole
and
index.column()
not
in
(LICENSE, CUSTOMER, NOTES)):
return
QVariant(
int
(Qt.AlignRight|Qt.AlignVCenter))
if
role
=
=
Qt.BackgroundColorRole:
palette
=
QApplication.palette()
if
index.column()
in
(LICENSE, MILEAGE, DAYS):
return
QVariant(palette.alternateBase())
else
:
return
QVariant(palette.base())
return
QVariant()
def
setData(
self
, index, value, role
=
Qt.EditRole):
if
(index.isValid()
and
role
=
=
Qt.EditRole
and
index.column()
not
in
(LICENSE, MILEAGE, DAYS)):
log
=
self
.logs[index.row()]
column
=
index.column()
if
column
=
=
CUSTOMER:
log.customer
=
value
elif
column
=
=
HIRED:
#log.hired = value.toDate()
log.hired
=
value
elif
column
=
=
MILEAGEOUT:
log.mileageout
=
value
elif
column
=
=
RETURNED:
#log.returned = value.toDate()
log.returned
=
value
elif
column
=
=
MILEAGEBACK:
log.mileageback
=
value
elif
column
=
=
NOTES:
log.notes
=
value
self
.dataChanged[QModelIndex,QModelIndex].emit(index,index)
return
True
return
False
def
headerData(
self
, section, orientation, role):
if
role
=
=
Qt.TextAlignmentRole:
if
orientation
=
=
Qt.Horizontal:
return
QVariant(
int
(Qt.AlignCenter))
return
QVariant(
int
(Qt.AlignRight|Qt.AlignVCenter))
if
role !
=
Qt.DisplayRole:
return
QVariant()
if
orientation
=
=
Qt.Horizontal:
if
section
=
=
LICENSE:
return
"License"
elif
section
=
=
CUSTOMER:
return
"Customer"
elif
section
=
=
HIRED:
return
"Hired"
elif
section
=
=
MILEAGEOUT:
return
"Mileage #1"
elif
section
=
=
RETURNED:
return
"Returned"
elif
section
=
=
MILEAGEBACK:
return
"Mileage #2"
elif
section
=
=
DAYS:
return
"Days"
elif
section
=
=
MILEAGE:
return
"Miles"
elif
section
=
=
NOTES:
return
"Notes"
return
section
+
1
def
flags(
self
, index):
flag
=
QAbstractTableModel.flags(
self
, index)
if
index.column()
not
in
(LICENSE, MILEAGE, DAYS):
flag |
=
Qt.ItemIsEditable
return
flag
class
HireDateColumnDelegate(genericdelegates.DateColumnDelegate):
def
createEditor(
self
, parent, option, index):
i
=
index.sibling(index.row(), RETURNED)
self
.maximum
=
i.model().data(i, Qt.DisplayRole).addDays(
-
1
)
return
genericdelegates.DateColumnDelegate.createEditor(
self
, parent, option, index)
class
ReturnDateColumnDelegate(genericdelegates.DateColumnDelegate):
def
createEditor(
self
, parent, option, index):
i
=
index.sibling(index.row(), HIRED)
self
.minimum
=
i.model().data(i, Qt.DisplayRole).addDays(
1
)
return
genericdelegates.DateColumnDelegate.createEditor(
self
, parent, option, index)
class
MileageOutColumnDelegate(genericdelegates.IntegerColumnDelegate):
def
createEditor(
self
, parent, option, index):
i
=
index.sibling(index.row(), MILEAGEBACK)
maximum
=
i.model().data(i, Qt.DisplayRole)
self
.maximum
=
1000000
if
maximum
=
=
0
else
maximum
-
1
return
genericdelegates.IntegerColumnDelegate.createEditor(
self
, parent, option, index)
class
MileageBackColumnDelegate(genericdelegates.IntegerColumnDelegate):
def
createEditor(
self
, parent, option, index):
i
=
index.sibling(index.row(), MILEAGEOUT)
self
.minimum
=
i.model().data(i, Qt.DisplayRole)
+
1
return
genericdelegates.IntegerColumnDelegate.createEditor(
self
, parent, option, index)
class
MainForm(QMainWindow):
def
__init__(
self
, parent
=
None
):
super
(MainForm,
self
).__init__(parent)
model
=
CarHireModel(
self
)
self
.view
=
QTableView()
self
.view.setModel(model)
self
.view.resizeColumnsToContents()
delegate
=
genericdelegates.GenericDelegate(
self
)
delegate.insertColumnDelegate(CUSTOMER,
genericdelegates.PlainTextColumnDelegate())
earliest
=
QDate.currentDate().addYears(
-
3
)
delegate.insertColumnDelegate(HIRED,
HireDateColumnDelegate(earliest))
delegate.insertColumnDelegate(MILEAGEOUT,
MileageOutColumnDelegate(
0
,
1000000
))
delegate.insertColumnDelegate(RETURNED,
ReturnDateColumnDelegate(earliest))
delegate.insertColumnDelegate(MILEAGEBACK,
MileageBackColumnDelegate(
0
,
1000000
))
delegate.insertColumnDelegate(NOTES,
genericdelegates.RichTextColumnDelegate())
self
.view.setItemDelegate(delegate)
self
.setCentralWidget(
self
.view)
QShortcut(QKeySequence(
"Escape"
),
self
,
self
.close)
QShortcut(QKeySequence(
"Ctrl+Q"
),
self
,
self
.close)
self
.setWindowTitle(
"Car Hire Logs"
)
app
=
QApplication(sys.argv)
form
=
MainForm()
rect
=
QApplication.desktop().availableGeometry()
form.resize(
int
(rect.width()
*
0.7
),
int
(rect.height()
*
0.8
))
form.move(
0
,
0
)
form.show()
app.exec_()
|
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:https://blog.csdn.net/xiaoyangyang20/article/details/70257659 。
最后此篇关于python3+PyQt5泛型委托详解的文章就讲到这里了,如果你想了解更多关于python3+PyQt5泛型委托详解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我有一个加载 View 的 View ,需要将 View 推送到主导航 Controller 。 我已经为每个 View 设置了一个委托(delegate),并且基本上使我的调用沿着“链”返回到主导航
我通过 NSURLConnction 从服务器获取数据,并希望从获取的数组中填充表格 View 。数据出现在 NSURLConnection 委托(delegate)方法的日志中,但我意识到 numb
我已经将我用作标题 View 的 View 子类化,它里面有一些按钮委托(delegate),它工作得很好。 但是,我在 viewController 上方展示了一个 modalViewControl
我希望这听起来像是一个显而易见的问题,但是委托(delegate)返回类型是否也必须与其委托(delegate)的方法的返回类型相匹配? EG,像这样: public static void Save
我想使用 Kotlin 委托(delegate),但我不想在委托(delegate)人之外创建委托(delegate)。委托(delegate)的所有示例都如下所示: interface Worker
我有一个问题: - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInf
伙计们。我正在寻求帮助。这似乎是一个非常简单的任务,但我可以解决一整天。我正在尝试使用容器 View 创建侧面菜单。当用户按下“更多”按钮(barButtonItem)时,整个 View 向右滑动并出
我正在努力将 UIWebView 迁移到 WKWebView。我已经更改了所有委托(delegate)方法。我需要 WKWebView 委托(delegate)方法等于下面的 UIWebView 委托
我正在学习 VB.NET 中的委托(delegate),但对委托(delegate)类型感到困惑。在阅读有关委托(delegate)的内容时,我了解到委托(delegate)是一种数据类型,可以引用具
我有一个用 Objetive-C 构建的框架。该框架用于连接蓝牙设备并与之交互。 在演示代码中,Objetive-C 委托(delegate)函数如下所示。演示代码由框架创建者提供。 -(void)b
//NewCharts.h #import @interface NewCharts : UIViewController @property(nonatomic,retain)IBOutlet U
我正在努力了解 async/await 并认为我确实了解有关用法的一些事情。但仍然不太清楚在下面的场景中实际好处是什么。 查看 Task.Run 用法。第一种方法使用普通委托(delegate)并使用
我已经尝试了在我的网站上使用 openID 委托(delegate)的所有可能选项,但没有一个方法对我有用。 我的 HTML 文件的 head 部分有“link rel”标签。 我在 HTML 文件的
如何快速创建一个委托(delegate),即 NSUserNotificationCenterDelegate? 最佳答案 这里有一些关于两个 View Controller 之间委托(delegat
我有一个带有数据源的 NSComboBox,当您单击三角形并通过单击它选择其中一个项目时,它可以完美运行。但是,我还希望它允许用户在框中键入以使用自动完成来选择名称。目前,当用户键入时,我希望选择的项
我在代码中定义了以下类和委托(delegate)(为简洁起见,剪掉了许多其他行)。 Public Delegate Sub TimerCallback(canceled As Boolean) Pub
我使用 ansible 2.1 并且我想使用 delegate_to 向一组主机运行命令。我使用 localhost 作为主机参数,并且我想将“touch”命令委托(delegate)给两个 cls
我想通过重载为我的类实现几个构造函数。据我了解,遵循 DRY 原则的惯用方式是使用一种称为委托(delegate)构造函数的功能。我也看到了关于在任何地方使用引用参数并不惜一切代价避免使用指针的想法,
代表们会导致内存泄漏吗? 我的意思是,例如如果一个类A包含一个ADelegate,并且后者指向BMethod(属于B类),这可以防止GC收集A类或B类吗? 如果是这样,我们如何“释放”代表(设置ADe
委托(delegate)命令和路由命令有什么区别? 我读了一些文章说在 MVVM 上使用委托(delegate)命令而不是路由命令。 那么当我们使用 MVVM 时,Delegate Command 相
我是一名优秀的程序员,十分优秀!