0

I use Qt Designer to build my GUI's and convert them to py files using pyuic5. My end goal here is to interrupt the user from closing the program when a variable == 1 and present them with an 'are you sure you want to close?' type dialog. If said variable == 0 then just close the program normally.

I have seen lots of examples on how to do this, but all of them require editing the code in the GUI module. I import my gui.py file created by pyuic5 into my main script where I do all my connections to buttons, line edits, etc.. I do this so that at anytime I can update the GUI with Qt Designer and not affect the programs functionality.

Is there a way to do this from my main script that has the GUI module from Qt Designer imported?

Example of how my main script is structured:

import philipsControlGui
import sys

def main():
    MainWindow.show()
    sys.exit(app.exec_())

def test():
    print('test')

# Main window setup
app = philipsControlGui.QtWidgets.QApplication(sys.argv)
MainWindow = philipsControlGui.QtWidgets.QMainWindow()
ui = philipsControlGui.Ui_MainWindow()
ui.setupUi(MainWindow)

# Main window bindings
ui.onButton.clicked.connect(test)
### Can I insert something here to do: if user closes the window... do something else instead?

if __name__ == "__main__":
    main()
ryn1x
  • 962
  • 1
  • 7
  • 17
  • You need to override the closeEvent method (as you probably already know from the lots of examples). I don't know if you can do this in Qt Designer. – Trilarion Nov 16 '16 at 15:53

2 Answers2

1

You should create a subclass from your imported gui so you can reimplement the closeEvent method:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from philipsControlGui import Ui_MainWindow

class MainWindow(QtWidgets.QMainWindow):    
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setUpUi(self)
        self.ui.onButton.clicked.connect(self.test)
        self._check_close = True

    def test(self):
        print('test')
        self._check_close = not self._check_close

    def closeEvent(self, event):
        if self._check_close:
            result = QtWidgets.QMessageBox.question(
                self, 'Confirm Close', 'Are you sure you want to close?',
                QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
            if result == QtWidgets.QMessageBox.Yes:
                event.accept()
            else:
                event.ignore()

def main():
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

if __name__ == '__main__':

    main()
ekhumoro
  • 107,367
  • 18
  • 208
  • 308
-1

If there's a specific 'ExitButton' in your design, you should be able to connect it in the main code and create a pop up dialog. You would have to import the QtCore/QtGui components. I always write my GUI directly (QtDesigner is pain when it comes to these things) so I'm assuming something like this:

 from PyQt4.QtGui import *
 from PyQt4.QtCore import *

[YOUR CODE]

ui.ExitButton.clicked.connect(Exit)

def Exit():

  msg = QMessageBox()
  msg.setIcon(QMessageBox.Information)

  msg.setText("Are you sure you want to close this window?")
  msg.setWindowTitle("MessageBox demo")
  msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
  msg.buttonClicked.connect(msgbtn)
  retval = msg.exec_()
  print "value of pressed message box button:", retval
avelampudi
  • 296
  • 2
  • 6
  • 15
  • Thanks. I do have a Exit Action Item that I could connect that way, but how would I ensure the same result if the user uses the 'X' button on the window's frame? – ryn1x Nov 16 '16 at 19:19
  • There's a main window CloseEvent that you can control. Take a look at this: http://stackoverflow.com/questions/22460003/pyqts-qmainwindow-closeevent-is-never-called – avelampudi Nov 16 '16 at 20:16
  • Another fool proof way to make sure the user doesn't do that is to make your GUI full screen with - mainWindow.showFullScreen() – avelampudi Nov 16 '16 at 20:23