Hi,

I would like to show a widget to the user when a "hotkey" is pressed. I 
have done some tests and found three ways to do this.

* Using the Hotkey editor, add a hotkey for a custom runtime command.

* Install an eventfilter to the main maya window and intercept a specific 
keypress.

* Add a QAction (with a "key shortcut") to the maya main window.


Feels like the the first approach is more flexible for the user as they can 
choose how to call the script with the hotkey editor. And the other two 
feels a bit "invasive".

To run this I create a hotkey for a custom script and add a runtime command.

import path.to.module.custom_widget
custom_widget.show_widget()

Below you will find a snippet of a stripped version of the script.

from PySide2 import QtGui
from PySide2 import QtCore
from PySide2 import QtWidgets
from shiboken2 import wrapInstance 
from maya import OpenMayaUI as omui

class CustomWidget(QtWidgets.QWidget):

    def __init__(self, parent=None):
        super(CustomWidget, self).__init__(parent)

        self.setWindowFlags(QtCore.Qt.Popup|QtCore.Qt.FramelessWindowHint)

        vbox = QtWidgets.QVBoxLayout(self)
        vbox.setContentsMargins(0,0,0,0)

        self.line_edit = QtWidgets.QLineEdit()
        vbox.addWidget(self.line_edit)

        btn = QtWidgets.QPushButton('Button')
        btn.clicked.connect(self.button_clicked)
        vbox.addWidget(btn)

    def button_clicked(self):
        print(self.line_edit.text())
        self.close()
        
    def show_at_mouse(self):
        self.move(QtGui.QCursor.pos())
        self.show()


def show_widget():

    global widget
    mainWin = wrapInstance(long(omui.MQtUtil.mainWindow()), 
QtWidgets.QWidget)

    try:
        widget.show_at_mouse()

    except NameError:
        widget = CustomWidget(mainWin)
        widget.show_at_mouse()


The reason for using a global variable for the widget is that I would like 
to keep whatever state the user has set in the widget(in this simple 
example only the text in the line edit)

I would like to get some feedback on this subject, are there some best 
practices that I am missing, other ways to do this. Any constructive 
feedback is much appreciated!

Regards,
Johan

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/560eea0a-3817-4743-bfde-15e0bd2ff94fn%40googlegroups.com.

Reply via email to