2012-05-18 33 views
0

我正在使用Eric4和PyQt4创建一个应用程序,但由于我是一个新手请随身携带。Python PyQt4如何引用当前打开的对话框?

我有两个对话框,其中一个作为线程运行,另一个是带有内部标签的标准对话框,我想更改为图像。

每次主窗口线程运行时,我都希望它将对话框中显示的当前图像更改为新图像。一切正常,除了每次线程运行时,它都会创建一个新的对话框,其中包含新图像 - 我希望它在当前打开的对话框中更改图像。

对话框里面的图像:

class SubWindow(QDialog, Ui_subWindow): 
    def __init__(self, parent = None): 
     QDialog.__init__(self, parent) 
     self.setupUi(self) 
     self.show() 

    def main(self, img): 
     pic = self.imgView 
     pic.setPixmap(QtGui.QPixmap(os.getcwd() + img)) 

线程,其改变形象:

class MainWindow(QDialog, Ui_MainWindow, threading.Thread): 
    def __init__(self, parent = None): 
     threading.Thread.__init__(self) 
     QDialog.__init__(self, parent) 
     self.setupUi(self) 
     self.show() 
     #some code here which does some stuff then calls changeImg() 

    def changeImg(self): 
     img = SubWindow() 
     img.main(img) 

我不包括我的所有代码,只有相关的位。任何帮助,将不胜感激。谢谢。

+0

我不知道你的代码做什么,但它看起来很可疑。首先,当涉及到与GUI元素的交互时,你最好使用Qt自己的线程类'QThread'。其次,你不应该把GUI相关的东西放在不同的线程中。所有GUI代码应该与事件循环在同一个线程中运行。 – Avaris

回答

0

看起来问题在于,每当您想要更改图像时,都会创建一个新的SubWindow。我建议创建SubWindow作为属性MainWindowMainWindiw.__init__功能:

class MainWindow(QDialog, Ui_MainWindow, threading.Thread): 

    def __init__(self, parent = None): 
     threading.Thread.__init__(self) 
     QDialog.__init__(self, parent) 
     self.setupUi(self) 
     self.show() 
     self.img = SubWindow() # Create SubWindow once here 

    def changeImg(self): 
     self.img.main(self.img) # Only change the image, no new SubWindow 
+0

感谢您的回答,但不幸的是,它不工作,MainWindow类多次作为线程运行,并且它每次运行时都会创建一个新的SubWindow。 – amba88