pyqt model/view框架 1.第一個model
>關(guān)于Qt的mvc模式就不多說了,網(wǎng)上很多很多,這里按著 Chen Chun-Chia的`《PyQt's Model/View Framework》`走一邊
我的第一個model
---
class MyListModel(QAbstractListModel):
"""
我的第一個模型
"""
def __init__(self,parent=None):
super(MyListModel,self).__init__(parent)
#這是數(shù)據(jù)
self._data=[70,90,20,50]
pass
def rowCount(self, parent=QModelIndex()):
"""
這個方法返回了數(shù)據(jù)的行數(shù)
也就是有多少個條目得數(shù)據(jù)
"""
return len(self._data)
def data(self,index,role=Qt.DisplayRole):
"""
根據(jù)當(dāng)前index索引,返回當(dāng)前的數(shù)據(jù)
然后再由Qt進(jìn)行渲染顯示
"""
#如果當(dāng)前得索引是不活動得
if not index.isValid() or not 0 <= index.row() < self.rowCount():
#亦或者當(dāng)前的索引值不在合理范圍,即小于等于0,超出總行數(shù)
return QVariant() #返回一個QVariant,相當(dāng)與空條目
#從索引取得當(dāng)前的航號
row=index.row()
#如果當(dāng)前角色是DisplayRole
if role==Qt.DisplayRole:
#返回當(dāng)前行的數(shù)據(jù)
return self._data[row]
#如果角色不滿足需求,則返回QVariant
return QVariant()
代碼中,我們覆蓋了一個model中最基本得方法:
`rowCount` 數(shù)據(jù)總行數(shù);
`data` 獲取數(shù)據(jù);
并在__init__構(gòu)造方法中,設(shè)置好數(shù)據(jù)源`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()
#設(shè)置view的model
view.setModel(model)
view.show()
sys.exit(app.exec_())
####################################################################
class MyListModel(QAbstractListModel):
"""
我的第一個模型
"""
def __init__(self,parent=None):
super(MyListModel,self).__init__(parent)
#這是數(shù)據(jù)
self._data=[70,90,20,50]
pass
def rowCount(self, parent=QModelIndex()):
"""
這個方法返回了數(shù)據(jù)的行數(shù)
也就是有多少個條目得數(shù)據(jù)
"""
return len(self._data)
def data(self,index,role=Qt.DisplayRole):
"""
根據(jù)當(dāng)前index索引,返回當(dāng)前的數(shù)據(jù)
然后再由Qt進(jìn)行渲染顯示
"""
#如果當(dāng)前得索引是不活動得
if not index.isValid() or not 0 <= index.row() < self.rowCount():
#亦或者當(dāng)前的索引值不在合理范圍,即小于等于0,超出總行數(shù)
return QVariant() #返回一個QVariant,相當(dāng)與空條目
#從索引取得當(dāng)前的航號
row=index.row()
#如果當(dāng)前角色是DisplayRole
if role==Qt.DisplayRole:
#返回當(dāng)前行的數(shù)據(jù)
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,可在他的基礎(chǔ)上加數(shù)字,比如`Qt.UserRole+1`,`Qt.UserRole+2`
>目前為止,我們使用自定義Model完成了一個最基本的demo,并且知道了Qt.ItemDataRole的各種類型
>
>[下一篇](http://www.rzrgm.cn/hangxin1940/archive/2012/12/07/2806449.html),將介紹Qt Model/View 中的委托 `Delegate`,通過它我們可以自由渲染view的顯示效果
浙公網(wǎng)安備 33010602011771號