<div dir="ltr">Hello.<div>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?</div><div><br></div><div><div>#!/usr/bin/python3</div><div>import sys</div><div>from PyQt5.QtCore import *</div><div>from PyQt5.QtGui import *</div><div>from PyQt5.QtWidgets import *</div><div><br></div><div>my_array = [[i for _ in range(15)] for i in range(30)]</div><div><br></div><div>app = None</div><div><br></div><div>def main():</div><div> global app</div><div> app = QApplication(sys.argv)</div><div> w = MyWindow()</div><div> w.show()</div><div> sys.exit(app.exec_())</div><div><br></div><div>class MyWindow(QWidget):</div><div> def __init__(self, *args):</div><div> QWidget.__init__(self, *args)</div><div><br></div><div> tablemodel = MyTableModel(my_array, self)</div><div> tableview = QTableView()</div><div> tableview.setModel(tablemodel)</div><div><br></div><div> self.cdelegates = []</div><div> for i in range(15):</div><div> d = RSpinDelegate1(self)</div><div> self.cdelegates.append(d)</div><div> tableview.setItemDelegateForColumn(i, d)</div><div><br></div><div> layout = QVBoxLayout(self)</div><div> layout.addWidget(tableview)</div><div> self.setLayout(layout)</div><div><br></div><div>class RSpinDelegate1(QItemDelegate):</div><div> def __init__(self, parent=None):</div><div> super(RSpinDelegate1, self).__init__(parent)</div><div><br></div><div> def getBrush(self, option):</div><div> brush = option.palette.base()</div><div> if option.state & QStyle.State_Selected:</div><div> if option.state & QStyle.State_Active:</div><div> brush = option.palette.highlight()</div><div> else:</div><div> brush = option.palette.light()</div><div> return brush</div><div><br></div><div> def updateEditorGeometry(self, editor, option, index):</div><div> editor.setGeometry(option.rect)</div><div><br></div><div> def paint(self, painter, option, index):</div><div> opt = QStyleOptionViewItem(option)</div><div> brush = self.getBrush(option) # comment this line to remove memory leak</div><div> painter.fillRect(option.rect, brush) # comment this line to remove memory leak</div><div> super(RSpinDelegate1, self).paint(painter, opt, index)</div><div><br></div><div>class MyTableModel(QAbstractTableModel):</div><div> refreshTable = pyqtSignal()</div><div><br></div><div> def __init__(self, datain, parent = None, *args):</div><div> QAbstractTableModel.__init__(self, parent, *args)</div><div> self.arraydata = datain</div><div> self.timer = self.startTimer(300)</div><div><br></div><div> def timerEvent(self, e):</div><div> if self.timer == e.timerId():</div><div> self.refreshTable.emit()</div><div> else:</div><div> super(MyTableModel, self).timerEvent(e)</div><div><br></div><div> def refreshTableSlot(self):</div><div> self.layoutAboutToBeChanged.emit()</div><div> self.layoutChanged.emit()</div><div><br></div><div> def rowCount(self, parent):</div><div> return len(self.arraydata)</div><div><br></div><div> def columnCount(self, parent):</div><div> return len(self.arraydata[0])</div><div><br></div><div> def data(self, index, role):</div><div> if not index.isValid():</div><div> return None</div><div> elif role != Qt.DisplayRole:</div><div> return None</div><div> return (self.arraydata[index.row()][index.column()])</div><div><br></div><div>if __name__ == "__main__":</div><div> main()</div><div><br></div>-- <br><div><div dir="ltr"><div>Faithfully yours, Roman I. Liverovskiy</div></div></div>
</div></div>