简体   繁体   English

PyQt4:如何从主窗口重新打开对话框窗口

[英]PyQt4: How to reopen dialog window from main window

I am using PyQt4 with Python 3. I am trying to open a dialog window when a button in the main window is pressed. 我正在将PyQt4与Python 3配合使用。我试图在按下主窗口中的按钮时打开一个对话框窗口。 The dialog window also needs to be able to send data back to the main window via signals. 对话窗口还需要能够通过信号将数据发送回主窗口。

A similar question has been asked here: Open a second window in PyQt 这里也提出了类似的问题: 在PyQt中打开第二个窗口

I have used that post as a guide to build my code. 我已将该帖子用作构建代码的指南。 Now all of that is currently working, except for if you close the dialog window, and try to open it again, you get: 现在,所有这些都可以正常工作,除非您关闭对话框窗口,然后尝试再次打开它,您将获得:

RuntimeError: wrapped C/C++ object of type QDialog has been deleted RuntimeError:QDialog类型的包装C / C ++对象已被删除

Meaning you have to restart the program before you can open it again. 这意味着您必须重新启动程序才能再次打开它。 This will not work for my particular application. 这不适用于我的特定应用程序。

From what I understand here: https://www.daniweb.com/programming/software-development/threads/299395/pyqt-how-to-open-and-close-dialog-multiple-times 据我了解: https//www.daniweb.com/programming/software-development/threads/299395/pyqt-how-to-open-and-close-dialog-multiple-times

I need to destroy the object (the dialog window) before trying to open it again. 我需要销毁对象(对话框窗口),然后再尝试将其打开。 I tried to do that, however I am not sure how to do it without closing the entire application when I just want to close the dialog window. 我试图这样做,但是当我只想关闭对话框窗口时,我不确定在不关闭整个应用程序的情况下怎么做。 Also not sure if that is the solution. 也不确定这是否是解决方案。

Here is a summarized version of my code: 这是我的代码的摘要版本:

Main window: 主视窗:

from PyQt4 import QtCore, QtGui
#Qt designer generated code here
class Ui_MainWindow(object):

    def setupUi(self, MainWindow):
        #Ui setup stuff here
        #Button 3
        self.pushButton_3 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_3.clicked.connect(self.pressed_3)

        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        #signal from dialog menu
        self.dialog = QtGui.QDialog()
        self.dialog.ui = entry_window_2.Ui_Dialog()
        self.dialog.ui.setupUi(self.dialog)
        self.dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.dialog.ui.sendVals.connect(self.recieved_save_data)

    def pressed_3(self, checked=None):
        self.dialog.exec_()

    def recieved_save_data(self, value):
        print(value)

Dialog window 对话视窗

from PyQt4 import QtCore, QtGui
#PyQt generated code here

class Ui_Dialog(QtCore.QObject):
    sendVals = QtCore.pyqtSignal(int)

    def setupUi(self, Dialog):
        #PyQt Ui_setup code here

        self.ok_cancel = QtGui.QDialogButtonBox(Dialog)
        self.ok_cancel.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
        self.ok_cancel.accepted.connect(self.save)

        QtCore.QObject.connect(self.ok_cancel, QtCore.SIGNAL(_fromUtf8("accepted()")), Dialog.accept)
        QtCore.QObject.connect(self.ok_cancel, QtCore.SIGNAL(_fromUtf8("rejected()")), Dialog.reject)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def save(self):
        self.sendVals.emit(1212)

Any help would be appreciated! 任何帮助,将不胜感激! :) :)

So I solved the issue thanks to this article: http://enki-editor.org/2014/08/23/Pyqt_mem_mgmt.html 因此,由于有了这篇文章,我解决了这个问题: http : //enki-editor.org/2014/08/23/Pyqt_mem_mgmt.html

The solution is to delete this line here: 解决方法是在此处删除此行:

self.dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)

I am no expert, but from what I understand, for every C++ class in PyQt there is a python wrapper class. 我不是专家,但是据我了解,对于PyQt中的每个C ++类,都有一个python包装器类。 As the python programmer, you are interacting with the python wrapper, and wrapper interacts with the C++ class in the background. 作为python程序员,您正在与python包装器进行交互,而包装器在后台与C ++类进行交互。

The issue here is the "C++ object is deleted by Qt but Python wrapper still exists". 这里的问题是“ Qt删除了C ++对象,但Python包装器仍然存在”。 This appears to be caused by that line that just deleted. 这似乎是由刚删除的那一行引起的。

Perhaps someone can explain it better than I can. 也许有人可以比我更好地解释它。 But that did in fact fix the problem. 但这确实解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM