Home > other >  Pyqt5 and what is the difference between pyside2 QWaitconditon&QMutext usage?
Pyqt5 and what is the difference between pyside2 QWaitconditon&QMutext usage?

Time:03-23

Want to realize thread blocking and awaken, use PySide2 code is as follows, thread to sleep, why the main interface also fall into a state of no response, the same writing, in PyQt5, can perform normal sleep and wake up, the PySide and PyQt QWaitconditon& What is the difference between QMutext usage? How to change to achieve the effect of the graph one? Please bosses help me
Graph one: the desired effect (PyQt5 effect) :

Figure 2: PySide2 code execution results:

` ` ` python
The from PySide2. QtCore import QThread QWaitCondition, QMutex, Signal
The from PySide2. QtWidgets import QWidget, QVBoxLayout QPushButton, QProgressBar


The class Thread (QThread) :

ValueChange=Signal (int)

Def __init__ (self, * args, * * kwargs) :
The self, super (Thread) __init__ (* args, * * kwargs)
Self. _isPause=False
Self. _value=https://bbs.csdn.net/topics/0
Self. Cond=QWaitCondition ()
The self. The mutex=QMutex ()

Def pause (self) :
Print (" thread to sleep ")
Self. _isPause=True

Def resume (self) :
Print (" thread startup ")
Self. _isPause=False
Self. Cond. WakeAll ()

Def run (self) :
While 1:
The self. The mutex. Lock ()
If self. _isPause:
Self. Cond. Wait (self. Mutex)
If self. _value & gt; 100:
Self. _value=https://bbs.csdn.net/topics/0
Self. _value +=1
Self. ValueChange. Emit (self _value)
Self. Msleep (100)
The self. The mutex. Unlock ()


Class Window (QWidget) :

Def __init__ (self, * args, * * kwargs) :
Window, super (self) __init__ (* args, * * kwargs)
Layout=QVBoxLayout (self)
The self. The progressBar=QProgressBar (self)
Layout. AddWidget (self. ProgressBar)
Layout. AddWidget (QPushButton (' dormant 'self, clicked=self doWait))
Layout. AddWidget (QPushButton (' wake up ', the self, clicked=self doWake))

Self. T=Thread (self)
The self. The t.v alueChange. Connect (self. ProgressBar. SetValue)
Self. T.s tart ()

Def doWait (self) :
The self. The t.p ause ()

Def doWake (self) :
Self. T.r esume ()


If __name__=="__main__ ':
The import sys
The from PySide2. QtWidgets import QApplication
App=QApplication (sys. Argv)
W=Window ()
Baron how ()
Sys. Exit (app. Exec_ ())
` ` `

CodePudding user response:

PySide2. QtCore. QWaitCondition. Wait (lockedMutex [, deadline=QDeadlineTimer (QDeadlineTimer. Forever)]), when I was given the deadline for 3000, the performance of the code is normal again - waiting for 3000 ms after wake up the thread, the document said, use the default Forever, waiting for will never timeout, but ask what procedures can not directly response
  • Related