【问题标题】:PyQt4 QDialog connections not being madePyQt4 QDialog 连接未​​建立
【发布时间】:2010-01-30 20:31:16
【问题描述】:

我正在使用 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_()

【问题讨论】:

  • 你能从你的主窗口类中发布更多代码吗?我看不出这段代码有什么问题...... - 另外,尝试写入文件而不是打印以确保它真的没有调用该函数
  • 这是简化的代码。最初我在函数中使用 Image.show()(来自 PIL)来显示用于调试的图像,但是当它不起作用时,我切换到了简单的打印。我将尝试打印到文件。我将从上面的主窗口类中添加更多代码。
  • 我尝试写入文件,仍然没有响应。
  • 抱歉回复延迟,但我想我有个主意

标签: python pyqt4 signals-slots qdialog


【解决方案1】:

感觉信号只是没有从父母传递到您的孩子 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)
            ...

【讨论】:

  • 感谢您的想法,但我对此一无所知。问题是 Ui_EncodeDialog 没有像 show() 这样的方法,而 QDialog 没有必要的按钮或标签。我试着摆弄它,我确实让它显示对话框,但是使用__name__="__main__" 方法时按钮甚至不起作用。我会继续玩它,但到目前为止我仍然没有解决方案。感谢所有的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多