简体   繁体   English

Python Qt如何从QMainWindow打开弹出的QDialog

[英]Python Qt How to open a pop up QDialog from a QMainWindow

I'm working on a project where I have a Database linked with a Python interface (I'm using Qt Designer for the design). 我正在一个项目中,我有一个与Python接口链接的数据库(我正在使用Qt Designer进行设计)。 I want to have a delete button from my main window ( QMainWindow ) where, when I'm pressing it, it opens a pop up ( QDialog ) which says 我想从主窗口( QMainWindow )中有一个删除按钮,当我按下它时,它将打开一个弹出窗口( QDialog ),其中显示

Are you sure you want to delete this item? 你确定要删除这个项目吗?

But I have no idea how to do it. 但是我不知道该怎么做。

Thanks for you help! 感谢您的帮助!

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)

Bind this function to the button click event. 将此功能绑定到按钮单击事件。

Let's say your Qt Designer ui has a main window called "MainWindow" and a button called "buttonDelete". 假设您的Qt Designer ui具有一个名为“ MainWindow”的主窗口和一个名为“ buttonDelete”的按钮。

The first step is to set up your main window class and connect the button's clicked signal to a handler: 第一步是设置主窗口类,并将按钮的单击信号连接到处理程序:

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)

Next you need to add a method to the MainWindow class that handles the signal and opens the dialog: 接下来,您需要向MainWindow类添加一个方法来处理信号并打开对话框:

    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')

This uses one of the built-in QMessageBox functions to create the dialog. 这使用内置的QMessageBox函数之一来创建对话框。 The first three arguments set the parent, title and text. 前三个参数设置父项,标题和文本。 The next two arguments set the group of buttons that are shown, plus the default button (the one that is initially highlighted). 接下来的两个参数设置显示的按钮组以及默认按钮(最初突出显示的按钮)。 If you want to use different buttons, the available ones can be found here . 如果要使用其他按钮,可以在此处找到可用的按钮。

To complete the example, you just need some code to start the application and show the window: 为了完成示例,您只需要一些代码即可启动应用程序并显示窗口:

if __name__ == '__main__':

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

I got the same error for qt5.6 我对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'

So I changed self to None in the following code, and it works. 因此,我在以下代码中将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')

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

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