[英]How to close a QDialog
I've been trying to close a QDialog window that is branching off of my main window.我一直在尝试关闭从主窗口分支出来的 QDialog 窗口。 The following have not worked for me so far:到目前为止,以下内容对我不起作用:
self.close()
QDialog.close()
I tried other commands such as exit
and exec_()
with no luck.我尝试了其他命令,例如exit
和exec_()
,但没有成功。 The most common error I get is我得到的最常见的错误是
[className] object has no attribute 'close' [className] 对象没有属性“关闭”
# Creating our window
class Ui_MainWindow(object):
# Sets up GUI
def setupUi(self, MainWindow):
[GUI CODE]
# Sets text for parts of GUI
def retranslateUi(self, MainWindow):
[MORE GUI CODE]
# Function handling screencap on click and metadata for filenames
def cap_on_Click(arg1,arg2):
popup = QDialog()
popup_ui = Ui_Dialog()
popup_ui.setupUi(popup)
popup.show()
sys.exit(popup.exec_())
The above is my main window上面是我的主窗口
class Ui_Dialog(object):
def setupUi(self, Dialog):
[GUI CODE]
def retranslateUi(self, Dialog):
[MORE GUI CODE]
def button_click(self, arg1):
self.close()
The second block is the dialog window code.第二个块是对话窗口代码。 How do I close this dialog window?如何关闭此对话窗口?
First of all, sorry for the links related to C++, but Python has the same concept . 首先,很抱歉与C ++相关的链接,但是Python具有相同的概念 。
You can try using the reject
or accept
or done
function to close the dialog box. 您可以尝试使用reject
或accept
或done
功能来关闭对话框。 By doing this, you're setting the return value appropriately ( Rejected
, Accepted
or the one you specify as the argument). 这样,您就可以适当地设置返回值( Rejected
, Accepted
或您指定为参数的值)。
All in all, you should try calling YourDialog.done(n)
to close the dialog and return n
and YourDialog.accept()
or YourDialog.reject()
when you want it accepted/rejected. 总而言之,您应该尝试调用YourDialog.done(n)
关闭对话框,并在希望接受/拒绝时返回n
和YourDialog.accept()
或YourDialog.reject()
。
I guess the problem is that Ui_Dialog
does not inherit QDialog
, so reject()
, accept()
and done()
is not defined. 我猜问题是Ui_Dialog
不继承QDialog
,所以没有定义reject()
, accept()
和done()
。 I think 我认为
class Ui_Dialog(object):
should be changed to 应该更改为
class Ui_Dialog(QDialog):
But I can not test it because minimal working example is not provided. 但是我无法测试它,因为没有提供最少的工作示例。
Since a QDialog
is a QWidget
, and a QWidget
has the close()
method, I don't understand how it can not work. 由于QDialog
是QWidget
,并且QWidget
具有close()
方法,所以我不明白它是如何工作的。 You should never invoke popup.exec_()
though, since it will happily require a lot of your code to be reentrant without you realizing it. 但是,您永远不要调用popup.exec_()
,因为它很可能需要大量代码才能在您没有意识到的情况下重新输入。 It is unnecessary - you already have the application event loop running and on the call stack when cap_on_Click
is executing. 不必要-执行cap_on_Click
时, cap_on_Click
在调用堆栈上运行了应用程序事件循环。
After popup.show()
, the dialog will be visible and usable until it gets accepted or rejected by the user. 在popup.show()
,该对话框将可见并可用,直到被用户接受或拒绝为止。 Hopefully your design for the dialog connects the button box's accepted()
and rejected()
signals to the dialog's accept()
and reject()
slots. 希望您设计的对话框按钮盒的连接accepted()
和rejected()
信号对话框的accept()
和reject()
插槽。 That is the default behavior of a QDialog
template provided with Qt Creator and perhaps Qt Designer as well, but you should check it by going into the signal/slot editor mode while viewing the Ui file. 这是Qt Creator和Qt Designer附带的QDialog
模板的默认行为,但是您应该在查看Ui文件时进入信号/插槽编辑器模式来检查它。
I know it's over 5months but I choose to drop this comment here, it might be of help to others tomorrow.我知道已经超过 5 个月了,但我选择在此处删除此评论,明天可能对其他人有所帮助。 To be able to close or cancel an opened dialog box, using only self.close would close the entire program.为了能够关闭或取消打开的对话框,仅使用 self.close 将关闭整个程序。 use this example:使用这个例子:
self.dlg = QDialog(self)
self.dlg.setWindowTitle("Admin")
self.dlg.setGeometry(100,100,300,200)
btnBox = QDialogButtonBox()
btnBox.setStandardButtons(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
btnBox.rejected.connect(self.close1)
def close1():
self.dlg.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.