Python Qt如何从Qmain窗口打开弹出式QDialog

Python Qt如何从Qmain窗口打开弹出式QDialog,python,qt,popup,qmainwindow,qdialog,Python,Qt,Popup,Qmainwindow,Qdialog,我正在从事一个项目,其中我有一个与Python接口链接的数据库(我使用Qt Designer进行设计)。我想在我的主窗口(QMainWindow)上有一个删除按钮,当我按下该按钮时,它会打开一个弹出窗口(QDialog),上面写着 是否确实要删除此项目 但是我不知道怎么做 谢谢你的帮助 def button_click(): dialog = QtGui.QMessageBox.information(self, 'Delete?', 'Are you sure you want to

我正在从事一个项目,其中我有一个与Python接口链接的数据库(我使用Qt Designer进行设计)。我想在我的主窗口(
QMainWindow
)上有一个删除按钮,当我按下该按钮时,它会打开一个弹出窗口(
QDialog
),上面写着

是否确实要删除此项目

但是我不知道怎么做

谢谢你的帮助

def button_click():
    dialog = QtGui.QMessageBox.information(self, 'Delete?', 'Are you sure you want to delete this item?', buttons = QtGui.QMessageBox.Ok|QtGui.QMessageBox.Cancel)
将此函数绑定到按钮单击事件


将此函数绑定到按钮单击事件。

假设您的Qt Designer ui有一个名为“MainWindow”的主窗口和一个名为“buttonDelete”的按钮

第一步是设置主窗口类,并将按钮的单击信号连接到处理程序:

from PyQt4 import QtCore, QtGui
from mainwindow_ui import Ui_MainWindow

class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.setupUi(self)
        self.buttonDelete.clicked.connect(self.handleButtonDelete)
接下来,您需要向
main窗口
类添加一个方法,该方法处理信号并打开对话框:

    def handleButtonDelete(self):
        answer = QtGui.QMessageBox.question(
            self, 'Delete Item', 'Are you sure you want to delete this item?',
            QtGui.QMessageBox.Yes | QtGui.QMessageBox.No |
            QtGui.QMessageBox.Cancel,
            QtGui.QMessageBox.No)
        if answer == QtGui.QMessageBox.Yes:
            # code to delete the item
            print('Yes')
        elif answer == QtGui.QMessageBox.No:
            # code to carry on without deleting
            print('No')
        else:
            # code to abort the whole operation
            print('Cancel')
这将使用其中一个内置对话框来创建对话框。前三个参数设置父项、标题和文本。接下来的两个参数设置显示的按钮组,以及默认按钮(最初高亮显示的按钮)。如果要使用不同的按钮,可以找到可用的按钮

要完成此示例,您只需要一些代码来启动应用程序并显示窗口:

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

假设您的Qt Designer ui有一个名为“MainWindow”的主窗口和一个名为“buttonDelete”的按钮

第一步是设置主窗口类,并将按钮的单击信号连接到处理程序:

from PyQt4 import QtCore, QtGui
from mainwindow_ui import Ui_MainWindow

class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.setupUi(self)
        self.buttonDelete.clicked.connect(self.handleButtonDelete)
接下来,您需要向
main窗口
类添加一个方法,该方法处理信号并打开对话框:

    def handleButtonDelete(self):
        answer = QtGui.QMessageBox.question(
            self, 'Delete Item', 'Are you sure you want to delete this item?',
            QtGui.QMessageBox.Yes | QtGui.QMessageBox.No |
            QtGui.QMessageBox.Cancel,
            QtGui.QMessageBox.No)
        if answer == QtGui.QMessageBox.Yes:
            # code to delete the item
            print('Yes')
        elif answer == QtGui.QMessageBox.No:
            # code to carry on without deleting
            print('No')
        else:
            # code to abort the whole operation
            print('Cancel')
这将使用其中一个内置对话框来创建对话框。前三个参数设置父项、标题和文本。接下来的两个参数设置显示的按钮组,以及默认按钮(最初高亮显示的按钮)。如果要使用不同的按钮,可以找到可用的按钮

要完成此示例,您只需要一些代码来启动应用程序并显示窗口:

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

对于qt5.6,我得到了相同的错误

TypeError: question(QWidget, str, str, buttons: Union[QMessageBox.StandardButtons, QMessageBox.StandardButton] = QMessageBox.StandardButtons(QMessageBox.Yes|QMessageBox.No), defaultButton: QMessageBox.StandardButton = QMessageBox.NoButton): argument 1 has unexpected type 'Ui_MainWindow'
因此,我在下面的代码中将
self
更改为
None
,它可以正常工作

def handleButtonDelete(self):
    answer = QtGui.QMessageBox.question(
        None, 'Delete Item', 'Are you sure you want to delete this item?',
        QtGui.QMessageBox.Yes | QtGui.QMessageBox.No |
        QtGui.QMessageBox.Cancel,
        QtGui.QMessageBox.No)
    if answer == QtGui.QMessageBox.Yes:
        # code to delete the item
        print('Yes')
    elif answer == QtGui.QMessageBox.No:
        # code to carry on without deleting
        print('No')
    else:
        # code to abort the whole operation
        print('Cancel')

对于qt5.6,我得到了相同的错误

TypeError: question(QWidget, str, str, buttons: Union[QMessageBox.StandardButtons, QMessageBox.StandardButton] = QMessageBox.StandardButtons(QMessageBox.Yes|QMessageBox.No), defaultButton: QMessageBox.StandardButton = QMessageBox.NoButton): argument 1 has unexpected type 'Ui_MainWindow'
因此,我在下面的代码中将
self
更改为
None
,它可以正常工作

def handleButtonDelete(self):
    answer = QtGui.QMessageBox.question(
        None, 'Delete Item', 'Are you sure you want to delete this item?',
        QtGui.QMessageBox.Yes | QtGui.QMessageBox.No |
        QtGui.QMessageBox.Cancel,
        QtGui.QMessageBox.No)
    if answer == QtGui.QMessageBox.Yes:
        # code to delete the item
        print('Yes')
    elif answer == QtGui.QMessageBox.No:
        # code to carry on without deleting
        print('No')
    else:
        # code to abort the whole operation
        print('Cancel')

我在编写脚本时出现了以下错误:dialog=QtGui.QMessageBox.information(self.tableViewPatient,“Delete?”,“确实要删除此项吗?”,buttons=QtGui.QMessageBox.Ok | QtGui.QMessageBox.Cancel)类型错误:参数与任何重载调用不匹配:QMessageBox.information(QWidget,QString,QString,QMessageBox.StandardButtons buttons=QMessageBox.Ok,QMessageBox.StandardButton defaultButton=QMessageBox.NoButton):参数1具有意外的类型“PySide.QtGui.QTableView”,我想这可能是自身的错误?(不确定)你还可以帮我吗?我在编写脚本时遇到了这个错误:dialog=QtGui.QMessageBox.information(self.tableViewPatient,“Delete?”,“确实要删除此项吗?”,buttons=QtGui.QMessageBox.Ok | QtGui.QMessageBox.Cancel)TypeError:参数与任何重载调用不匹配:QMessageBox.information(QWidget,QString,QString,QMessageBox.StandardButtons buttons=QMessageBox.Ok,QMessageBox.StandardButton defaultButton=QMessageBox.NoButton):参数1具有意外的类型“PySide.QtGui.QTableView”,我想这可能是自身的错误?(不确定)您仍然可以帮我解决这个问题吗?我得到了以下错误:TypeError:参数与任何重载调用不匹配:QMessageBox.question(QWidget、QString、QString、QMessageBox.StandardButtons buttons=QMessageBox.Ok、QMessageBox.StandardButton defaultButton=QMessageBox.NoButton):参数1具有意外类型'MyKinectViewer@user3369214.我的示例代码是正确的(我知道,因为我已经测试过了)。
QMessageBox.question
的第一个参数(在我的示例中是
self
)必须是一个Qt小部件。我怀疑您正试图传递一个普通的python对象,这就是为什么会出现错误。什么是
MyKinectViewer
类?如果没有看到您的代码,就很难进一步诊断问题。嗯,MyKinectViewer是一个QGLWidget…您能帮我了解一下这些信息吗?:这是一个小部件它允许我显示Kinect 3Dvideo@user3369214.您的
MyKinectViewer
类不是
QGLWidget
的子类。如果是,则不会出现该错误。您可以通过执行
print(viewer.\uuuuu class\uuuuu.mro())来确认这一点
,其中
viewer
MyKinectViewer
的一个实例。这将表明它既不继承
QGLWidget
,也不继承
QWidget
。它可能有一个
QGLWidget
(或其他一些
QWidget
)的实例作为属性。如果继承,则传递此小部件(或任何其他小部件,如您的主窗口)作为
QMessageBox.question
的第一个参数。当我打印print(self.\uu class.\uuuu.mro())时,我收到以下消息:[,,,,,,],这是什么意思?(很抱歉打扰您这么多。)我得到以下错误:TypeError:参数与任何重载调用不匹配:QMessageBox.question(QWidget、QString、QString、QMessageBox.StandardButtons buttons=QMessageBox.Ok、QMessageBox.StandardButton defaultButton=QMessageBox.NoButton):参数1具有意外类型'MyKinectViewer@user3369214.我的示例代码是正确的(我知道,因为我已经测试过了)。QMessageBox.question的第一个参数(在我的示例中,它是
self
)必须是一个Qt小部件。我怀疑您正试图传递一个普通的python对象,这就是为什么会出现错误。什么是
MyKinectViewer
类?如果没有看到您的代码,就很难进一步诊断问题。嗯,MyKinectViewer是一个QGLWidget…您能帮我了解一下这些信息吗?:这是一个小部件这使得我