PyQt QDialog 返回响应是或否 [英] PyQt QDialog return response yes or no

查看:41
本文介绍了PyQt QDialog 返回响应是或否的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 QDialog 类

confirmation_dialog = uic.loadUiType("ui\\confirmation_dialog.ui")[0]类 ConfirmationDialog(QDialog,confirmation_dialog):def __init__(self,parent=None):QDialog.__init__(self,parent)self.setupUi(self)message = "你好,对话测试"self.yes_button.clicked.connect(self.yes_clicked)self.no_button.clicked.connect(self.no_clicked)self.message_box.insertPlainText(message)def yes_clicked(self):self.emit(SIGNAL("dialog_response"),"是")def no_clicked(self):self.emit(SIGNAL("dialog_response"),"no")

我有一个需要确认是否继续的函数,但在当前的实现中,它不会等待 QDialog 关闭.

如何让我的函数等待 QDialog 的响应,然后相应地继续.

我想实现类似于 confirm 功能的东西,如下所示

def 函数(self):........如果 self.confirm() == 'yes':#做一点事elif self.confirm() == '否':#做一点事定义确认(自我):对话框 = 确认对话框()对话框显示()从对话框返回#response

解决方案

您将使用 dialog.exec_(),它将以模态、阻塞模式打开对话框并返回一个整数,指示对话是否被接受.通常,您可能只想在对话框内调用 self.accept()self.reject() 来关闭它,而不是发出信号.

dialog = ConfirmationDialog()结果 = dialog.exec_()如果结果:#接受返回已接受"

如果我使用对话框从用户那里获取一组特定的值,我通常会将它包装在 staticmethod 中,这样我就可以调用它并在其中获取值我的应用程序的控制流,就像一个普通的函数.

class MyDialog(...)def getValues(self):返回 (self.textedit.text(), self.combobox.currentText())@静态方法定义启动(父):dlg = MyDialog(父)r = dlg.exec_()如果 r:返回 dlg.getValues()返回无values = MyDialog.launch(None)

然而,在几乎所有的情况下,我只需要向用户展示一条消息,或者通过点击按钮让他们做出选择,或者我需要他们输入一小段数据,我可以使用内置的- 在普通对话框类的静态方法中 -- QMessageBox, QInputDialog

I have a QDialog class

confirmation_dialog = uic.loadUiType("ui\\confirmation_dialog.ui")[0]

class ConfirmationDialog(QDialog,confirmation_dialog):

    def __init__(self,parent=None):
        QDialog.__init__(self,parent)
        self.setupUi(self)
        message = "Hello, Dialog test"
        self.yes_button.clicked.connect(self.yes_clicked)
        self.no_button.clicked.connect(self.no_clicked)
        self.message_box.insertPlainText(message)

    def yes_clicked(self):
        self.emit(SIGNAL("dialog_response"),"yes")

    def no_clicked(self):
        self.emit(SIGNAL("dialog_response"),"no")

I have a function which requires confirmation to continue or not, but with the current implementation it doesn't wait for QDialog to close.

How can I make my function wait for response from QDialog and then proceed accordingly.

I want to implement something similar to confirm function as shown below

def function(self):
    ....
    ....
    if self.confirm() == 'yes':
        #do something
    elif self.confirm() == 'no':
        #do something

def confirm(self):
    dialog = ConfirmationDialog()
    dialog.show()
    return #response from dialog

解决方案

You would use dialog.exec_(), which will open the dialog in a modal, blocking mode and returns an integer that indicates whether the dialog was accepted or not. Generally, instead of emitting signals, you probably just want to call self.accept() or self.reject() inside the dialog to close it.

dialog = ConfirmationDialog()
result = dialog.exec_()
if result:  # accepted
    return 'accepted'

If I'm using a dialog to get a specific set of values from the user, I'll usually wrap it in a staticmethod, that way I can just call it and get values back within the control flow of my application, just like a normal function.

class MyDialog(...)

    def getValues(self):
        return (self.textedit.text(), self.combobox.currentText())

    @staticmethod
    def launch(parent):
        dlg = MyDialog(parent)
        r = dlg.exec_()
        if r:
            return dlg.getValues()
        return None

values = MyDialog.launch(None)

However, in almost all cases where I just need to present a message to the user, or get them to make a choice by clicking a button, or I need them to input a small piece of data, I can use the built-in static methods on the normal dialog classes -- QMessageBox, QInputDialog

这篇关于PyQt QDialog 返回响应是或否的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