Qt.Tool

241 views
Skip to first unread message

Fredrik Averpil

unread,
Aug 9, 2016, 7:53:37 AM8/9/16
to python_in...@googlegroups.com

I’m scratching my head here… why does this window when launched in Maya 2017 not stay on top of the main Maya window?

If you click outside this window, it’ll disappear behind the main Maya window.

I just want this window to behave like e.g. the hypershade or any other editor which floats atop Maya and doesn’t lock up the main Maya window in any way.

What did I do wrong?

from PySide2 import QtWidgets, QtCore

class ToolWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(ToolWindow, self).__init__()

        self.setWindowFlags(QtCore.Qt.Tool)

        self.widget = QtWidgets.QWidget()
        self.layout = QtWidgets.QVBoxLayout()
        self.button = QtWidgets.QPushButton('Yoghurt')
        self.layout.addWidget(self.button)
        self.widget.setLayout(self.layout)
        self.setCentralWidget(self.widget)

window = ToolWindow()
window.show()

// Fredrik

Fredrik Averpil

unread,
Aug 9, 2016, 8:06:48 AM8/9/16
to python_in...@googlegroups.com

Sorry… managed to copy-paste the wrong code where I didn’t parent to the MayaWindow. Still can’t make it to work though, so any input is greatly appreciated!

from PySide2 import QtWidgets, QtCore

def _maya_main_window():
    """Return Maya's main window"""
    for obj in QtWidgets.qApp.topLevelWidgets():
        if obj.objectName() == 'MayaWindow':
            return obj
    raise RuntimeError('Could not find MayaWindow instance')

class ToolWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):

        super(ToolWindow, self).__init__()

        self.setWindowFlags(QtCore.Qt.Tool)

        self.widget = QtWidgets.QWidget()
        self.layout = QtWidgets.QVBoxLayout()
        self.button = QtWidgets.QPushButton('Yoghurt'
)
        self.layout.addWidget(self.button)
        self.widget.setLayout(self.layout)
        self.setCentralWidget(self.widget)

window = ToolWindow(parent=_maya_main_window())
window.show()

Fredrik Averpil

unread,
Aug 9, 2016, 8:11:58 AM8/9/16
to python_in...@googlegroups.com

johan Borgström

unread,
Aug 9, 2016, 3:29:13 PM8/9/16
to Python Programming for Autodesk Maya
Hi Fredrik,

Just tried it on my mac and it worked fine when the Qt.Tool flag was set.

// Johan

Justin Israel

unread,
Aug 9, 2016, 3:35:27 PM8/9/16
to python_in...@googlegroups.com
You aren't actually passing the parent argument from your own constructor to the superclass constructor. Is that still a typo?
If it is a typo and you are properly passing the parent, try moving the Tool window flag into the superclass constructor call as well, instead of calling it afterwards. 

Justin 

On Wed, 10 Aug 2016, 12:11 AM Fredrik Averpil <fredrik...@gmail.com> wrote:
--
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/CAD%3DwhWNX6ksPR6O0rVCuOjbwPyCq_tpj-jQq66v0vgY3Z%2BU9cg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Fredrik Averpil

unread,
Aug 10, 2016, 1:50:36 AM8/10/16
to python_in...@googlegroups.com

Just tried it on my mac and it worked fine when the Qt.Tool flag was set

Hi Johan, yeah - that’s what I found was weird too. It works when the flag is set in Maya on OS X, but not in Maya on Windows.

You aren’t actually passing the parent argument from your own constructor to the superclass constructor. Is that still a typo?
If it is a typo and you are properly passing the parent, try moving the Tool window flag into the superclass constructor call as well, instead of calling it afterwards.

Hey Justin, hm… what typo were you referring to?
I mistakenly copy-pasted the wrong code in my initial email, if that's what you meant. The gist is here.

I tried also doing a super(ToolWindow, self).setWindowFlags(QtCore.Qt.Tool) but that didn’t change anything. Is that what you meant?

// Fredrik

Justin Israel

unread,
Aug 10, 2016, 2:17:22 AM8/10/16
to python_in...@googlegroups.com
On Wed, Aug 10, 2016 at 5:50 PM Fredrik Averpil <fredrik...@gmail.com> wrote:

