2012-05-18 107 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