Created
February 15, 2016 02:10
-
-
Save oglops/5bc295ca260ac9c0f033 to your computer and use it in GitHub Desktop.
QTableView + QAbstractTableModel
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
from PyQt4.QtCore import * | |
from PyQt4.QtGui import * | |
import sys | |
my_array = [['00', '01', '02'], | |
['10', '11', '12'], | |
['20', '21', '22']] | |
def main(): | |
app = QApplication(sys.argv) | |
w = MyWindow() | |
w.show() | |
sys.exit(app.exec_()) | |
class MyWindow(QWidget): | |
def __init__(self, *args): | |
QWidget.__init__(self, *args) | |
tablemodel = MyTableModel(my_array, self) | |
tableview = QTableView() | |
tableview.setModel(tablemodel) | |
layout = QVBoxLayout(self) | |
layout.addWidget(tableview) | |
self.setLayout(layout) | |
self.resize(350,150) | |
class MyTableModel(QAbstractTableModel): | |
def __init__(self, datain, parent=None, *args): | |
QAbstractTableModel.__init__(self, parent, *args) | |
self.arraydata = datain | |
def rowCount(self, parent): | |
return len(self.arraydata) | |
def columnCount(self, parent): | |
return len(self.arraydata[0]) | |
def data(self, index, role): | |
if not index.isValid(): | |
return QVariant() | |
elif role != Qt.DisplayRole: | |
return QVariant() | |
return QVariant(self.arraydata[index.row()][index.column()]) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment