示例代码1:
首先看下运行效果:
1:采用QTableView的MVC模式;
2:支持排序
代码:
''' pqt_tableview3.py
explore PyQT's QTableView Model
using QAbstractTableModel to present tabular data
allow table sorting by clicking on the header title
used the Anaconda package (comes with PyQt4) on OS X
(dns)
'''
import operator # used for sorting
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class MyWindow(QWidget):
def __init__(self, dataList, header, *args):
QWidget.__init__(self, *args)
# setGeometry(x_pos, y_pos, width, height)
self.setGeometry(70, 150, 420, 250)
self.setWindowTitle("Click on the header to sort table")
table_model = MyTableModel(self, dataList, header)
table_view = QTableView()
# bind cell click to a method reference
table_view.clicked.connect(self.showSelection)
table_view.setModel(table_model)
# enable sorting
table_view.setSortingEnabled(True)
layout = QVBoxLayout(self)
layout.addWidget(table_view)
self.setLayout(layout)
def showSelection(self, item):
cellContent = item.data()
print(cellContent) # test
sf = "You clicked on {}".format(cellContent)
# display in title bar for convenience
self.setWindowTitle(sf)
class MyTableModel(QAbstractTableModel):
"""
keep the method names
they are an integral part of the model
"""
def __init__(self, parent, mylist, header, *args):
QAbstractTableModel.__init__(self, parent, *args)
self.mylist = mylist
self.header = header
def rowCount(self, parent):
return len(self.mylist)
def columnCount(self, parent):
return len(self.mylist[0])
def data(self, index, role):
if not index.isValid():
return None
elif role != Qt.DisplayRole:
return None
return self.mylist[index.row()][index.column()]
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self.header[col]
return None
def sort(self, col, order):
"""sort table by given column number col"""
self.emit(SIGNAL("layoutAboutToBeChanged()"))
self.mylist = sorted(self.mylist,
key=operator.itemgetter(col))
if order == Qt.DescendingOrder:
self.mylist.reverse()
self.emit(SIGNAL("layoutChanged()"))
# you could process a CSV file to create this data
header = ['First Name', 'Last Name', 'Age', 'Weight']
# a list of (fname, lname, age, weight) tuples
dataList = [
('Ben', 'Dover', 36, 127),
('Foster', 'Krampf', 27, 234),
('Barry', 'Chaurus', 19, 315),
('Sede', 'Anowski', 59, 147),
('Carolus', 'Gabel', 94, 102),
('Michel', 'Zittus', 21, 175),
('Annie', 'Waters', 31, 114)
]
app = QApplication([])
win = MyWindow(dataList, header)
win.show()
app.exec_()
示例代码2:
首先看下效果:
代码:
import random, sys
from PyQt4.QtCore import Qt, QVariant
from PyQt4.QtGui import *
class NumberSortModel(QSortFilterProxyModel):
def lessThan(self, left, right):
print("lvalue", left.data())
print("lvalue", right.data())
lvalue = left.data()
rvalue = right.data()
return lvalue < rvalue
if __name__ == "__main__":
app = QApplication(sys.argv)
model = QStandardItemModel(5, 5)
random.seed()
for i in range(5):
for j in range(5):
item = QStandardItem()
item.setData(random.randint(-500, 500)/10.0, Qt.DisplayRole)
model.setItem(i, j, item)
proxy = NumberSortModel()
proxy.setSourceModel(model)
view = QTableView()
view.setModel(proxy)
view.setSortingEnabled(True)
view.show()
sys.exit(app.exec_())
文章的脚注信息由WordPress的wp-posturl插件自动生成


微信扫一扫,打赏作者吧~![[整理]how to run flask with pyqt5](http://www.jyguagua.com/wp-content/themes/begin/timthumb.php?src=http://www.jyguagua.com/wp-content/uploads/2021/03/pyqt_flask.png&w=280&h=210&zc=1)
![[已解决]LINK : fatal error LNK1158: cannot run 'rc.exe' 错误的解决办法](http://www.jyguagua.com/wp-content/themes/begin/timthumb.php?src=http://www.jyguagua.com/wp-content/uploads/2021/02/Snipaste_2021-02-17_15-18-26-1024x505.png&w=280&h=210&zc=1)
![[已解决]Python扩展模块 error: Unable to find vcvarsall.bat](http://www.jyguagua.com/wp-content/themes/begin/timthumb.php?src=http://www.jyguagua.com/wp-content/uploads/2020/11/Snipaste_2020-11-19_10-01-38.png&w=280&h=210&zc=1)
![[整理]PyQt画圆,动态变色](http://www.jyguagua.com/wp-content/themes/begin/timthumb.php?src=http://www.jyguagua.com/wp-content/uploads/2020/08/drawCircle.gif&w=280&h=210&zc=1)