Just tried it on my mac and it worked fine when the Qt.Tool flag was set

Hi Johan, yeah - that’s what I found was weird too. It works when the flag is set in Maya on OS X, but not in Maya on Windows.

You aren’t actually passing the parent argument from your own constructor to the superclass constructor. Is that still a typo?
If it is a typo and you are properly passing the parent, try moving the Tool window flag into the superclass constructor call as well, instead of calling it afterwards.

Hey Justin, hm… what typo were you referring to?
I mistakenly copy-pasted the wrong code in my initial email, if that's what you meant. The gist is here.

Look at your gist. Where are you actually passing that parent value to the QMainWindow constructor? I see you accepting it in your custom subclass but you don't do anything with it.
 

I tried also doing a super(ToolWindow, self).setWindowFlags(QtCore.Qt.Tool) but that didn’t change anything. Is that what you meant?

No I meant actually passing it into the QMainWindow constructor as a windowflag:


QMainWindow::QMainWindow(QWidget *parent = Q_NULLPTR, Qt::WindowFlags flags = Qt::WindowFlags())

I have seen it not work properly on OSX when you try and set it after the constructor via setWindowFlags() instead of at the time the constructor is called.

// 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.

Fredrik Averpil

unread,
Aug 10, 2016, 4:04:52 AM8/10/16
to python_in...@googlegroups.com

Ahhh, as always, spot on Justin :)

Thanks. This did the trick:

super(ToolWindow, self).__init__(parent)

Updated gist here:
https://gist.github.com/fredrikaverpil/b76bd91b9924435465ecb1b2270c1a9d

What’s weird is now that you must set the Qt.Tool window flag in OS X to make the window stay on top of Maya. In Windows, you don’t have to use this window flag…

// Fredrik

Justin Israel

unread,
Aug 10, 2016, 4:22:27 AM8/10/16
to python_in...@googlegroups.com


On Wed, 10 Aug 2016, 8:04 PM Fredrik Averpil <fredrik...@gmail.com> wrote:

Ahhh, as always, spot on Justin :)

Thanks. This did the trick:

Awesome. Glad it actually was the real code and not something even more complicated to debug :) 

super(ToolWindow, self).__init__(parent)

Updated gist here:
https://gist.github.com/fredrikaverpil/b76bd91b9924435465ecb1b2270c1a9d

What’s weird is now that you must set the Qt.Tool window flag in OS X to make the window stay on top of Maya. In Windows, you don’t have to use this window flag…

Pretty sure I have seen something like this. Could be the window system or platform specific qt defaults. Who knows. 

// 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.

Fredrik Averpil

unread,
Aug 12, 2016, 3:27:38 AM8/12/16
to python_in...@googlegroups.com

Managed to get help from Autodesk on this. Apparantly, the following code makes Maya cast some black magic on the window and bless it with being properly float on top of the main Maya window without the Qt.Tool flag:

self.setProperty("saveWindowPref", True)

Figured I’d just share my findings on this:
https://gist.github.com/fredrikaverpil/b76bd91b9924435465ecb1b2270c1a9d

In the gist there are various ways of making a cross-platform floating window. The code is for PySide2 but can easily be ported to PySide.

Cheers!

Justin Israel

unread,
Aug 12, 2016, 7:36:20 PM8/12/16
to python_in...@googlegroups.com
Thanks for this info. Part of their customizations to Qt I take it. 
The part about actually saving window positions... Is that between layout changes or something? I didn't think that would refer to Maya startup since your window wouldn't exist by the time Maya went to restore prefs. Unless it is more black magic that records the object name and catches the new window creation when you do it? 

--
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.

Fredrik Averpil

unread,
Aug 13, 2016, 4:05:57 AM8/13/16
to python_in...@googlegroups.com

Thanks for this info. Part of their customizations to Qt I take it.

Yeah, must be.

The part about actually saving window positions… Is that between layout changes or something? I didn’t think that would refer to Maya startup since your window wouldn’t exist by the time Maya went to restore prefs. Unless it is more black magic that records the object name and catches the new window creation when you do it?

I does record the object name and require you set one for Maya to remember its position. There’s definitively some “black magic” involved.

// Fredrik

Reply all
Reply to author
Forward
0 new messages