Search code examples
pythonqttextfieldmaya

Maya 2018 Python+QT: Querying Value of Imported TextField


I've been following This Tutorial on using a .UI file from QT designer in a Maya plugin. It states that, in order to query the value of a QtextEdit field after the UI has been loaded into Maya, I need to do the following:

So now when we load our QT Ui inside of maya we can query the text of our line edit every time we want to by using the following line of code:

pm.textField('textFieldName', query = True, text = True)

However I can't seem to get this to function. I'm loading the UI as follows:

# Load our window and put it into a variable.
ebWin = cmds.loadUI(uiFile = self.BE_UIpath)

No issues there, when I try cmds.showWindow(ebWin), everything works and looks exactly as intended. Now, when I try to query the QtextEdit I've named 'exportDirectoryTF', Maya insists it does not exist. I've tried two different approaches:

approach A:

# Connect Functions to the buttons.
exportDir = ebWin.textField('exportDirectoryTF', query = True, text = True)

which outputs:

# Error: 'unicode' object has no attribute 'textField'
# # Traceback (most recent call last):
# #   File "C:/Users/Censored/Documents/maya/2018/plug-ins/EB_pi_cmds.py", line 39, in doIt
# #     exportDir = ebWin.textField('exportDirectoryTF', query = True, text = True)
# # AttributeError: 'unicode' object has no attribute 'textField'

and approach B:

import maya.cmds as cmds
# Connect Functions to the buttons.
exportDir = cmds.textField('exportDirectoryTF', query = True, text = True)

which returns:

# RuntimeError: Object 'exportDirectoryTF' not found.
# # Traceback (most recent call last):
# #   File "C:/Users/Censored/Documents/maya/2018/plug-ins/EB_pi_cmds.py", line 39, in doIt
# #     exportDir = cmds.textField('exportDirectoryTF', query = True, text = True)
# # RuntimeError: Object 'exportDirectoryTF' not found. # 

The tutorial has 'pm.textField('textFieldName', q = True, text = True)', and I can't figure out where the "pm" came from, if it is supposed to indicate the variable from loading the UI or the maya Python textField command, or neither.

If anyone could point me in the right direction here, it would be greatly appreciated.


Solution

  • From your code it is not visible at which time you try to execute the textField cmd. This code below works fine for me. The test.ui only contains a widget with a lineEdit field called "lineEdit". Querying the text field only works if the window is visible. If you close the window and try to query the text field, you get the "object not found" error.

    ui = "D:/temp/test.ui"
    qtW = cmds.loadUI(uiFile = ui)
    cmds.showWindow(qtW)
    cmds.textField("lineEdit", query=True, text=True)