Accessing Qt and Maya controls

Controls created directly using Qt are generally not recognized by Maya's UI commands.

They are not listed by the isUI command, and the commands for specific types of controls, such as the button command, will not recognize them. Commands such as button only recognize controls they created.

However, if setObjectName() was used to give the Qt control a unique name, you can use the control command to test for the existence of the control and perform basic operations on the control.

For example, if you assign your button a unique name:

   button = new MyAction("Perform Action");
   button->setObjectName("myActionButton");

then you can use the control command to hide the button:

if (`control -q -exists myActionButton`) {
   control -e -visible false myActionButton;
}

You can use Qt to access UI elements created using Maya commands.

The MQtUtil C++ class provides methods for retrieving the Qt object underlying a Maya control, layout, window, or menu item.

For example, if you created a Maya checkBox control named myCheckBox using the checkBox command, you can use MQtUtil::findControl() class to retrieve the checkBox's QWidget pointer and cast it appropriately.

QWidget* control = MQtUtil::findControl("myCheckBox");
if (control) {
   QCheckBox* cb = qobject_cast<QCheckBox*>(control);
}

Important: If your control's type does not match the type you cast it to, the cast operation will return nullptr.

You can then use the QWidget pointer to access any of the QCheckBox's properties, or call any method on the QCheckBox:

if (cb) {
    if (cb->isChecked()) {
        MGlobal::displayInfo("myCheckBox is checked");
      } else {
          MGlobal::displayInfo("myCheckBox is not checked");
      }
   }