python3+PyQt5 使用自定义模型保存数据并通过不同视图形式展示数据
来源:互联网 发布:特高压标准知乎 编辑:程序博客网 时间:2024/05/17 08:22
本文通过将创建自定义模型保存船只数据,并会用两个不同的表格视图形式来显示同样的模型。
实现自定义模型,继承QAbstractTableModel,具体参照QT库。
/home/yrd/eric_workspace/chap14/ships_model/ships.py
#!/usr/bin/env python3import platformfrom PyQt5.QtCore import (QAbstractTableModel, QDataStream, QFile, QIODevice, QModelIndex,QVariant, Qt,pyqtSignal)from PyQt5.QtGui import QColorfrom PyQt5.QtWidgets import QApplicationNAME, OWNER, COUNTRY, DESCRIPTION, TEU = range(5)MAGIC_NUMBER = 0x570C4FILE_VERSION = 1class Ship(object): def __init__(self, name, owner, country, teu=0, description=""): self.name = name self.owner = owner self.country = country self.teu = teu self.description = description def __hash__(self): return super(Ship, self).__hash__() def __lt__(self, other): return bool(self.name.lower()<other.name.lower()) def __eq__(self, other): return bool(self.name.lower()==other.name.lower())class ShipTableModel(QAbstractTableModel): dataChanged = pyqtSignal(QModelIndex,QModelIndex) def __init__(self, filename=""): super(ShipTableModel, self).__init__() self.filename = filename self.dirty = False self.ships = [] self.owners = set() self.countries = set() def sortByName(self): self.beginResetModel() self.ships = sorted(self.ships) self.endResetModel() def sortByCountryOwner(self): self.beginResetModel() self.ships = sorted(self.ships, key=lambda x: (x.country, x.owner, x.name)) self.endResetModel() def flags(self, index): if not index.isValid(): return Qt.ItemIsEnabled return Qt.ItemFlags( QAbstractTableModel.flags(self, index)| Qt.ItemIsEditable) def data(self, index, role=Qt.DisplayRole): if (not index.isValid() or not (0 <= index.row() < len(self.ships))): return QVariant() ship = self.ships[index.row()] column = index.column() if role == Qt.DisplayRole: if column == NAME: return ship.name elif column == OWNER: return ship.owner elif column == COUNTRY: return ship.country elif column == DESCRIPTION: return ship.description elif column == TEU: return ship.teu elif role == Qt.TextAlignmentRole: if column == TEU: return QVariant(int(Qt.AlignRight|Qt.AlignVCenter)) return QVariant(int(Qt.AlignLeft|Qt.AlignVCenter)) elif role == Qt.TextColorRole and column == TEU: if ship.teu < 80000: return QVariant(QColor(Qt.black)) elif ship.teu < 100000: return QVariant(QColor(Qt.darkBlue)) elif ship.teu < 120000: return QVariant(QColor(Qt.blue)) else: return QVariant(QColor(Qt.red)) elif role == Qt.BackgroundColorRole: if ship.country in ("Bahamas", "Cyprus", "Denmark", "France", "Germany", "Greece"): return QVariant(QColor(250, 230, 250)) elif ship.country in ("Hong Kong", "Japan", "Taiwan"): return QVariant(QColor(250, 250, 230)) elif ship.country in ("Marshall Islands",): return QVariant(QColor(230, 250, 250)) else: return QVariant(QColor(210, 230, 230)) return QVariant() def headerData(self, section, orientation, role=Qt.DisplayRole): if role == Qt.TextAlignmentRole: if orientation == Qt.Horizontal: return QVariant(int(Qt.AlignLeft|Qt.AlignVCenter)) return QVariant(int(Qt.AlignRight|Qt.AlignVCenter)) if role != Qt.DisplayRole: return QVariant() if orientation == Qt.Horizontal: if section == NAME: return QVariant("Name") elif section == OWNER: return QVariant("Owner") elif section == COUNTRY: return QVariant("Country") elif section == DESCRIPTION: return QVariant("Description") elif section == TEU: return QVariant("TEU") return QVariant(int(section + 1)) def rowCount(self, index=QModelIndex()): return len(self.ships) def columnCount(self, index=QModelIndex()): return 5 def setData(self, index, value, role=Qt.EditRole): if index.isValid() and 0 <= index.row() < len(self.ships): #self.beginResetModel() ship = self.ships[index.row()] column = index.column() if column == NAME: ship.name = str(value) elif column == OWNER: ship.owner = str(value) elif column == COUNTRY: ship.country = str(value) elif column == DESCRIPTION: ship.description = str(value) elif column == TEU: if str(value).isdecimal(): ship.teu=int(value) self.dirty = True self.dataChanged[QModelIndex,QModelIndex].emit(index,index) #self.endResetModel() return True return False def insertRows(self, position, rows=1, index=QModelIndex()): self.beginInsertRows(QModelIndex(), position, position + rows - 1) for row in range(rows): self.ships.insert(position + row, Ship(" Unknown", " Unknown", " Unknown")) self.endInsertRows() self.dirty = True return True def removeRows(self, position, rows=1, index=QModelIndex()): self.beginRemoveRows(QModelIndex(), position, position + rows - 1) self.ships = (self.ships[:position] + self.ships[position + rows:]) self.endRemoveRows() self.dirty = True return True def load(self): exception = None fh = None try: if not self.filename: raise IOError("no filename specified for loading") fh = QFile(self.filename) if not fh.open(QIODevice.ReadOnly): raise IOError(str(fh.errorString())) stream = QDataStream(fh) magic = stream.readInt32() if magic != MAGIC_NUMBER: raise IOError("unrecognized file type") fileVersion = stream.readInt16() if fileVersion != FILE_VERSION: raise IOError("unrecognized file type version") self.ships = [] while not stream.atEnd(): name = "" owner = "" country = "" description = "" name=stream.readQString() owner=stream.readQString() country=stream.readQString() description=stream.readQString() teu = stream.readInt32() self.ships.append(Ship(name, owner, country, teu, description)) self.owners.add(str(owner)) self.countries.add(str(country)) self.dirty = False except IOError as e: exception = e finally: if fh is not None: fh.close() if exception is not None: raise exception def save(self): exception = None fh = None try: if not self.filename: raise IOError("no filename specified for saving") fh = QFile(self.filename) if not fh.open(QIODevice.WriteOnly): raise IOError(str(fh.errorString())) stream = QDataStream(fh) stream.writeInt32(MAGIC_NUMBER) stream.writeInt16(FILE_VERSION) stream.setVersion(QDataStream.Qt_5_7) for ship in self.ships: stream.writeQString(ship.name) stream.writeQString(ship.owner) stream.writeQString(ship.country) stream.writeQString(ship.description) stream.writeInt32(ship.teu) self.dirty = False except IOError as e: exception = e finally: if fh is not None: fh.close() if exception is not None: raise exceptiondef generateFakeShips(): for name, owner, country, teu, description in (("Emma M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 151687, "<b>W\u00E4rtsil\u00E4-Sulzer RTA96-C</b> main engine," "<font color=green>109,000 hp</font>"),("MSC Pamela", "MSC", "Liberia", 90449, "Draft <font color=green>15m</font>"),("Colombo Express", "Hapag-Lloyd", "Germany", 93750, "Main engine, <font color=green>93,500 hp</font>"),("Houston Express", "Norddeutsche Reederei", "Germany", 95000, "Features a <u>twisted leading edge full spade rudder</u>. " "Sister of <i>Savannah Express</i>"),("Savannah Express", "Norddeutsche Reederei", "Germany", 95000, "Sister of <i>Houston Express</i>"),("MSC Susanna", "MSC", "Liberia", 90449, ""),("Eleonora M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 151687, "Captain <i>Hallam</i>"),("Estelle M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 151687, "Captain <i>Wells</i>"),("Evelyn M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 151687, "Captain <i>Byrne</i>"),("Georg M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 97933, ""),("Gerd M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 97933, ""),("Gjertrud M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 97933, ""),("Grete M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 97933, ""),("Gudrun M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 97933, ""),("Gunvor M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 97933, ""),("CSCL Le Havre", "Danaos Shipping", "Cyprus", 107200, ""),("CSCL Pusan", "Danaos Shipping", "Cyprus", 107200, "Captain <i>Watts</i>"),("Xin Los Angeles", "China Shipping Container Lines (CSCL)", "Hong Kong", 107200, ""),("Xin Shanghai", "China Shipping Container Lines (CSCL)", "Hong Kong", 107200, ""),("Cosco Beijing", "Costamare Shipping", "Greece", 99833, ""),("Cosco Hellas", "Costamare Shipping", "Greece", 99833, ""),("Cosco Guangzho", "Costamare Shipping", "Greece", 99833, ""),("Cosco Ningbo", "Costamare Shipping", "Greece", 99833, ""),("Cosco Yantian", "Costamare Shipping", "Greece", 99833, ""),("CMA CGM Fidelio", "CMA CGM", "France", 99500, ""),("CMA CGM Medea", "CMA CGM", "France", 95000, ""),("CMA CGM Norma", "CMA CGM", "Bahamas", 95000, ""),("CMA CGM Rigoletto", "CMA CGM", "France", 99500, ""),("Arnold M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 93496, "Captain <i>Morrell</i>"),("Anna M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 93496, "Captain <i>Lockhart</i>"),("Albert M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 93496, "Captain <i>Tallow</i>"),("Adrian M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 93496, "Captain <i>G. E. Ericson</i>"),("Arthur M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 93496, ""),("Axel M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 93496, ""),("NYK Vega", "Nippon Yusen Kaisha", "Panama", 97825, ""),("MSC Esthi", "MSC", "Liberia", 99500, ""),("MSC Chicago", "Offen Claus-Peter", "Liberia", 90449, ""),("MSC Bruxelles", "Offen Claus-Peter", "Liberia", 90449, ""),("MSC Roma", "Offen Claus-Peter", "Liberia", 99500, ""),("MSC Madeleine", "MSC", "Liberia", 107551, ""),("MSC Ines", "MSC", "Liberia", 107551, ""),("Hannover Bridge", "Kawasaki Kisen Kaisha", "Japan", 99500, ""),("Charlotte M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 91690, ""),("Clementine M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 91690, ""),("Columbine M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 91690, ""),("Cornelia M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 91690, ""),("Chicago Express", "Hapag-Lloyd", "Germany", 93750, ""),("Kyoto Express", "Hapag-Lloyd", "Germany", 93750, ""),("Clifford M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 91690, ""),("Sally M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 91690, ""),("Sine M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 91690, ""),("Skagen M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 91690, ""),("Sofie M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 91690, ""),("Sor\u00F8 M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 91690, ""),("Sovereing M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 91690, ""),("Susan M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 91690, ""),("Svend M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 91690, ""),("Svendborg M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 91690, ""),("A.P. M\u00F8ller", "M\u00E6rsk Line", "Denmark", 91690, "Captain <i>Ferraby</i>"),("Caroline M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 91690, ""),("Carsten M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 91690, ""),("Chastine M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 91690, ""),("Cornelius M\u00E6rsk", "M\u00E6rsk Line", "Denmark", 91690, ""),("CMA CGM Otello", "CMA CGM", "France", 91400, ""),("CMA CGM Tosca", "CMA CGM", "France", 91400, ""),("CMA CGM Nabucco", "CMA CGM", "France", 91400, ""),("CMA CGM La Traviata", "CMA CGM", "France", 91400, ""),("CSCL Europe", "Danaos Shipping", "Cyprus", 90645, ""),("CSCL Africa", "Seaspan Container Line", "Cyprus", 90645, ""),("CSCL America", "Danaos Shipping ", "Cyprus", 90645, ""),("CSCL Asia", "Seaspan Container Line", "Hong Kong", 90645, ""),("CSCL Oceania", "Seaspan Container Line", "Hong Kong", 90645, "Captain <i>Baker</i>"),("M\u00E6rsk Seville", "Blue Star GmbH", "Liberia", 94724, ""),("M\u00E6rsk Santana", "Blue Star GmbH", "Liberia", 94724, ""),("M\u00E6rsk Sheerness", "Blue Star GmbH", "Liberia", 94724, ""),("M\u00E6rsk Sarnia", "Blue Star GmbH", "Liberia", 94724, ""),("M\u00E6rsk Sydney", "Blue Star GmbH", "Liberia", 94724, ""),("MSC Heidi", "MSC", "Panama", 95000, ""),("MSC Rania", "MSC", "Panama", 95000, ""),("MSC Silvana", "MSC", "Panama", 95000, ""),("M\u00E6rsk Stralsund", "Blue Star GmbH", "Liberia", 95000, ""),("M\u00E6rsk Saigon", "Blue Star GmbH", "Liberia", 95000, ""),("M\u00E6rsk Seoul", "Blue Star Ship Managment GmbH", "Germany", 95000, ""),("M\u00E6rsk Surabaya", "Offen Claus-Peter", "Germany", 98400, ""),("CMA CGM Hugo", "NSB Niederelbe", "Germany", 90745, ""),("CMA CGM Vivaldi", "CMA CGM", "Bahamas", 90745, ""),("MSC Rachele", "NSB Niederelbe", "Germany", 90745, ""),("Pacific Link", "NSB Niederelbe", "Germany", 90745, ""),("CMA CGM Carmen", "E R Schiffahrt", "Liberia", 89800, ""),("CMA CGM Don Carlos", "E R Schiffahrt", "Liberia", 89800, ""),("CMA CGM Don Giovanni", "E R Schiffahrt", "Liberia", 89800, ""),("CMA CGM Parsifal", "E R Schiffahrt", "Liberia", 89800, ""),("Cosco China", "E R Schiffahrt", "Liberia", 91649, ""),("Cosco Germany", "E R Schiffahrt", "Liberia", 89800, ""),("Cosco Napoli", "E R Schiffahrt", "Liberia", 89800, ""),("YM Unison", "Yang Ming Line", "Taiwan", 88600, ""),("YM Utmost", "Yang Ming Line", "Taiwan", 88600, ""),("MSC Lucy", "MSC", "Panama", 89954, ""),("MSC Maeva", "MSC", "Panama", 89954, ""),("MSC Rita", "MSC", "Panama", 89954, ""),("MSC Busan", "Offen Claus-Peter", "Panama", 89954, ""),("MSC Beijing", "Offen Claus-Peter", "Panama", 89954, ""),("MSC Toronto", "Offen Claus-Peter", "Panama", 89954, ""),("MSC Charleston", "Offen Claus-Peter", "Panama", 89954, ""),("MSC Vittoria", "MSC", "Panama", 89954, ""),("Ever Champion", "NSB Niederelbe", "Marshall Islands", 90449, "Captain <i>Phillips</i>"),("Ever Charming", "NSB Niederelbe", "Marshall Islands", 90449, "Captain <i>Tonbridge</i>"),("Ever Chivalry", "NSB Niederelbe", "Marshall Islands", 90449, ""),("Ever Conquest", "NSB Niederelbe", "Marshall Islands", 90449, ""),("Ital Contessa", "NSB Niederelbe", "Marshall Islands", 90449, ""),("Lt Cortesia", "NSB Niederelbe", "Marshall Islands", 90449, ""),("OOCL Asia", "OOCL", "Hong Kong", 89097, ""),("OOCL Atlanta", "OOCL", "Hong Kong", 89000, ""),("OOCL Europe", "OOCL", "Hong Kong", 89097, ""),("OOCL Hamburg", "OOCL", "Marshall Islands", 89097, ""),("OOCL Long Beach", "OOCL", "Marshall Islands", 89097, ""),("OOCL Ningbo", "OOCL", "Marshall Islands", 89097, ""),("OOCL Shenzhen", "OOCL", "Hong Kong", 89097, ""),("OOCL Tianjin", "OOCL", "Marshall Islands", 89097, ""),("OOCL Tokyo", "OOCL", "Hong Kong", 89097, "")): yield Ship(name, owner, country, teu, description)
/home/yrd/eric_workspace/chap14/ships_model/ships-model.pyw
#!/usr/bin/env python3import sysfrom PyQt5.QtCore import QFile, QTimer, Qtfrom PyQt5.QtWidgets import (QApplication, QDialog, QHBoxLayout, QLabel, QMessageBox, QPushButton, QSplitter, QTableView, QVBoxLayout, QWidget)import shipsMAC = Truetry: from PyQt5.QtGui import qt_mac_set_native_menubarexcept ImportError: MAC = Falseclass MainForm(QDialog): def __init__(self, parent=None): super(MainForm, self).__init__(parent) self.model = ships.ShipTableModel("ships.dat") tableLabel1 = QLabel("Table &1") self.tableView1 = QTableView() tableLabel1.setBuddy(self.tableView1) self.tableView1.setModel(self.model) tableLabel2 = QLabel("Table &2") self.tableView2 = QTableView() tableLabel2.setBuddy(self.tableView2) self.tableView2.setModel(self.model) addShipButton = QPushButton("&Add Ship") removeShipButton = QPushButton("&Remove Ship") quitButton = QPushButton("&Quit") if not MAC: addShipButton.setFocusPolicy(Qt.NoFocus) removeShipButton.setFocusPolicy(Qt.NoFocus) quitButton.setFocusPolicy(Qt.NoFocus) buttonLayout = QHBoxLayout() buttonLayout.addWidget(addShipButton) buttonLayout.addWidget(removeShipButton) buttonLayout.addStretch() buttonLayout.addWidget(quitButton) splitter = QSplitter(Qt.Horizontal) vbox = QVBoxLayout() vbox.addWidget(tableLabel1) vbox.addWidget(self.tableView1) widget = QWidget() widget.setLayout(vbox) splitter.addWidget(widget) vbox = QVBoxLayout() vbox.addWidget(tableLabel2) vbox.addWidget(self.tableView2) widget = QWidget() widget.setLayout(vbox) splitter.addWidget(widget) layout = QVBoxLayout() layout.addWidget(splitter) layout.addLayout(buttonLayout) self.setLayout(layout) for tableView in (self.tableView1, self.tableView2): header = tableView.horizontalHeader() header.sectionClicked[int].connect(self.sortTable) addShipButton.clicked.connect(self.addShip) removeShipButton.clicked.connect(self.removeShip) quitButton.clicked.connect(self.accept) self.setWindowTitle("Ships (model)") QTimer.singleShot(0, self.initialLoad) def initialLoad(self): if not QFile.exists(self.model.filename): self.model.beginResetModel() for ship in ships.generateFakeShips(): self.model.ships.append(ship) self.model.owners.add(str(ship.owner)) self.model.countries.add(str(ship.country)) self.model.endResetModel() self.model.dirty = False else: try: self.model.load() except IOError as e: QMessageBox.warning(self, "Ships - Error", "Failed to load: {0}".format(e)) self.model.sortByName() self.resizeColumns() def resizeColumns(self): for tableView in (self.tableView1, self.tableView2): for column in (ships.NAME, ships.OWNER, ships.COUNTRY, ships.TEU): tableView.resizeColumnToContents(column) def reject(self): self.accept() def accept(self): if (self.model.dirty and QMessageBox.question(self, "Ships - Save?", "Save unsaved changes?", QMessageBox.Yes|QMessageBox.No) == QMessageBox.Yes): try: self.model.save() except IOError as e: QMessageBox.warning(self, "Ships - Error", "Failed to save: {0}".format(e)) QDialog.accept(self) def sortTable(self, section): if section in (ships.OWNER, ships.COUNTRY): self.model.sortByCountryOwner() else: self.model.sortByName() self.resizeColumns() def addShip(self): row = self.model.rowCount() self.model.insertRows(row) index = self.model.index(row, 0) tableView = self.tableView1 if self.tableView2.hasFocus(): tableView = self.tableView2 tableView.setFocus() tableView.setCurrentIndex(index) tableView.edit(index) def removeShip(self): tableView = self.tableView1 if self.tableView2.hasFocus(): tableView = self.tableView2 index = tableView.currentIndex() if not index.isValid(): return row = index.row() name = self.model.data( self.model.index(row, ships.NAME)) owner = self.model.data( self.model.index(row, ships.OWNER)) country = self.model.data( self.model.index(row, ships.COUNTRY)) if (QMessageBox.question(self, "Ships - Remove", "Remove {0} of {1}/{2}?".format(name,owner,country), QMessageBox.Yes|QMessageBox.No) == QMessageBox.No): return self.model.removeRows(row) self.resizeColumns()app = QApplication(sys.argv)form = MainForm()form.show()app.exec_()
运行结果:
0 0
- python3+PyQt5 使用自定义模型保存数据并通过不同视图形式展示数据
- python3+PyQt5 使用自定义模型保存数据并通过不同视图形式展示数据
- python3+PyQt5 自定义视图
- python3+PyQt5 使用三种不同的简便项窗口部件显示数据
- python3+PyQt5 使用三种不同的简便项窗口部件显示数据
- python3+PyQt5 重新实现自定义数据拖放处理
- python3+PyQt5 使用自定义委托控制数据项的展示和 编辑
- python3+PyQt5 使用数据库窗口视图
- python3+PyQt5 使用数据库表视图
- python3+PyQt5 树中表达表格数据
- AppDelegate存储全局变量和 NSUserDefaults standardUserDefaults 通过模型保存和读取数据,存储自定义的对象
- 将dataGridView中的数据全选并以txt形式保存
- 通过一个接口获取json数据并展示
- python3:mysql查询数据并通过邮件群发excel附件
- Android:使用LoadingLayout来展示加载数据时不同状态
- 使用matlab读取excel数据并保存
- iOS uitableview自定义相册(实现拍照并保存在指定目录以相册的形式展示图片)
- python3+PyQt5 自定义窗口部件--使用窗口部件样式表
- Prime Ring Problem
- Android 相关资源下载链接
- java 手动调用System.gc();
- JTable使用
- C++继承详解二——默认成员函数
- python3+PyQt5 使用自定义模型保存数据并通过不同视图形式展示数据
- 每天一个Linux命令(51):ping
- underscore.js模板内循环输出
- 转载--HashMap实现原理分析
- tensorflow保存加载模型查看训练参数
- linux英文缩写含义
- java 数据类型转换遇到的坑
- VMware Network Adapter VMnet1/8详解
- C#+VC打造炫酷USB电脑锁<一>引言