我有以下代码:
class Functions(QObject):
mysig = Signal(filename)
def __init__(self, parent=None):
super(Functions, self).__init__(parent)
self.result = None
def showDialog(self, filename):
self.mysig.emit(filename)
def grabResult(self):
while not self.result:
time.sleep(5)
return result #this is the question
def setResult(self, result):
self.result = result
代码的另一部分是这样的:
class Dialog(QDialog):
anotherSig = Signal(str)
fun = Functions()
def __init__(self, parent=None, filename=filename):
self.filename = filename
#Here it displays a picture based on the filename parameter
def okButtonClicked(self):
text = self.lineedit.text()
fun.setResult(text)
#Tried also this:
self.anotherSig.emit(text)
Functions()
类从 worker QThread
调用(此处未显示)。
我想我的问题是:我如何告诉我的 Functions
类用户已经输入了文本并单击了 OK 按钮?我尝试连接那个 anotherSig
信号,但是当我尝试这样做时,Qt 提示 QPixmaps 从不同的线程设置不安全,而且它不起作用。
我在这里使用的方法“有效”,但我觉得它不是很可靠。另外,它仅在 Functions
类中的所有相关方法都是 @classmethod
时才起作用 - 这样,出于某种原因,它不起作用。 setResult
被调用(我添加了一个 print
语句来确保),但是 grabResult
仍然显示 self.result
作为 无
。
最佳答案
此代码不起作用,因为对 showDialog
的调用发生在 Functions
对象的实例化上,该对象是另一个线程上关闭的任何对象的属性.您设置结果的 Dialog
中的 fun
是不同的实例。
要将结果移回原始 Functions
对象,我认为您需要将 Dialog
对象的 anotherSig
连接到
函数,您希望返回结果。Functions
对象上的 setResult
做这样的事情吗(没有大量的样板很难测试它)。
class Functions(QObject):
mysig = Signal(filename,Functions)
def __init__(self, parent=None):
super(Functions, self).__init__(parent)
self.result = None
def showDialog(self, filename):
self.mysig.emit(filename,self)
def grabResult(self):
while not self.result:
time.sleep(5)
return result #this is the question
@QtCore.Slot(str)
def setResult(self, result):
self.result = result
def connection_fun(filename,fun):
d = Dialog(filename)
# what ever else you do in here
d.anotherSig.connect(fun.setResult))
关于python - 我如何告诉类方法等待,直到捕获到来自 QDialog 类方法的信号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13696212/