CPU overload when updating QListView from QAbstractListModel - python

I'm building a little pyqt5 app that display a list of 512 values in a QListView. The list is updated through separate thread, with QThread.
It's working nice, except the fact that it is using 65/95 % of the CPU of the (old) Core 2 Duo 2,53 Ghz I'm developing on.
I simplify the code to remove dependancies, because the update is done from a network protocol. Updates are done 40 times per second (each 25 ms).
The simplified script below is refreshing the list 10 times per second and the CPU is still at 65 % when list is updated.
Is there anything to do for avoiding the overload?
Is there some best practices to follow for updating a View?
(the global is not in my last code, it's here to have a simple example)
from random import randrange
from time import sleep
from sys import argv, exit
from PyQt5.QtCore import QThread, QAbstractListModel, Qt, QVariant, pyqtSignal
from PyQt5.QtWidgets import QListView, QApplication, QGroupBox, QVBoxLayout, QPushButton
universe_1 = [0 for i in range(512)]
class SpecialProcess(QThread):
universeChanged = pyqtSignal()
def __init__(self):
super(SpecialProcess, self).__init__()
self.start()
def run(self):
global universe_1
universe_1 = ([randrange(0, 101, 2) for i in range(512)])
self.universeChanged.emit()
sleep(0.1)
self.run()
class Universe(QAbstractListModel):
def __init__(self, parent=None):
super(Universe, self).__init__(parent)
def rowCount(self, index):
return len(universe_1)
def data(self, index, role=Qt.DisplayRole):
index = index.row()
if role == Qt.DisplayRole:
try:
return universe_1[index]
except IndexError:
return QVariant()
return QVariant()
class Viewer(QGroupBox):
def __init__(self):
super(Viewer, self).__init__()
list_view = QListView()
self.list_view = list_view
# create a vertical layout
vbox = QVBoxLayout()
universe = Universe()
vbox.addWidget(list_view)
# Model and View setup
self.model = Universe(self)
self.list_view.setModel(self.model)
# meke a process running in parallel
my_process = SpecialProcess()
my_process.universeChanged.connect(self.model.layoutChanged.emit)
# set the layout on the groupbox
vbox.addStretch(1)
self.setLayout(vbox)
if __name__ == "__main__":
app = QApplication(argv)
group_widget = Viewer()
group_widget.show()
exit(app.exec_())

It seems to be a normal behavior…

Related

QThreadPool causing freezes when loading QImage in Qt5, when perfectly smooth in Qt4

I made a widget for loading lots of images in a deferred fashion by using threads. When using Python 2 (with PySide), the scrolling is super smooth with all the threads running. On Python 3 (with PySide2), it freezes every time you attempt to scroll.
I narrowed it down to the QtGui.QImage call within the thread.
class ImageLoader(QtCore.QRunnable):
def __init__(self, item):
self.item = item
super(ImageLoader, self).__init__()
def run(self):
QtGui.QImage(self.item.path)
Does anyone know why it would only be causing issues on newer versions of Qt, and perhaps how I can fix the issue?
Here's the full script, trimmed down as much as I was able to:
PATH_TO_LARGE_FILE = 'C:/large_image.png' # Pick something >1MB to really show the slowness
import sys
from Qt import QtCore, QtGui, QtWidgets
class GridView(QtWidgets.QListView):
def __init__(self):
super(GridView, self).__init__()
self.setViewMode(QtWidgets.QListView.IconMode)
self.setModel(GridModel())
class GridModel(QtGui.QStandardItemModel):
def __init__(self):
super(GridModel, self).__init__()
self.threadPool = QtCore.QThreadPool()
def data(self, index, role=QtCore.Qt.UserRole):
# Load image
if role == QtCore.Qt.DecorationRole:
item = self.itemFromIndex(index)
worker = ImageLoader(item)
self.threadPool.start(worker)
return None
# Set size of icons
elif role == QtCore.Qt.SizeHintRole:
return QtCore.QSize(64, 89)
return super(GridModel, self).data(index, role)
class ImageLoader(QtCore.QRunnable):
def __init__(self, item):
self.item = item
super(ImageLoader, self).__init__()
def run(self):
QtGui.QImage(self.item.path)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
widget = GridView()
for i in range(1000):
item = QtGui.QStandardItem('test {}'.format(i))
item.path = PATH_TO_LARGE_FILE
widget.model().appendRow(item)
widget.show()
app.setActiveWindow(widget)
app.exec_()
Edit: Link to full script

How to use Threading with variables inside a function? PyQt5

I have a big function which freezes my PyQt5 program, I tried to use a different thread for it (I use QThread for that). Problem is my function needs some variables to work properly.
How to make this works? I show what I did.
Original code:
class AnalysisWindow(QtWidgets.QMainWindow):
def __init__(self, firstWindow):
super(AnalysisWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.letsgo)
def letsgo(self):
#here some code , not big
#and at some point i have a heavy one which make it freeze until it's over:
self.x1, self.x2, self.y1,self.y2, self.z, = self.analyze(self.i1, self.i2, self.i3)
def analyze(self,i1,i2,i3):
#big function
return(x1,x2,y1,y2,z)
what I tried :
from PyQt5.QtCore import Qt, QThread, pyqtSignal
class AnalysisWindow(QtWidgets.QMainWindow):
class MyThread(QThread):
_signal =pyqtSignal()
def __init__(self):
super().__init__()
def run(self,i1,i2,i3): # here I obviously can't put variables
#I copied here my analyze function
return(x1,x2,y1,y2,z)
self._signal.emit()
def __init__(self, firstWindow):
super(AnalysisWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.letsgo)
def letsgo(self):
self.thread = MyThread()
self.thread.start()
#here I dont see how to send the variables self.i1, self.i2, self.i3 and how to get the result: x1,x2,y1,y2,z
I created the thread class inside the QMainWindow class because i need to pass some variables (self.i1, self.i2, self.i3) from QMainWindow to the function which will use the new thread. Maybe that's bad, but it doesn't work in any way. Thanks everyone.
Here is a minimal working example that you can adapt it to your code.
Few things to note:
You should not inherit from QThread. Instead, you should create a worker and move it into your thread.
In worker, instead of trying to return a result, emit the signal that holds the result and process that signal in your application.
Similarly, instead of trying to call your worker normally, communicate it through its slots via QtCore.QMetaObject.invokeMethod. Once your thread is started, you can call this method as much as you want.
Refer to this answer for more
import sys
import random
from PyQt5.QtCore import QThread, pyqtSignal, QObject, pyqtSlot, Qt
from PyQt5 import QtWidgets
from PyQt5 import QtCore
class Analyzer(QObject):
analyze_completed = pyqtSignal(bool)
analyze_result = pyqtSignal(list, int)
#pyqtSlot(str, list)
def analyze(self, foo, analyze_args):
print(foo, analyze_args)
self.analyze_completed.emit(False)
# do your heavy calculations
for i in range(10000000):
x = i ** 0.5
result = sum(analyze_args)
self.analyze_result.emit(analyze_args, result)
self.analyze_completed.emit(True)
class AnalysisWindow(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.label = QtWidgets.QLabel("")
self.i = 0
self.label_i = QtWidgets.QLabel("Value of i: {}".format(self.i))
self.increment_button = QtWidgets.QPushButton("increment i")
self.pushbutton = QtWidgets.QPushButton("Analyze")
super(AnalysisWindow, self).__init__()
self.analyze_args = []
self.analyzer = Analyzer()
self.thread = QThread()
self.analyzer.analyze_result.connect(self.on_analyze_result_ready)
self.analyzer.analyze_completed.connect(self.on_analyze_completed)
self.analyzer.moveToThread(self.thread)
self.thread.start()
self.init_UI()
def init_UI(self):
grid = QtWidgets.QGridLayout()
grid.addWidget(self.label, 0, 0)
grid.addWidget(self.pushbutton)
grid.addWidget(self.label_i)
grid.addWidget(self.increment_button)
self.increment_button.clicked.connect(self.increment_i)
self.pushbutton.clicked.connect(self.start_analyze)
self.setLayout(grid)
self.move(300, 150)
self.setMinimumSize(300, 100)
self.setWindowTitle('Thread Test')
self.show()
def start_analyze(self):
self.analyze_args.clear()
self.analyze_args.extend(random.choices(range(100), k=5))
QtCore.QMetaObject.invokeMethod(self.analyzer, 'analyze', Qt.QueuedConnection,
QtCore.Q_ARG(str, "Hello World!"),
QtCore.Q_ARG(list, self.analyze_args))
def increment_i(self):
self.i += 1
self.label_i.setText("Value of i: {}".format(self.i))
def on_analyze_result_ready(self, args, result):
t = "+".join(str(i) for i in args)
self.label.setText(f"{t} = {result}")
def on_analyze_completed(self, completed):
if completed:
self.label.setStyleSheet('color: blue')
else:
self.label.setText(
"Analyzing... {}".format(", ".join(str(i) for i in self.analyze_args)))
self.label.setStyleSheet('color: yellow')
app = QtWidgets.QApplication(sys.argv)
widget = AnalysisWindow()
sys.exit(app.exec_())
Hope this helps!

Why does my QTableWidgetItems's rowHeight not get properly set on refresh?

I'm creating a table widget that I want to auto-refresh at certain intervals. The trouble I'm having is that refreshing the contents of the table is resetting their rowHeight property and ignoring the call to setRowHeight().
For example, I have a repeater class here running on a separate thread:
class RepeatedTimer(QtCore.QThread):
def __init__(self, obj):
super(RepeatedTimer, self).__init__(obj)
self.obj = obj
self.stop = False
def run(self):
while not self.stop:
time.sleep(2)
self.obj.refresh()
and it's being used in my QTableWidget like this:
from PySide import QtCore, QtGui
import sys, time
class TestTable(QtGui.QTableWidget):
def __init__(self, parent=None):
super(TestTable, self).__init__(parent)
self.setColumnCount(1)
self.thread = RepeatedTimer(self) # Create the auto-refresher thread
self.thread.start() # Start the thread
self.refresh()
def refresh(self):
print "Clearing table"
while self.rowCount():
self.removeRow(0)
for each in xrange(3):
self.insertRow(each)
text = str(time.time())
item = QtGui.QTableWidgetItem(text)
self.setItem(each, 0, item)
for row in xrange(self.rowCount()):
self.setRowHeight(row, 100) # This part is not behaving as expected
print 'Row %d height: %d' % (row, self.rowHeight(row))
def closeEvent(self, event):
print 'Stopping thread...'
self.thread.stop = True
self.thread.exit()
app = QtGui.QApplication(sys.argv)
test = TestTable()
test.show()
sys.exit(app.exec_())
If you run this, you'll see that each time the table refreshes, it clears all the contents, adds new items in each row, and sets all the row heights to 100. Except that last part. It is correctly looping through the rows because it prints each time. But for some reason it stops setting the row heights after the first loop.
Any ideas why this is happening?
It is not necessary to create a thread to update the QTableWidget, you could use a QTimer, on the other hand remove QTableWidgetItem and set them again is expensive so I recommend you update them.
import sys
import time
from PySide import QtCore, QtGui
class TestTable(QtGui.QTableWidget):
def __init__(self, parent=None):
super(TestTable, self).__init__(parent)
self.setRowCount(3)
self.setColumnCount(1)
for row in range(self.rowCount()):
for col in range(self.columnCount()):
self.setItem(row, col, QtGui.QTableWidgetItem(str(time.time())))
for each in range(self.rowCount()):
self.setRowHeight(each, 100)
self.setColumnWidth(each, 250)
timer_refresh = QtCore.QTimer(self)
timer_refresh.timeout.connect(self.refresh)
timer_refresh.start(2000)
def refresh(self):
for row in range(self.rowCount()):
for col in range(self.columnCount()):
self.item(row, col).setText(str(time.time()))
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
test = TestTable()
test.show()
sys.exit(app.exec_())
Sorry, but you do not need a separate thread for this task.
Use the QTimer class provides repetitive and single-shot timers.
Here is an example, sorry for me PyQt5
import sys
import time
#from PySide import QtCore, QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class TestTable(QTableWidget):
def __init__(self, parent=None):
super(TestTable, self).__init__(parent)
self.setWindowTitle("QTableWidget setRowHeight ")
self.resize(530, 330);
self.setRowCount(3)
self.setColumnCount(1)
self.setHorizontalHeaderLabels(['time.time())',])
for each in range(3):
self.setRowHeight(each, 100)
self.setColumnWidth(each, 250)
self.my_qtimer = QTimer(self)
self.my_qtimer.timeout.connect(self.timerTick)
self.my_qtimer.start(1000) # msec
def timerTick(self):
for each in range(3):
item = QTableWidgetItem(str(time.time()))
self.setItem(0, each, item)
app = QApplication(sys.argv)
test = TestTable()
test.show()
sys.exit(app.exec_())

QGraphicesItem emit a signal upon hoverEnterEvent

What is the best method/practice for emitting a signal upon entering either a QGraphicsWidget or a QGraphicsItem ?
In my MWE I would like to trigger a call to MainWindow.update, from Square.hoverEnterEvent, whenever the user mouse(s) over an item in a QGraphicsScene. The trouble is that QGraphicsItem/Widget is not really designed to emit signals. Instead these classes are setup to handle events passed down to them from QGraphicsScene. QGraphicsScene handles the case that the user has selected an item but does not appear to handle mouse entry events, At least there is no mechanism for entryEvent to percolate up to the parent widget/window.
import sys
from PyQt5.QtWidgets import QWidget, QApplication, qApp, QMainWindow, QGraphicsScene, QGraphicsView, QStatusBar, QGraphicsWidget, QStyle
from PyQt5.QtCore import Qt, QSizeF
class Square(QGraphicsWidget) :
"""
doc string
"""
def __init__(self,*args, name = None, **kvps) :
super().__init__(*args, **kvps)
self.radius = 5
self.name = name
self.setAcceptHoverEvents(True)
def sizeHint(self, hint, size):
size = super().sizeHint(hint, size)
print(size)
return QSizeF(50,50)
def paint(self, painter, options, widget):
self.initStyleOption(options)
ink = options.palette.highLight() if options.state == QStyle.State_Selected else options.palette.button()
painter.setBrush(ink) # ink
painter.drawRoundedRect(self.rect(), self.radius, self.radius)
def hoverEnterEvent(self, event) :
print("Enter Event")
super().hoverEnterEvent(event)
class MainWindow(QMainWindow):
def __init__(self, *args, **kvps) :
super().__init__(*args, **kvps)
# Status bar
self.stat = QStatusBar(self)
self.setStatusBar(self.stat)
self.stat.showMessage("Started")
# Widget(s)
self.data = QGraphicsScene(self)
self.view = QGraphicsView(self.data, self)
item = self.data.addItem(Square())
self.view.ensureVisible(self.data.sceneRect())
self.setCentralWidget(self.view)
# Visibility
self.showMaximized()
def update(self, widget) :
self.stat.showMessage(str(widget.name))
if __name__ == "__main__" :
# Application
app = QApplication(sys.argv)
# Scene Tests
main = MainWindow()
main.show()
# Loop
sys.exit(app.exec_())
The docs state that QGraphicsItem is not designed to emit signals, instead it is meant to respond to the events sent to it by QGraphicsScene. In contrast it seems that QGraphicsWidget is designed to do so but I'm not entirely sure where the entry point ought to be. Personally I feel QGraphicsScene should really be emitting these signals, from what I understand of the design, but am not sure where the entry point ought to be in this case either.
Currently I see the following possible solutions, with #3 being the preferred method. I was wondering if anyone else had a better strategy :
Create a QGraphicsScene subclass, let's call it Scene, to each QGraphicsItem/QGraphicsWidget and call a custom trigger/signal upon the Scene from each widget. Here I would have to subclass any item I intend on including within the scene.
Set Mainwindow up as the event filter for each item in the scene or upon the scene itself and calling MainWindow.update.
Set Mainwindow.data to be a subclass of QGraphicsScene, let's call it Scene, and let it filter it's own events emitting a hoverEntry signal. hoverEntry is then connected to MainWindow.update as necessary.
As Murphy's Law would have it Ekhumoro already provides an answer.
It seems one should subclass QGraphicsScene and add the necessary signal. this is then triggered from the QGraphicsItem/Widget. This requires that all items within a scene be sub-classed to ensure that they call the corresponding emit function but it seems must do this do this anyhow when working with the graphics scene stuff.
I'll not mark this as answered for a bit in case some one has a better suggestion.
import sys
from PyQt5.QtWidgets import QWidget, QApplication, qApp, QMainWindow, QGraphicsScene, QGraphicsView, QStatusBar, QGraphicsWidget, QStyle, QGraphicsItem
from PyQt5.QtCore import Qt, QSizeF, pyqtSignal
class Square(QGraphicsWidget) :
"""
doc string
"""
def __init__(self,*args, name = None, **kvps) :
super().__init__(*args, **kvps)
self.radius = 5
self.name = name
self.setAcceptHoverEvents(True)
self.setFlag(self.ItemIsSelectable)
self.setFlag(self.ItemIsFocusable)
def sizeHint(self, hint, size):
size = super().sizeHint(hint, size)
print(size)
return QSizeF(50,50)
def paint(self, painter, options, widget):
self.initStyleOption(options)
ink = options.palette.highLight() if options.state == QStyle.State_Selected else options.palette.button()
painter.setBrush(ink) # ink
painter.drawRoundedRect(self.rect(), self.radius, self.radius)
def hoverEnterEvent(self, event) :
super().hoverEnterEvent(event)
self.scene().entered.emit(self)
self.update()
class GraphicsScene(QGraphicsScene) :
entered = pyqtSignal([QGraphicsItem],[QGraphicsWidget])
class MainWindow(QMainWindow):
def __init__(self, *args, **kvps) :
super().__init__(*args, **kvps)
# Status bar
self.stat = QStatusBar(self)
self.setStatusBar(self.stat)
self.stat.showMessage("Started")
# Widget(s)
self.data = GraphicsScene(self)
self.data.entered.connect(self.itemInfo)
self.data.focusItemChanged.connect(self.update)
self.view = QGraphicsView(self.data, self)
item = Square(name = "A")
item.setPos( 50,0)
self.data.addItem(item)
item = Square(name = "B")
item.setPos(-50,0)
self.data.addItem(item)
self.view.ensureVisible(self.data.sceneRect())
self.setCentralWidget(self.view)
# Visibility
self.showMaximized()
def itemInfo(self, item):
print("Here it is -> ", item)
if __name__ == "__main__" :
# Application
app = QApplication(sys.argv)
# Scene Tests
main = MainWindow()
main.show()
# Loop
sys.exit(app.exec_())
The magic lines of interest are then the QGrahicsScene subclass.
class GraphicsScene(QGraphicsScene) :
entered = pyqtSignal([QGraphicsItem],[QGraphicsWidget])
The QGraphicsWidget.hoverEnterEvent triggers the entered signal. (This is where I got stuck)
def hoverEnterEvent(self, event) :
...
self.scene().entered.emit(self)
...
And the switcheroo from self.data = QGraphicsScene(...) to self.data = GraphicsScene in the MainWindow's init function.

PyQt: signal emitted twice when calculations are too long

I use two widgets: a QSpinBox and a QLineEdit. valueChanged slot of the QSpinBox widget is connected to the update function. This function consist of a time-consuming processing (a loop with calculations or a time.sleep() call) and a QLineEdit.setText() call. At the beginning, i thought it worked as expected but I noticed that the signal seems to be emitted twice when the calculations takes a long time.
Bellow is the code:
import time
from PyQt5.QtWidgets import QWidget, QSpinBox, QVBoxLayout, QLineEdit
class Window(QWidget):
def __init__(self):
# parent constructor
super().__init__()
# widgets
self.spin_box = QSpinBox()
self.line_edit = QLineEdit()
# layout
v_layout = QVBoxLayout()
v_layout.addWidget(self.spin_box)
v_layout.addWidget(self.line_edit)
# signals-slot connections
self.spin_box.valueChanged.connect(self.update)
#
self.setLayout(v_layout)
self.show()
def update(self, param_value):
print('update')
# time-consuming part
time.sleep(0.5) # -> double increment
#time.sleep(0.4) # -> works normally!
self.line_edit.setText(str(param_value))
if __name__ == '__main__':
from PyQt5.QtWidgets import QApplication
import sys
app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())
Another version of update:
# alternative version, calculations in a loop instead of time.sleep()
# -> same behaviour
def update2(self, param_value):
print('update2')
for i in range(2000000): # -> double increment
x = i**0.5 * i**0.2
#for i in range(200000): # -> works normally!
# x = i**0.5 * i**0.2
self.line_edit.setText(str(param_value))
There is no real mystery here. If you click a spin-box button, the value will increase by a single step. But if you press and hold down the button, it will increase the values continually. In order to tell the difference between a click and a press/hold, a timer is used. Presumably, the threshold is around half a second. So if you insert a small additional delay, a click may be interpreted as a short press/hold, and so the spin-box will increment by two steps instead of one.
UPDATE:
One way to work around this behaviour is by doing the processing in a worker thread, so that the delay is eliminated. The main problem with this is avoiding too much lag between spin-box value changes and line-edit updates. If you press and hold the spin-box button, a large number of signal events could be queued by the worker thread. A simplistic approach would wait until the spin-box button was released before handling all those queued signals - but that would result in a long delay whilst each value was processed separately. A better approach is to compress the events, so that only the most recent signal is handled. This will still be somewhat laggy, but if the processing time is not too long, it should result in acceptable behaviour.
Here is a demo that implements this approach:
import sys, time
from PyQt5.QtWidgets import (
QApplication, QWidget, QSpinBox, QVBoxLayout, QLineEdit,
)
from PyQt5.QtCore import (
pyqtSignal, pyqtSlot, Qt, QObject, QThread, QMetaObject,
)
class Worker(QObject):
valueUpdated = pyqtSignal(int)
def __init__(self, func):
super().__init__()
self._value = None
self._invoked = False
self._func = func
#pyqtSlot(int)
def handleValueChanged(self, value):
self._value = value
if not self._invoked:
self._invoked = True
QMetaObject.invokeMethod(self, '_process', Qt.QueuedConnection)
print('invoked')
else:
print('received:', value)
#pyqtSlot()
def _process(self):
self._invoked = False
self.valueUpdated.emit(self._func(self._value))
class Window(QWidget):
def __init__(self):
super().__init__()
self.spin_box = QSpinBox()
self.line_edit = QLineEdit()
v_layout = QVBoxLayout()
v_layout.addWidget(self.spin_box)
v_layout.addWidget(self.line_edit)
self.setLayout(v_layout)
self.thread = QThread(self)
self.worker = Worker(self.process)
self.worker.moveToThread(self.thread)
self.worker.valueUpdated.connect(self.update)
self.spin_box.valueChanged.connect(self.worker.handleValueChanged)
self.thread.start()
self.show()
def process(self, value):
time.sleep(0.5)
return value
def update(self, param_value):
self.line_edit.setText(str(param_value))
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())

Categories