zoukankan      html  css  js  c++  java
  • Message Box

    By default, if we click on the x button on the titlebar, the QtGui.QWidget is closed. Sometimes we want to modify this default behaviour. For example, if we have a file opened in an editor to which we did some changes. We show a message box to confirm the action.

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    """
    ZetCode PyQt4 tutorial 
    
    This program shows a confirmation 
    message box when we click on the close
    button of the application window. 
    
    author: Jan Bodnar
    website: zetcode.com 
    last edited: October 2011
    """
    
    import sys
    from PyQt4 import QtGui
    
    
    class Example(QtGui.QWidget):
        
        def __init__(self):
            super(Example, self).__init__()
            
            self.initUI()
            
            
        def initUI(self):               
            
            self.setGeometry(300, 300, 250, 150)        
            self.setWindowTitle('Message box')    
            self.show()
            
            
        def closeEvent(self, event):
            
            reply = QtGui.QMessageBox.question(self, 'Message',
                "Are you sure to quit?", QtGui.QMessageBox.Yes | 
                QtGui.QMessageBox.No, QtGui.QMessageBox.No)
    
            if reply == QtGui.QMessageBox.Yes:
                event.accept()
            else:
                event.ignore()        
            
            
    def main():
        
        app = QtGui.QApplication(sys.argv)
        ex = Example()
        sys.exit(app.exec_())
    
    
    if __name__ == '__main__':
        main()
    

    If we close a QtGui.QWidget, a QtGui.QCloseEvent is generated. To modify the widget behaviour we need to reimplement the closeEvent() event handler.

    reply = QtGui.QMessageBox.question(self, 'Message',
        "Are you sure to quit?", QtGui.QMessageBox.Yes | 
        QtGui.QMessageBox.No, QtGui.QMessageBox.No)
    

    We show a message box with two buttons: Yes and No. The first string appears on the titlebar. The second string is the message text displayed by the dialog. The third argument specifies the combination of buttons appearing in the dialog. The last parameter is the default button. It is the button which has initially the keyboard focus. The return value is stored in the reply variable.

    if reply == QtGui.QMessageBox.Yes:
        event.accept()
    else:
        event.ignore()  
    

    Here we test the return value. If we click the Yes button, we accept the event which leads to the closure of the widget and to the termination of the application. Otherwise we ignore the close event.

    Message boxFigure: Message box

  • 相关阅读:
    软件错误,软件缺陷,软件故障与软件失效
    QEMU命令创建KVM Guest(bridge桥接)
    Linux下设置网卡随系统启动
    RHEL查看CPU等机器信息
    QEMU/KVM功能测试
    CentOS 删除自带的OpenJDK 和 安装SunJDK
    将Centos的yum源更换为国内的阿里云源
    Centos6.8下安装oracle_11gr2版主要过程
    Centos6.8下安装oracle_11gr2版主要过程
    opendrive.com提供的免费网盘
  • 原文地址:https://www.cnblogs.com/hushaojun/p/4435402.html
Copyright © 2011-2022 走看看