Reading text from a QLineEdit with Python

In my ongoing quest to work with Qt to build UIs, I have a hard time figuring out how to read text from a QLineEdit. I built my interface with QtDesigner, added a QLineEdit and gave it the name “txtMyText”.

What’s the correct way to query the text from this QLineEdit. I assumed I could do something like

cmds.textField("txtMyText", query = True, text = True)

but that gives me an error:

# RuntimeError: Object 'txtMyText' not found.

Which, when you think about it, makes sense since a QLineEdit is not the same as a textField.

But how then do you find the QLineEdit “txtMyText” from a script?

Any help is appreciated!

Ack, why do I always find a solution after I post here… ? :slight_smile:

Anyhoo, the solution I’m using now is the following:

My imports and global ui variable


from maya import OpenMayaUI as omui
from PySide.QtCore import * 
from PySide.QtGui import * 
from PySide.QtUiTools import *
from shiboken import wrapInstance
from stringBuilder import *

import os
import maya.cmds as cmds
import pymel.core as pm

ui = "thisIsNothingForNow"


To create the UI I use this function



def CreateUI():
	mayaMainWindowPtr = omui.MQtUtil.mainWindow()
	mayaMainWindow = wrapInstance(long(mayaMainWindowPtr), QWidget)

	loader = QUiLoader()
	file = QFile("D:/Dropbox/QtFiles/checkupScriptUI.ui")
	file.open(QFile.ReadOnly)

	global ui

	ui = loader.load(file, parentWidget = mayaMainWindow)
	file.close()


Then in the function I want to use the entered text I do the following:



def TEST():
	global ui
	words = ui.txtMyText.text()
	
	print words

I would recommend reading the Qt Documentation when you have specific questions about a particular widget. You can often find examples in the description covering basic usage.
The docs are in C++, so it takes a little effort to translate to python.

http://qt-project.org/doc/qt-4.8/qlineedit.html

This PyQt documentation is also a good source for answers. Does it pretty well for me. It is not as complete as the c++ one but close enough.

http://pyqt.sourceforge.net/Docs/PyQt4/modules.html