没有进行PyQt4 QDialog连接

时间:2010-01-30 20:31:17

标签: python pyqt4 signals-slots qdialog

我正在使用PyQt4及其提供的设计器开发应用程序。我有一个主窗口应用程序工作正常,但我想创建自定义消息对话框。我设计了一个对话框,并在__init__方法中设置了一些自定义信号/插槽连接,并编写了if __name__=='__main__':并进行了测试。自定义插槽工作正常。但是,当我从主窗口应用程序创建对话框的实例时,没有任何按钮工作。这是我的对话框:

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在单击时不执行任何操作。我已经google了解决方案,但找不到任何有用的东西。如果您需要更多信息,请与我们联系。任何有关这方面的帮助将不胜感激!

如下所示,为了简洁起见,我的主窗口类中还有一些代码,我添加了省略号以删除看起来无关紧要的代码。如果没有人能想到任何东西,我会添加更多。 (如果缩进有点偏,它就发生在复制粘贴中。原始代码是正确的)

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_()

1 个答案:

答案 0 :(得分:0)

感觉信号没有从父母传递给你的孩子QDIalog。

尝试以下建议:

  1. 使用新方法连接信号
  2. 不是扩展创建的pyuic类,而是扩展实际的QT类并调用pyuic生成的类
  3. 您的新代码将如下所示:

        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)
                ...