[PyQt] Strange memory leak on QPainter fillRect

Roman Liverovskiy r.liverovskiy at gmail.com
Tue Sep 13 15:37:23 BST 2016


Hello.
I have a strange memory leak on QPainter's fillRect. Here is example code,
when I uncomment line with fillRect there are memory leaks, but when I
remove line with fillRect, there are no memory leaks. Why it can be so?

#!/usr/bin/python3
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

my_array = [[i for _ in range(15)] for i in range(30)]

app = None

def main():
    global app
    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)

        self.cdelegates = []
        for i in range(15):
            d = RSpinDelegate1(self)
            self.cdelegates.append(d)
            tableview.setItemDelegateForColumn(i, d)

        layout = QVBoxLayout(self)
        layout.addWidget(tableview)
        self.setLayout(layout)

class RSpinDelegate1(QItemDelegate):
    def __init__(self, parent=None):
        super(RSpinDelegate1, self).__init__(parent)

    def getBrush(self, option):
        brush = option.palette.base()
        if option.state & QStyle.State_Selected:
            if option.state & QStyle.State_Active:
                brush = option.palette.highlight()
            else:
                brush = option.palette.light()
        return brush

    def updateEditorGeometry(self, editor, option, index):
        editor.setGeometry(option.rect)

    def paint(self, painter, option, index):
        opt = QStyleOptionViewItem(option)
        brush = self.getBrush(option) # comment this line to remove memory
leak
        painter.fillRect(option.rect, brush) # comment this line to remove
memory leak
        super(RSpinDelegate1, self).paint(painter, opt, index)

class MyTableModel(QAbstractTableModel):
    refreshTable = pyqtSignal()

    def __init__(self, datain, parent = None, *args):
        QAbstractTableModel.__init__(self, parent, *args)
        self.arraydata = datain
        self.timer = self.startTimer(300)

    def timerEvent(self, e):
        if self.timer == e.timerId():
            self.refreshTable.emit()
        else:
            super(MyTableModel, self).timerEvent(e)

    def refreshTableSlot(self):
        self.layoutAboutToBeChanged.emit()
        self.layoutChanged.emit()

    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 None
        elif role != Qt.DisplayRole:
            return None
        return (self.arraydata[index.row()][index.column()])

if __name__ == "__main__":
    main()

-- 
Faithfully yours, Roman I. Liverovskiy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160913/9fc201fc/attachment.html>


More information about the PyQt mailing list