• 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的显示效果
  • 相关阅读:
    poj 2029 Get Many Persimmon Trees 夜
    poj 1191 棋盘分割 夜
    DOM (四)
    div拖拽, onmousedown ,onmousemove, onmouseup
    闭包理解
    DOM(一)
    内存溢出与内存泄漏
    div随鼠标移动而移动(滚动条)
    对象继承模式
    DOM(二)
  • 原文地址:https://www.cnblogs.com/hangxin1940/p/2806444.html
Copyright © 2020-2023  润新知