pyqt model/view框架 1.第一個model


>關於Qt的mvc模式就不多說了,網上很多很多,這里按着 Chen Chun-Chia的`《PyQt's Model/View Framework》`走一邊 我的第一個model --- class MyListModel(QAbstractListModel): """ 我的第一個模型 """ def __init__(self,parent=None): super(MyListModel,self).__init__(parent) #這是數據 self._data=[70,90,20,50] pass def rowCount(self, parent=QModelIndex()): """ 這個方法返回了數據的行數 也就是有多少個條目得數據 """ return len(self._data) def data(self,index,role=Qt.DisplayRole): """ 根據當前index索引,返回當前的數據 然后再由Qt進行渲染顯示 """ #如果當前得索引是不活動得 if not index.isValid() or not 0 <= index.row() < self.rowCount(): #亦或者當前的索引值不在合理范圍,即小於等於0,超出總行數 return QVariant() #返回一個QVariant,相當與空條目 #從索引取得當前的航號 row=index.row() #如果當前角色是DisplayRole if role==Qt.DisplayRole: #返回當前行的數據 return self._data[row] #如果角色不滿足需求,則返回QVariant return QVariant() 代碼中,我們覆蓋了一個model中最基本得方法: `rowCount` 數據總行數; `data` 獲取數據; 並在__init__構造方法中,設置好數據源`self._data` 運行下面的代碼試試看: # -*- coding: utf-8 -*- import sys from PyQt4.QtCore import * from PyQt4.QtGui import * #################################################################### def main(): app=QApplication(sys.argv) #新建一個ListView view=QListView() #新建一個自定義Model model=MyListModel() #設置view的model view.setModel(model) view.show() sys.exit(app.exec_()) #################################################################### class MyListModel(QAbstractListModel): """ 我的第一個模型 """ def __init__(self,parent=None): super(MyListModel,self).__init__(parent) #這是數據 self._data=[70,90,20,50] pass def rowCount(self, parent=QModelIndex()): """ 這個方法返回了數據的行數 也就是有多少個條目得數據 """ return len(self._data) def data(self,index,role=Qt.DisplayRole): """ 根據當前index索引,返回當前的數據 然后再由Qt進行渲染顯示 """ #如果當前得索引是不活動得 if not index.isValid() or not 0 <= index.row() < self.rowCount(): #亦或者當前的索引值不在合理范圍,即小於等於0,超出總行數 return QVariant() #返回一個QVariant,相當與空條目 #從索引取得當前的航號 row=index.row() #如果當前角色是DisplayRole if role==Qt.DisplayRole: #返回當前行的數據 return self._data[row] #如果角色不滿足需求,則返回QVariant return QVariant() #################################################################### if __name__ == "__main__": main() 角色類型(Qt.ItemDataRole) --- The general purpose roles (and the associated types) are: Constant Value Description Qt.DisplayRole 0 The key data to be rendered in the form of text. (QString) Qt.DecorationRole 1 The data to be rendered as a decoration in the form of an icon. (QColor, QIcon or QPixmap) Qt.EditRole 2 The data in a form suitable for editing in an editor. (QString) Qt.ToolTipRole 3 The data displayed in the item's tooltip. (QString) Qt.StatusTipRole 4 The data displayed in the status bar. (QString) Qt.WhatsThisRole 5 The data displayed for the item in "What's This?" mode. (QString) Qt.SizeHintRole 13 The size hint for the item that will be supplied to views. (QSize) Roles describing appearance and meta data (with associated types): Constant Value Description Qt.FontRole 6 The font used for items rendered with the default delegate. (QFont) Qt.TextAlignmentRole 7 The alignment of the text for items rendered with the default delegate. (Qt.AlignmentFlag) Qt.BackgroundRole 8 The background brush used for items rendered with the default delegate. (QBrush) Qt.BackgroundColorRole 8 This role is obsolete. Use BackgroundRole instead. Qt.ForegroundRole 9 The foreground brush (text color, typically) used for items rendered with the default delegate. (QBrush) Qt.TextColorRole 9 This role is obsolete. Use ForegroundRole instead. Qt.CheckStateRole 10 This role is used to obtain the checked state of an item. (Qt.CheckState) Qt.InitialSortOrderRole 14 This role is used to obtain the initial sort order of a header view section. (Qt.SortOrder). This role was introduced in Qt 4.8. Accessibility roles (with associated types): Constant Value Description Qt.AccessibleTextRole 11 The text to be used by accessibility extensions and plugins, such as screen readers. (QString) Qt.AccessibleDescriptionRole 12 A description of the item for accessibility purposes. (QString) User roles: Constant Value Description Qt.UserRole 32 The first role that can be used for application-specific purposes. 最后的`Qt.UserRole`是用戶自定義Role,如果多個Role,可在他的基礎上加數字,比如`Qt.UserRole+1`,`Qt.UserRole+2` >目前為止,我們使用自定義Model完成了一個最基本的demo,並且知道了Qt.ItemDataRole的各種類型 > >[下一篇](http://www.cnblogs.com/hangxin1940/archive/2012/12/07/2806449.html),將介紹Qt Model/View 中的委托 `Delegate`,通過它我們可以自由渲染view的顯示效果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM