Cursor select contents of a textField

Hey there I have a rename script with a custom UI. The textField is populated with the current selection. It would be handy to have this text selected on generation of the UI as usually I would overwrite the name.
I don’t see a command to select contents in ‘textField’. ‘setFocus’ doesn’t throw up anything either.
Basically, my script pops up and I want something to trigger ‘ctrl+a’ to select the contents. Am I overlooking something simple ?
Thanks Alan.

Hello,
I am not sure this is possible with Maya cmds API. But it is definitely possible with Qt, here is an example:

from PySide2 import QtWidgets
import maya.OpenMayaUI as mui
import shiboken2


class TestWindow(QtWidgets.QDialog):
	def __init__(self, parent=None):
		super(TestWindow, self).__init__(parent)

		main_layout = QtWidgets.QVBoxLayout()

		textfield = QtWidgets.QLineEdit()
		textfield.setText("bonjour")
		textfield.selectAll()
		
		main_layout.addWidget(textfield)

		self.setLayout(main_layout)


def getMayaWindow():
	ptr = mui.MQtUtil.mainWindow()
	return shiboken2.wrapInstance(int(ptr), QtWidgets.QWidget)


def main():
	global ui
	ui = TestWindow(getMayaWindow())
	ui.show()


if __name__ == "__main__":
	main()

Hope you find this useful :slight_smile:

Cheers !

2 Likes

Thank you for taking the time to respond, that is amazing. Really appreciate that. I was going to trigger at command line utility to do a keystroke! (ctrl+a).

Very hacky but who cares for personal workflow!
Using a utility that can trigger key press… so this runs just after UI is shown in MEL.

//trigger c+a  using hotkey p to select all text as no other way to do it.  
system("call \"%USERPROFILE%/Programs/HotkeyP/HotkeyP.exe\" -macro \\ctrldown\\A\\ctrlup");
1 Like