so i have a GUI that uses lots of pexpects to login to a program and retrieve information. So i started integrating that code into a Qthread; everything was working fine, until i remembered to test the error messages, it appears that the emited signal inside the Qthread is sometimes not beeing caught by my main code.
Here is the piece of code im talking about:
(python 3, pyqt5)
Code revision 1 - changelog: testf - All values are now loaded into the Qthread using signals and slots - untested, will update soon- Qthread - added "signal receiving function" (tt signal added to loginb)
def testf(self):
self.movie.start()
self.gif.show()
self.myThread = thread()
self.loginb.tt.connect(self.myThread.valueset)
self.loginb.tt.emit(self.userfield.text(), self.pwfield.text(), self.checkBox.text(), self.checkBox.isChecked())
self.myThread.start()
self.myThread.done.connect(self.threadrelay)
def threadrelay(self, code, text): #decides what to do given the thread's emited signal arguments
print('enter relay',code)
self.movie.stop()
self.gif.hide()
self.myThread.exit()
if code == 3:
print(text)
print('trying to open main diag')
self.Omaindialog(text)
elif code == 4:
self.wrongpass.setText(text)
self.wrongpass.show()
else:
self.errordiag(text,code)
thread code:
class thread(QtCore.QThread):
done=pyqtSignal(int, str)
def valueset(self,u,p,c,ci):
print('received values')
self.user=u
self.paw=p
self.check=c
self.checkis=ci
def run (self):
print(self.check == "Save password", self.check)
if (self.paw=='' or self.user=='') and self.check == "Save password":
print('if 1')
txt=("loads of html code here")
self.done.emit(4,txt)
print('emited')
elif ('@' not in self.user or '.' not in self.user) and self.check == "Save password":
print('if 2')
txt=("loads of html code here")
self.done.emit(4,txt)
print('emited')
else:
this shows just the portion affected (maybe more of my code is affected by this, idk yet, i dont really unerstand what is happening here) - so the "testf" function is called (pushbutton), it starts the qthread, then the qthread emits the done signal with parameters that will be evaluated by the "threadrelay" function.
Here is some output (about 5-10 secs bettween each pushbutton click):
True Save password
if 2
emited
True Save password
if 2
emited
True Save password
if 2
emited
True Save password
if 2
emited
True enter relaySave password <- only time it entered the "threadrelay" function)
4if 2
emited
True Save password
if 2
emited
True Save password
if 2
emited
True Save password
if 2
emited
True Save password
if 2
emited
EDIT1: lol i went out for a cup of cofee came back, and now it works more often than it doesn't... I'm baffled... it still doesn't work sometimes though
EDIT2: I realize i can make this check before the Qthread, but this problem would probably also happen with the rest of the emits in the Qthread (I just had't caught it), and i'd like to avoid it and know what's wrong...
About the code in run: the check box is multi-purpose to save and to load password (the text within it changes accordingly: if there is an available password, it gets auto ticked into use saved; if user unticks, it changes to "save password" option); The @ or . Not in usr.txt() is because user must be an email. The wp(wrongpass) is a label that displays 'please fill in both fields' or 'insert a valid email'.
Update: Using signals/slots to pass the variables did not fix my problem. But i fixed it, it occurred to me that maybe, the Qthread was emiting the signals to fast, so i added a time.sleep(1) in the if and the elif before emiting the signal, and it works perfectly! - Could this actually have been the cause?