QtGui closeEvent

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
easyw-fc
Veteran
Posts: 3633
Joined: Thu Jul 09, 2015 9:34 am

QtGui closeEvent

Post by easyw-fc »

Hi,
I'm doing a a macro with QtGui interface
I would need to trigger an action on closing the main QtGui windows

starting from the QtGui example
http://www.freecadweb.org/wiki/index.ph ... r_Examples
More Than 2 Buttons
I would need to trigger a message box on closing the main Qt windows (e.g. a warning message before closing it)

thank you
Maurice
wmayer
Founder
Posts: 20324
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: QtGui closeEvent

Post by wmayer »

Code: Select all

from PySide import QtGui
class MyWindow(QtGui.QWidget):
    def closeEvent(self, e):
        res = QtGui.QMessageBox.question(None,"Close","Do you want to close the window?",QtGui.QMessageBox.Yes|QtGui.QMessageBox.No)
        if res is QtGui.QMessageBox.No:
            e.ignore()

m=MyWindow()
m.show()
User avatar
easyw-fc
Veteran
Posts: 3633
Joined: Thu Jul 09, 2015 9:34 am

Re: QtGui closeEvent

Post by easyw-fc »

@wmayer
thanks a lot ... that was exactly I was looking for... :)

just an other question... is there a way to check if an other instance of the same QTGui window is running, so to avoid to double exec the widgets?

Maurice
wmayer
Founder
Posts: 20324
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: QtGui closeEvent

Post by wmayer »

just an other question... is there a way to check if an other instance of the same QTGui window is running, so to avoid to double exec the widgets?
In order to control this you have to implement the singleton pattern: http://python-3-patterns-idioms-test.re ... leton.html
User avatar
easyw-fc
Veteran
Posts: 3633
Joined: Thu Jul 09, 2015 9:34 am

Re: QtGui closeEvent

Post by easyw-fc »

thanx again!

very helpful forum and people! :)
Maurice
User avatar
easyw-fc
Veteran
Posts: 3633
Joined: Thu Jul 09, 2015 9:34 am

Re: QtGui closeEvent

Post by easyw-fc »

@wmayer
I tried singletone and it works if I launch my fuction e.g. twice in the same macro...
but my target was to avoid the possibility to execute the same macro invoked from e.g. FreeCAD toolbar...
thanks
Maurice
wmayer
Founder
Posts: 20324
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: QtGui closeEvent

Post by wmayer »

but my target was to avoid the possibility to execute the same macro invoked from e.g. FreeCAD toolbar...
:?: Don't quite understand what you want to achieve.
User avatar
easyw-fc
Veteran
Posts: 3633
Joined: Thu Jul 09, 2015 9:34 am

Re: QtGui closeEvent

Post by easyw-fc »

hi @wmayer,
e.g. I have configured in the tool bar a button for calling a QtGui macro e.g. Macro Clone Convert
http://www.freecadweb.org/wiki/index.ph ... oneConvert
I would like to avoid that the action to click on that macro button twice, will open two QtGui windows of the same macro

thanks
User avatar
easyw-fc
Veteran
Posts: 3633
Joined: Thu Jul 09, 2015 9:34 am

Re: QtGui closeEvent

Post by easyw-fc »

here the solution for a single instance
viewtopic.php?f=22&p=127978#p127954
Maurice
Post Reply