removing a Qt instance after an error and window closed

131 views
Skip to first unread message

Benjam901

unread,
Dec 9, 2015, 10:48:28 AM12/9/15
to Python Programming for Autodesk Maya
Hello all,

Last week I finally figured out how to successfully stop multiple Qt window classed from being open inside Maya.

I checked the window existed and if it did I ignore and pass on the condition.

I overrode the close event for my class and when the window was closed the class was deleted. This allowed the window to be re-opened which was exactly what I was after...however.

When an error occurs in the script and I close the window to re-boot it, regardless of the close event override the script says the window still exists.

Is this the window being stuck in memory because of the error in the script?

The below code seems to work brilliantly except for when an error occurs. When this happens I close the window and try to re-open it but the "already exists" condition pops up and I have to re-start Maya

Code is below:

def main():
	winName = 'EXPORTER'
	if pm.windows.window(winName, exists=True):
		print 'Already exists, passing'
		pass
	else:
		window = ColladaExporterUI()
		window.setObjectName(winName)
		window.show()

def closeEvent(self, event):
	print 'CLOSED'
	del self.mainDialog
	del self
	event.accept()

Fredrik Averpil

unread,
Dec 9, 2015, 12:28:58 PM12/9/15
to Python Programming for Autodesk Maya
When you get that error, do you see the "CLOSED" print message at all?


Regards,
Fredrik

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/dcd4ecdf-ac70-411c-8ef9-d32337b96e89%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ben Hearn

unread,
Dec 9, 2015, 12:45:45 PM12/9/15
to python_in...@googlegroups.com
Hey Fredrik,

Yes I see the CLOSED print statement when I close the dialog

- Ben
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/ilAU64vmbfw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAD%3DwhWPbnGYCx4GjfZ9j1E-tdsTCsMHhCXXac2RNgDUGosshPg%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.


--

Tel - +46 76245 92 90 (Sweden)

Marcus Ottosson

unread,
Dec 9, 2015, 1:17:23 PM12/9/15
to python_in...@googlegroups.com
If you provide something (minimal) that we could run, it would be much easier to help you track down the issue.


For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Justin Israel

unread,
Dec 9, 2015, 1:35:42 PM12/9/15
to python_in...@googlegroups.com

Don't use the del keyword to clean up your windows. That just deletes python objects and can't guarantee it will delete qt objects. Use QObject.deleteLater()

If you make sure "self.mainDialog" is parented to "self" then you only need to call "self.deleteLater()". Qt will make sure any children of a deleted object are also deleted.

You can also achieve this result without needing a custom closeEvent, if you set a widget attribute:

self.setAttribute(QtCore.Qt.WA_DeleteOnClose)

This could either be done in your constructor, or outside the class by a caller than wants the object it is creating to be deleted when it is closed.


Fredrik Averpil

unread,
Dec 9, 2015, 1:44:56 PM12/9/15
to python_in...@googlegroups.com
I second using QObject.deleteLater() and was just about to recommend that as you can see the "CLOSED" print message. 

Cheers,
Fredrik


Marcus Ottosson

unread,
Dec 9, 2015, 3:02:27 PM12/9/15
to python_in...@googlegroups.com
Alternative to what Justin and Fredrik said, also consider whether you *need* to delete your window. Odds are you can simply hide/show it, which would save you some hassle and potentially improve startup time on multiple runs.


For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Justin Israel

unread,
Dec 9, 2015, 3:08:36 PM12/9/15
to python_in...@googlegroups.com
On Thu, Dec 10, 2015 at 9:02 AM Marcus Ottosson <konstr...@gmail.com> wrote:
Alternative to what Justin and Fredrik said, also consider whether you *need* to delete your window. Odds are you can simply hide/show it, which would save you some hassle and potentially improve startup time on multiple runs.

That is a very valid point. If it isn't expensive to keep around, might as well just keep it and show it again.
 

Ben Hearn

unread,
Dec 10, 2015, 4:41:19 AM12/10/15
to python_in...@googlegroups.com
I need to close and re-open as the boot runs some checks and such on boot but I suppose I could run the setup on show, I will run some tests thanks for the tip.
The QObject.deleteLater() worked like an absolute charm thanks for the heads up!


For more options, visit https://groups.google.com/d/optout.

Ben Hearn

unread,
Dec 10, 2015, 4:49:42 AM12/10/15
to python_in...@googlegroups.com
I also tried Justins solution by parenting my window to self. This however made sure the window would not show up. The delete later was called on the close event which for me is fine for the moment however is it generally recommended to avoid custom events if you can?

Justin Israel

unread,
Dec 10, 2015, 1:25:05 PM12/10/15
to python_in...@googlegroups.com

Its not a problem to have custom events. It was just a suggestion for how to properly delete objects in Qt. The WA_deleteonclose basically does the same thing, where it calls deleteLater() in a close event


Reply all
Reply to author
Forward
0 new messages