QPropertyAnimation use so much system resource when using to create a simple Indeterminate Progressbar
-
hi, Im creating a simple progress bar for my program, I also need it to support an Indeterminate mode, so what I did for it is using
QPropertyAnimationto create a offset in infinite loop, and then draw the progress bar based of that
here is a code snippet from what I haveclass QProgressDelegate(QObject): def __init__(self, _parent): super().__init__() self.m_progress = _parent self.m_offset = 0 def get_offset(self): return self.m_offset def set_offset(self, value:float): self.m_offset = value self.m_progress.update() offset = Property(float, get_offset, set_offset) # and then use it like this self.delegate = QProgressDelegate(self) self.animation = QPropertyAnimation(self) self.animation.setPropertyName(b"offset") self.animation.setTargetObject(self.delegate) self.animation.setStartValue(0) self.animation.setEndValue(1) self.animation.setDuration(1000) self.animation.setLoopCount(-1) # in paint event ... painter.drawRect(self.delegate.offset*self.width()*2-self.width(), 0, self.width(), self.height()) ...but the problem is, its use like 10% of cpu when running, and I think its too much... so Im thinking maybe I'm doing it wrong? if yes can anyone point me to a better way?
-
Hi,
Why not just use the standard QProgressBar procedure for "infinite bar" ?
As the doc says, set both minimum and maximum to 0 and voila, you have a busy indicator.
-
Hi,
Why not just use the standard QProgressBar procedure for "infinite bar" ?
As the doc says, set both minimum and maximum to 0 and voila, you have a busy indicator.
-
@saeid0034 said in QPropertyAnimation use so much system resource when using to create a simple Indeterminate Progressbar:
but the problem is, its use like 10% of cpu when running, and I think its too much... so Im thinking maybe I'm doing it wrong?
In general, I expect QPropertyAnimation to be used for short animations. It is likely that it will just run whenever the event queue is idle to make animations as smooth as possible. If you don't have anything else running, it might use up a whole core (is 10% of a single core or all cores together?).
If this is actually the problem then I would suggest to use a QTimer with 10ms or 50ms resolution and drop the QPropertyAnimation altogether. (Remember 60Hz is about 16ms and even 30Hz is probably smooth enough.)
-
@saeid0034 said in QPropertyAnimation use so much system resource when using to create a simple Indeterminate Progressbar:
but the problem is, its use like 10% of cpu when running, and I think its too much... so Im thinking maybe I'm doing it wrong?
In general, I expect QPropertyAnimation to be used for short animations. It is likely that it will just run whenever the event queue is idle to make animations as smooth as possible. If you don't have anything else running, it might use up a whole core (is 10% of a single core or all cores together?).
If this is actually the problem then I would suggest to use a QTimer with 10ms or 50ms resolution and drop the QPropertyAnimation altogether. (Remember 60Hz is about 16ms and even 30Hz is probably smooth enough.)
@SimonSchroeder thank you for reply
I tried to create a custom progress bar with stylesheet, but result was not what I after...
aboutQTimer, tbh I dont know how can I use it instead ofQPropertyAnimation...
I mean can I used like how I useQPropertyAnimation? I mean use it to set the QProperty? -
@saeid0034 said in QPropertyAnimation use so much system resource when using to create a simple Indeterminate Progressbar:
I mean can I used like how I use QPropertyAnimation? I mean use it to set the QProperty?
Yes, that is exactly right that when the QTimer times out you compute the offset based on the elapsed time and then set the QProperty accordingly.
