Last active
February 17, 2016 07:51
-
-
Save oglops/c6605f25421a06621fd9 to your computer and use it in GitHub Desktop.
QAbstractListModel example 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
from PyQt4.QtGui import * | |
from PyQt4.QtCore import * | |
class MyModel(QAbstractListModel): | |
def __init__(self, parent=None): | |
QAbstractListModel.__init__(self, parent) | |
self.__data = ['row1', 'row2', 'row3'] | |
def rowCount(self, parent=QModelIndex()): | |
return len(self.__data) | |
def data(self, index, role): | |
if not index.isValid(): | |
return QVariant() | |
if index.row() >= len(self.__data): | |
return QVariant() | |
if role == Qt.DisplayRole: | |
return QVariant(self.__data[index.row()]) | |
else: | |
return QVariant() | |
class MyWidget(QMainWindow): | |
def __init__(self, parent=None): | |
QWidget.__init__(self, parent) | |
self.listView = QListView(self) | |
model = MyModel(self) | |
self.listView.setModel(model) | |
self.setCentralWidget(self.listView) | |
if __name__ == "__main__": | |
app = QApplication(sys.argv) | |
widget = MyWidget() | |
widget.show() | |
sys.exit(app.exec_()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from sip import setapi | |
setapi("QVariant", 2) | |
import sys | |
from PyQt4.QtGui import * | |
from PyQt4.QtCore import * | |
class MyModel(QAbstractListModel): | |
def __init__(self, parent=None): | |
QAbstractListModel.__init__(self, parent) | |
self.__data = ['row1', 'row2', 'row3'] | |
def rowCount(self, parent=QModelIndex()): | |
return len(self.__data) | |
def data(self, index, role): | |
if not index.isValid(): | |
return None | |
if index.row() >= len(self.__data): | |
return None | |
if role == Qt.DisplayRole: | |
return self.__data[index.row()] | |
else: | |
return None | |
class MyWidget(QMainWindow): | |
def __init__(self, parent=None): | |
QWidget.__init__(self, parent) | |
self.listView = QListView(self) | |
model = MyModel(self) | |
self.listView.setModel(model) | |
self.setCentralWidget(self.listView) | |
if __name__ == "__main__": | |
app = QApplication(sys.argv) | |
widget = MyWidget() | |
widget.show() | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment