Reputation: 13
My PyQt app is slow. What have i do to speedup this def:
def paint(self):
for i in range(self.tableWidget.columnCount()):
c = []
for j in range(self.tableWidget.rowCount()):
c.append(self.tableWidget.item(j, i).text())
if "X" not in c:
for j in range(self.tableWidget.rowCount()):
self.tableWidget.item(j, i).setBackground(
QtGui.QColor(125, 125, 125))
else:
for j in range(self.tableWidget.rowCount()):
self.tableWidget.item(j, i).setBackground(
QtGui.QColor(255, 255, 251))
Upvotes: 0
Views: 241
Reputation: 243937
One possible solution is to implement that logic through a delegate:
class Delegate(QtWidgets.QStyledItemDelegate):
def initStyleOption(self, option, index):
super().initStyleOption(option, index)
column = index.column()
model = index.model()
found = False
for row in range(model.columnCount()):
if model.index(row, column).data() == "X":
found = True
break
option.backgroundBrush = (
QtGui.QColor(255, 255, 251) if found else QtGui.QColor(125, 125, 125)
)
delegate = Delegate(self.tableWidget)
self.tableWidget.setItemDelegate(delegate)
Upvotes: 2