1

我正在使用 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 什么也不做。我已经用谷歌搜索了一个解决方案,但找不到任何有用的东西。如果您需要更多信息,请告诉我。对此的任何帮助将不胜感激!

按照下面的要求,为了简洁起见,我的主窗口类中提供了更多代码,我添加了省略号以删除似乎不相关的代码。如果没有人能想到任何东西,我会添加更多。(如果缩进有点不对,就是复制粘贴的时候出现的,原码是对的)

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

1 回答 1

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)
            ...
于 2010-02-08T15:09:16.170 回答