Sunday, December 7, 2014

PyQt (4): QLineEdit

QLineEdit is a widget using to create a one-line text editor.

Case 1: 
The QLineEdit is used to be a user input area

Example:
from PyQt4 import QtGui, QtCore

# create main widget and its layout
mainWidget = QtGui.QWidget()
mainLayout = QtGui.QHBoxLayout()
mainWidget.setLayout(mainLayout)

# create a label
label = QtGui.QLabel('PrintOut Button: ')
# fine-tune the appearance of aligned text
label.setAlignment(QtCore.Qt.AlignCenter)

# create text area
textLine = QtGui.QLineEdit()
# set text when the text area is not activated
textLine.setPlaceholderText('please input something here!')

# create a button
btn = QtGui.QPushButton('Print')

# create button click function to print out what input in text line
def clickFunc():
    print str(textLine.text())

# create signal for the button
# when click the button, excute the click function
btn.clicked.connect(clickFunc)

# add ui elements to the main layout
mainLayout.addWidget(label)
mainLayout.addWidget(textLine)
mainLayout.addWidget(btn)

# show the main widget
mainWidget.show()
Case 2: 
The QLineEdit is used to be a display area and do not allow user to edit

Example:
from PyQt4 import QtGui, QtCore
import maya.cmds as mc

mainWidget = QtGui.QWidget()
mainLayout = QtGui.QHBoxLayout()
mainWidget.setLayout(mainLayout)

# create a label
label = QtGui.QLabel('File Path: ')
# fine-tune the appearance of aligned text
label.setAlignment(QtCore.Qt.AlignCenter)

# create text area
textLine = QtGui.QLineEdit()
# set text when the text area is not activated
textLine.setText('C:/test/test.py')
# set the text is read only, so that user still can copy text from text area
textLine.setReadOnly(True)

# add ui elements to the main layout
mainLayout.addWidget(label)
mainLayout.addWidget(textLine)

# show the main widget
mainWidget.show()

No comments:

Post a Comment