本文通过python3+pyqt5改写实现了python qt gui 编程变成15章的excise例子。
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
|
#!/usr/bin/env python3
import os
import sys
from pyqt5.qtcore import (qfile, qvariant, qt)
from pyqt5.qtwidgets import (qapplication, qdialog, qdialogbuttonbox, qmenu,
qmessagebox, qtableview, qvboxlayout)
from pyqt5.qtsql import (qsqldatabase, qsqlquery, qsqltablemodel)
mac = true
try :
from pyqt5.qtgui import qt_mac_set_native_menubar
except importerror:
mac = false
id , category, shortdesc, longdesc = range ( 4 )
class referencedatadlg(qdialog):
def __init__( self , parent = none):
super (referencedatadlg, self ).__init__(parent)
self .model = qsqltablemodel( self )
self .model.settable( "reference" )
self .model.setsort( id , qt.ascendingorder)
self .model.setheaderdata( id , qt.horizontal, "id" )
self .model.setheaderdata(category, qt.horizontal, "category" )
self .model.setheaderdata(shortdesc, qt.horizontal, "short desc." )
self .model.setheaderdata(longdesc, qt.horizontal, "long desc." )
self .model.select()
self .view = qtableview()
self .view.setmodel( self .model)
self .view.setselectionmode(qtableview.singleselection)
self .view.setselectionbehavior(qtableview.selectrows)
self .view.setcolumnhidden( id , true)
self .view.resizecolumnstocontents()
buttonbox = qdialogbuttonbox()
addbutton = buttonbox.addbutton( "&add" ,
qdialogbuttonbox.actionrole)
deletebutton = buttonbox.addbutton( "&delete" ,
qdialogbuttonbox.actionrole)
sortbutton = buttonbox.addbutton( "&sort" ,
qdialogbuttonbox.actionrole)
if not mac:
addbutton.setfocuspolicy(qt.nofocus)
deletebutton.setfocuspolicy(qt.nofocus)
sortbutton.setfocuspolicy(qt.nofocus)
menu = qmenu( self )
sortbycategoryaction = menu.addaction( "sort by &category" )
sortbydescriptionaction = menu.addaction( "sort by &description" )
sortbyidaction = menu.addaction( "sort by &id" )
sortbutton.setmenu(menu)
closebutton = buttonbox.addbutton(qdialogbuttonbox.close)
layout = qvboxlayout()
layout.addwidget( self .view)
layout.addwidget(buttonbox)
self .setlayout(layout)
addbutton.clicked.connect( self .addrecord)
deletebutton.clicked.connect( self .deleterecord)
sortbycategoryaction.triggered.connect( lambda : self .sort(category))
sortbydescriptionaction.triggered.connect( lambda : self .sort(shortdesc))
sortbyidaction.triggered.connect( lambda : self .sort( id ))
closebutton.clicked.connect( self .accept)
self .setwindowtitle( "reference data" )
def addrecord( self ):
row = self .model.rowcount()
self .model.insertrow(row)
index = self .model.index(row, category)
self .view.setcurrentindex(index)
self .view.edit(index)
def deleterecord( self ):
index = self .view.currentindex()
if not index.isvalid():
return
record = self .model.record(index.row())
category = record.value(category)
desc = record.value(shortdesc)
if (qmessagebox.question( self , "reference data" ,
( "delete {0} from category {1}?"
. format (desc,category)),
qmessagebox.yes|qmessagebox.no) = =
qmessagebox.no):
return
self .model.removerow(index.row())
self .model.submitall()
self .model.select()
def sort( self , column):
self .model.setsort(column, qt.ascendingorder)
self .model.select()
def main():
app = qapplication(sys.argv)
filename = os.path.join(os.path.dirname(__file__), "reference.db" )
create = not qfile.exists(filename)
db = qsqldatabase.adddatabase( "qsqlite" )
db.setdatabasename(filename)
if not db. open ():
qmessagebox.warning(none, "reference data" ,
"database error: {0}" . format (db.lasterror().text()))
sys.exit( 1 )
if create:
query = qsqlquery()
query.exec_( """create table reference (
id integer primary key autoincrement unique not null,
category varchar(30) not null,
shortdesc varchar(20) not null,
longdesc varchar(80))""" )
form = referencedatadlg()
form.show()
sys.exit(app.exec_())
main()
|
运行结果:
以上这篇python3+pyqt5 数据库编程--增删改实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/xiaoyangyang20/article/details/70240690