1
import sys
from PyQt4 import QtGui


class MessageBox(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('message box')
    def closeEvent(self, event):
        reply = QtGui.QMessageBox.question(self, 'Message',"Are you sure to quit?", QtGui.QMessageBox.Yes,QtGui.QMessageBox.No)
        if reply == QtGui.QMessageBox.Yes:
            event.accept()            
        else:
            event.ignore()

app = QtGui.QApplication([])
qb = MessageBox()
qb.show()
app.exec_()

Above code executed in python console editor. it will run and open the form on top of qgis it functioning also good but after close the gui from qgis application not responde it will be idle not functioning any operation. Again close the qgis application and reopen it to do the further operation in code.

1 Answer 1

2

Take out the app = QtGui.QApplication([]) and app.exec_() lines.

You'd need those lines if you were running the script outside of QGIS as you'd need to create a QApplication, but QGIS already has this instance, so is unnecessary.

Edit: You also don't need the import sys statement unless you use it in some other code you haven't included.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.