[PyQt] Memory leak when using QAbstractTableModel

Noam Raphael noamraph at gmail.com
Mon Dec 24 21:40:48 GMT 2007


Hurray! Thanks to Justin Noel on the qt-interest list, the following
code now works and doesn't leak!

Noam

===============
#!/usr/bin/env python

import sys
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import Qt

class TableModel(QtCore.QAbstractTableModel):
    def rowCount(self, index):
        return 1000 if not index.isValid() else 0

    def columnCount(self, index):
        return 1000 if not index.isValid() else 0

    def data(self, index, role):
        if index.isValid() and role == Qt.DisplayRole:
            return QtCore.QVariant(str((index.row(), index.column())))
        else:
            return QtCore.QVariant()

    def headerData(self, col, orientation, role):
        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
            return QtCore.QVariant(str(col))
        if orientation == Qt.Vertical and role == Qt.DisplayRole:
            return QtCore.QVariant(str(col))
        return QtCore.QVariant()


def main():
    app = QtGui.QApplication(sys.argv)

    model = TableModel()
    tableView = QtGui.QTableView()
    tableView.setModel(model)

    tableView.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()


More information about the PyQt mailing list