PyQt4 QDialog连接不正常

2024-09-28 21:04:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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_按钮将hello world打印到控制台。但是当我用电话的时候

^{pr2}$

在我的主窗口类中,对话框正确显示,但单击“查看图像”按钮时不执行任何操作。我在谷歌上搜索了一个解决方案,但没有找到任何有用的东西。如果你需要更多信息,请告诉我。如有任何帮助,将不胜感激!在

根据下面的要求,我的主窗口类中还有一些代码,为了简洁起见,我添加了省略号来删除似乎不相关的代码。如果没有人能想到什么,我会补充更多。(如果缩进有点偏差,则在复印粘贴时发生。原代码正确)

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

Tags: inimageselftxtbuttonfilenametmpbits
1条回答
网友
1楼 · 发布于 2024-09-28 21:04:21

感觉就像是信号没有从家长传递到你的孩子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)
            ...

相关问题 更多 >