[PyQt] FetchMore Example
Darryl Wallace
darryl.wallace at prosensus.ca
Fri May 8 16:34:28 BST 2009
Hello Everyone,
Since I was interested in the FetchMore example (included in Qt4.5) and
extending it for myself, I just decided to port the FetchMore example to
PyQt4 since I noticed it was not included in the ItemViews examples in
the latest snapshot.
Feel free to use it as you wish.
Phil feel free to included it in your list of examples should it be
deemed acceptable. The only thing that I have done differently is that
I force the QStringList to be a Python list. I wrote it using PyQt4.4.2
in Linux (Ubuntu).
See the attached file.
Regards,
Darryl
-------------- next part --------------
# Fetch More Example
# Ported to PyQt4 by Darryl Wallace, 2009 - wallacdj at gmail.com
import sys
from PyQt4 import QtGui, QtCore
class FileListModel(QtCore.QAbstractListModel):
def __init__(self, parent=None):
QtCore.QAbstractListModel.__init__(self, parent)
self.fileCount=0
self.fileList=[] #initialize the file list as a Python list.
#__init__
def rowCount(self, parent):
"""
parent=QModelIndex
"""
return self.fileCount
#rowCount
def data(self, index, role=QtCore.Qt.DisplayRole):
"""
index=QModelIndex
"""
if not index.isValid():
return QtCore.QVariant()
if index.row()>=len(self.fileList) or index.row()<0:
return QtCore.QVariant()
if role==QtCore.Qt.DisplayRole:
return QtCore.QVariant(self.fileList[index.row()])
elif role==QtCore.Qt.BackgroundRole:
batch=(index.row()/100)%2
if batch==0:
return QtCore.QVariant(QtGui.qApp.palette().base())
else:
return QtCore.QVariant(QtGui.qApp.palette().alternateBase())
return QtCore.QVariant()
#data
def canFetchMore(self, index):
"""
index=QModelIndex
"""
if self.fileCount<len(self.fileList):
return True
else:
return False
#canFetchMore
def fetchMore(self, index):
"""
Index=QModelIndex
"""
remainder=len(self.fileList)-self.fileCount
itemsToFetch=min(100, remainder)
self.beginInsertRows(QtCore.QModelIndex(), self.fileCount, self.fileCount+itemsToFetch)
self.fileCount+=itemsToFetch
self.endInsertRows()
self.emit(QtCore.SIGNAL("numberPopulated"), itemsToFetch)
#fetchMore
def setDirPath(self, path):
dir=QtCore.QDir(path)
self.fileList=list(dir.entryList()) # force Python list.
self.fileCount=0
self.reset()
#setDirPath
#FileListModel
class Window(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.model = FileListModel(self)
self.model.setDirPath(QtCore.QLibraryInfo.location(QtCore.QLibraryInfo.PrefixPath))
label = QtGui.QLabel(self.tr("Directory"))
lineEdit=QtGui.QLineEdit()
label.setBuddy(lineEdit)
view=QtGui.QListView()
view.setModel(self.model)
self.logViewer=QtGui.QTextBrowser()
self.logViewer.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Preferred))
self.connect(lineEdit, QtCore.SIGNAL("textChanged(const QString &)"), self.model.setDirPath)
self.connect(lineEdit, QtCore.SIGNAL("textChanged(const QString &)"),
self.logViewer, QtCore.SLOT("clear()"))
self.connect(self.model, QtCore.SIGNAL("numberPopulated"), self.updateLog)
layout=QtGui.QGridLayout()
layout.addWidget(label, 0, 0)
layout.addWidget(lineEdit, 0, 1)
layout.addWidget(view, 1, 0, 1, 2)
layout.addWidget(self.logViewer, 2, 0, 1, 2)
self.setLayout(layout)
self.setWindowTitle(self.tr("Fetch More Example"))
#__init__
def updateLog(self, number):
self.logViewer.append(self.tr("%1 items added.").arg(number))
#updateLog
#Window
if __name__=='__main__':
qApp=QtGui.QApplication(sys.argv)
fetchMoreWindow=Window()
fetchMoreWindow.show()
sys.exit(qApp.exec_())
More information about the PyQt
mailing list