Retrieve text from a PySide QLineEdit

Retrieve text from a PySide QLineEdit

Anonymous
Not applicable
4,716 Views
2 Replies
Message 1 of 3

Retrieve text from a PySide QLineEdit

Anonymous
Not applicable

I am new to PySide and am experimenting with it in Autodesk Maya.
I have a text code from Chris Zurbrigg. I am getting a good handle of how to setup a UI, but I am running into one major problem.

How do I retrieve the text from a QLineEdit outside of the class?

Ex:
If the QLineEdit is for a username.
How do I retrieve the username text, in a function, outside of this UI?

Here is the code:----------------------------------------------------------------------------------

import traceback
from PySide import QtCore
from PySide import QtGui
from shiboken import wrapInstance
import maya
.cmds as cmds
import maya
.OpenMayaUI as omui


def maya_main_window
():
    '''
    Return the Maya main window widget as a Python object
    '''
    main_window_ptr = omui.MQtUtil.mainWindow()
    return wrapInstance(long(main_window_ptr), QtGui.QWidget)

class TestUi(QtGui.QDialog😞
    test_signal = QtCore.Signal()
    
    def __init__
(self, parent=maya_main_window()):
        super(TestUi, self).__init__(parent)
        
    def create
(self😞
        '''
        Create the UI
        '''
        self.setWindowTitle("TestUi")
        self.setWindowFlags(QtCore.Qt.Tool)
        
        self
.create_controls()
        self.create_layout()
        self.create_connections()
        
    def create_controls
(self😞
        '''
        Create the widgets for the dialog
        '''
        self.push_button = QtGui.QPushButton("QPushButton")
        self.check_box_01 = QtGui.QCheckBox("QCheckBox 01")
        self.check_box_02 = QtGui.QCheckBox("QCheckBox 02")
        
        txt
=[]
        self.line_edit = QtGui.QLineEdit()
        self.line_edit.setPlaceholderText('Enter Name')
        self.line_edit.setToolTip('Enter Your Name in the Text Field.')
        self.line_edit.setObjectName('testLineEdit')
        
        self
.close_btn = QtGui.QPushButton("Close")
        self.close_btn.clicked.connect(self.close)
        
    def create_layout
(self😞
        '''
        Create the layouts and add widgets
        '''
        check_box_layout = QtGui.QHBoxLayout()
        check_box_layout.setContentsMargins(2, 2, 2, 2)
        check_box_layout.addWidget(self.check_box_01)
        check_box_layout.addWidget(self.check_box_02)
        
        
        main_layout 
= QtGui.QVBoxLayout()
        main_layout.setContentsMargins(6, 6, 6, 6)
        
        main_layout
.addWidget(self.line_edit)
        main_layout.addWidget(self.close_btn)
        main_layout.addStretch()
        
        self
.setLayout(main_layout)
                
    def create_connections
(self😞
        '''
        Create the signal/slot connections
        '''
        self.line_edit.editingFinished.connect(self.on_text_changed)
        
    
#--------------------------------------------------------------------------
    # SLOTS
    #--------------------------------------------------------------------------
        
    def on_text_changed
(self😞
        print("Text changed")
        print self.line_edit.text()

    
if __name__ 
== "__main__":
    
    
# Development workaround for PySide winEvent error (Maya 2014)
    # Make sure the UI is deleted before recreating
    try:
        test_ui.deleteLater()
    except:
        pass
    
    
# Create minimal UI object
    test_ui = TestUi()
    
    
# Delete the UI if errors occur to avoid causing winEvent
    # and event errors (in Maya 2014)
    try:
        test_ui.create()
        test_ui.show()
    except:
        test_ui.deleteLater()
        traceback.print_exc()

0 Likes
Accepted solutions (1)
4,717 Views
2 Replies
Replies (2)
Message 2 of 3

cheng_xi_li
Autodesk Support
Autodesk Support
Accepted solution

Hi Nathan,

 

You can add a method in your TestUi class to expose the string like below:

 

    def getLineText(self):
        return self.line_edit.text()

If you want to find your window and get its text, you'll need to set an object name for your window.  You can add following code in the create function

 

        self.setObjectName('testWindow')

Then, you can use following code to find the window and get the text

 

        mayaMainWindow = maya_main_window()
        testWindow = mayaMainWindow.findChild(TestUi, "testWindow")
        print testWindow.getLineText()

Yours,

Li

 

 

Message 3 of 3

Anonymous
Not applicable

Awesome!  Just what I was looking for.

 

Thank you!

0 Likes