PyQt 4.3 QTableView / QAbstractTableModel formatting example
This is my OLD blog. I've copied this post over to my NEW blog at:
http://www.saltycrane.com/blog/2007/12/pyqt-43-qtableview-qabstracttable-model/
You should be redirected in 2 seconds.
This example displays the output of the "dir" command using QTableView and QAbstractTableModel. It adds some more formatting to my previous example such as specifying the font, a header, column width, row height, etc.
import re import os import sys from PyQt4.QtCore import * from PyQt4.QtGui import * 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) # create table self.get_table_data() table = self.createTable() # layout layout = QVBoxLayout() layout.addWidget(table) self.setLayout(layout) def get_table_data(self): stdouterr = os.popen4("dir c:\\")[1].read() lines = stdouterr.splitlines() lines = lines[5:] lines = lines[:-2] self.tabledata = [re.split(r"\s+", line, 4) for line in lines] def createTable(self): # create the view tv = QTableView() # set the table model header = ['date', 'time', '', 'size', 'filename'] tm = MyTableModel(self.tabledata, header, self) tv.setModel(tm) # set the minimum size self.setMinimumSize(400, 300) # hide grid tv.setShowGrid(False) # set the font font = QFont("Courier New", 8) tv.setFont(font) # hide vertical header vh = tv.verticalHeader() vh.setVisible(False) # set horizontal header properties hh = tv.horizontalHeader() hh.setStretchLastSection(True) # set column width to fit contents tv.resizeColumnsToContents() # set row height nrows = len(self.tabledata) for row in xrange(nrows): tv.setRowHeight(row, 18) # enable sorting # this doesn't work #tv.setSortingEnabled(True) return tv class MyTableModel(QAbstractTableModel): def __init__(self, datain, headerdata, parent=None, *args): QAbstractTableModel.__init__(self, parent, *args) self.arraydata = datain self.headerdata = headerdata 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()]) def headerData(self, col, orientation, role): if orientation == Qt.Horizontal and role == Qt.DisplayRole: return QVariant(self.headerdata[col]) return QVariant() if __name__ == "__main__": main()
1 comment:
Thanks a lot Sofeng, it is very helpful.
Could you please show me how to put other widgets inside a treeview? e.g. checkboxes, combo boxes, etc.
Post a Comment