Using the loadUI Command

Qt's Designer tool can be used to interactively create graphical interfaces and save them out in a portable XML format. Maya provides the loadUI command which can be used by MEL and Python scripts to load these files into Maya and translate them, where possible, into standard Maya controls.

The following image demonstrates Qt Designer being used to create a simple dialog consisting of a combo-box, a text field, their associated labels, and a button box below with two buttons.

Assuming that the dialog is saved out to the file /home/autodesk/createDialog.ui, then it can be loaded into Maya and displayed using the following MEL commands:

string $dialog = `loadUI -uiFile "/home/autodesk/createDialog.ui"`;
showWindow $dialog;

Or, in Python:

import maya.cmds as cmds
dialog = cmds.loadUI(uiFile='/home/autodesk/createDialog.ui')

The loadUI command creates the widgets specified in the file by executing their corresponding Maya commands. For example, the two labels in the example above are created using Maya's text command, the comboBox control is created using the optionMenu command, and the input field is created with the textField command. As a result, those controls can be subsequently manipulated in MEL and Python scripts as if they had been created in the normal way.

Flags can be passed to the command used to create a control by adding dynamic properties to the widget within Qt Designer. The name of the property should match that of the flag, including the leading hyphen, for example -width. If you want the control to be created using Python rather than MEL, then replace the leading hyphen with a + instead, for example, +width. If you specify both MEL and Python flag properties for the same widget, the behavior is undefined and therefore may result in errors. If the flag takes one or more arguments, specify them as the property value.

The image below shows the -changeCommand property being added to the comboBox in Qt Designer.

When the corresponding optionMenu control is created, its -changeCommand flag will be set to execute the command string "newObjectType()".

If the loadUI command encounters a Qt widget for which there is no equivalent Maya control, the Qt widget is still created, but you cannot access it from MEL or Python scripts. An example of this is the button box at the bottom of the dialog. It uses a QDialogButtonBox widget for which there is no equivalent control in Maya. As a result, there is no access to either the button box itself or the buttons within it. Had the buttons been placed on the dialog individually, without using the button box, then the loadUI command would have created them using the Maya button commands and they would have been accessible to scripts.

Any layouts in the file are created as generic layouts and are only accessible within Maya using the layout command, not the commands for specific types of layouts such as formLayout, columnLayout, and so forth.

The name given to a control or layout within Maya is that specified for its objectName property in Qt Designer. The top-level window itself must have a unique name, though, so if the specified name is already in use, the loadUI command will add a number to the end of it to make it unique.