2010-01-30 17 views
1

我正在使用PyQt4和它提供的設計器的應用程序。我有一個主窗口應用程序工作正常,但我想創建自定義消息對話框。我設計了一個對話框,並在__init__方法中設置了一些自定義信號/插槽連接,並編寫了一個if __name__=='__main__':並進行了測試。自定義插槽正常工作。但是,當我從主窗口應用程序創建對話框的實例時,沒有任何按鈕可用。這裏是我的對話框:PyQt4 QDialog連接沒有進行

from PyQt4.QtGui import * 
from PyQt4.QtCore import * 
import sys 
import encode_dialog_ui 

# Ui_EncodeDialog is the python class generated by pyuic4 from the Designer 
class EncodeDialog(encode_dialog_ui.Ui_EncodeDialog): 

    def __init__(self, parent, in_org_im, txt_file, in_enc_im): 
     self.qd = QDialog(parent) 
     self.setupUi(self.qd) 
     self.qd.show() 
     self.message = (txt_file.split("/")[-1] + " encoded into " + 
      in_org_im.split("/")[-1] + " and written to " + 
      in_enc_im.split("/")[-1] + ".") 

     QObject.connect(self.view_image_button, SIGNAL("clicked()"), 
         self.on_view_image_button_press) 

     self.org_im = in_org_im 
     self.enc_im = in_enc_im 

     self.encoded_label.setText(self.message)  

    def on_view_image_button_press(self): 
     print "hello world" 

if __name__ == '__main__': 
    app = QApplication(sys.argv) 
    tmp = QMainWindow() 
    myg = EncodeDialog(tmp,'flower2.png','b','flower.png') 
    app.exec_() 

如果我運行這個類,它工作正常,並按下view_image_button打印hello world到控制檯。然而,當我使用呼叫

#self.mw is a QMainWindow, the rest are strings 
EncodeDialog(self.mw, self.encode_image_filename, 
      self.encode_txt_filename, 
      self.encode_new_image_filename) 
在我的主窗口類

,對話框顯示正常,但點擊時的view_image_button什麼都不做。我已經搜索了一個解決方案,但找不到任何有用的東西。讓我知道你是否需要更多信息。任何幫助,將不勝感激!

爲了簡潔起見,下面要求的是我的主窗口類中的一些代碼,我添加了省略號來移除看似不相關的代碼。如果沒有人能想到任何東西,我會補充更多。 (如果縮進是有點過,它發生在複製粘貼。的一部開拓創新的代碼是正確的)

class MyGUI(MainWindow.Ui_MainWindow): 

    def __init__(self): 
     self.mw = QMainWindow() 
     self.setupUi(self.mw) 
     self.mw.show() 

     self.encode_red_bits = 1 
     self.encode_blue_bits = 1 
     self.encode_green_bits = 1 

     self.decode_red_bits = 1 
     self.decode_blue_bits = 1 
     self.decode_green_bits = 1 

     self.encode_image_filename = "" 
     self.encode_new_image_filename = "" 
     self.encode_txt_filename = "" 

     self.decode_image_filename = "" 
     self.decode_txt_filename = "" 

     # Encode events 
     ... 
     QObject.connect(self.encode_button, SIGNAL("clicked()"), 
         self.on_encode_button_press) 

     # Decode events 
     ... 


    # Encode event handlers 
    ... 

    def on_encode_button_press(self): 
     tmp = QErrorMessage(self.mw) 
     if (self.encode_image_filename != "" and 
      self.encode_new_image_filename != "" and 
      self.encode_txt_filename != ""): 


      try: 
       im = Steganography.encode(self.encode_image_filename, self.encode_txt_filename, 
              self.encode_red_bits, self.encode_green_bits, 
              self.encode_blue_bits) 
       im.save(self.encode_new_image_filename) 
       encode_dialog.EncodeDialog(self.mw, self.encode_image_filename, 
              self.encode_txt_filename, 
              self.encode_new_image_filename) 
      except Steganography.FileTooLargeException: 
       tmp.showMessage(self.encode_txt_filename.split("/")[-1] + 
           " is to large to be encoded into " + 
           self.encode_image_filename.split("/")[-1]) 

     else: 
      tmp.showMessage("Please specify all filenames.") 


    # Decode event handlers 
    ... 


if __name__ == '__main__': 
    app = QApplication(sys.argv) 
    myg = MyGUI() 
    app.exec_() 
+0

你能發表更多的代碼從你的主窗口課程?我沒有看到這個代碼有什麼問題... - 另外,試圖寫入文件而不是打印以確保它*真的*不會調用該函數 – 2010-02-01 15:42:08

+0

這是簡化的代碼。最初我在函數中使用Image.show()(來自PIL)來顯示一個圖像進行調試,但是如果沒有工作,我切換到一個簡單的打印。我會嘗試打印到一個文件。我將在上面的主窗口類中添加更多的代碼。 – 2010-02-02 02:12:17

+0

我試圖寫入一個文件,仍然沒有迴應。 – 2010-02-02 02:32:45

回答

0

感覺就像信號只是沒有得到從父傳下來的孩子QDialog的。

試試以下建議:

  1. 使用新的方法,用於連接信號
  2. 不是擴展pyuic創建的類,擴展了實際的QT類和調用由pyuic

產生的那些你的新代碼將如下所示:

class MyGUI(QMainWindow): 
     def __init__(self, parent=None): 
      QMainWindow.__init__(self, parent) 
      self.mw = MainWindow.Ui_MainWindow() 
      self.mw.setupUi(self) 
      self.mw.show() 
      ... 
      self.encode_button.clicked.connect(self.on_encode_button_press) 
      ... 

    class EncodeDialog(QDialog): 
     def __init__(self, parent, in_org_im, txt_file, in_enc_im): 
      QDialog.__init__(self, parent) 
      self.qd = encode_dialog_ui.Ui_EncodeDialog() 
      self.qd.setupUi(self) 
      self.qd.show() 
      ... 
      self.view_image_button.clicked.connect(self.on_view_image_button_press) 
      ... 
+0

感謝您的想法,但我沒有得到任何與此。問題是Ui_EncodeDialog沒有像show()這樣的方法,QDialog沒有必要的按鈕或標籤。我嘗試瞭解它,並且確實讓它顯示了對話框,但是按鈕甚至沒有使用'__name __ =「__ main __」'方法工作。我會繼續玩,但至今我仍然沒有解決方案。感謝所有的幫助! – 2010-02-09 00:20:44