PDA

View Full Version : Change QLabel to QLineEdit when mousePressEvent happens



naoyamakino
16th July 2009, 07:45
Hi there, I am looking for a way to change QLabel to QLineEdit when user clicks on it.
for example, MSN Messenger shows user name as label, and it changes to editable when user clicks on it; and it goes back to a label again when hit Enter.
is there any way to do that?
here is my code (this does not work, it does not change QLabel to QLineEdit when it detects mousePressEvent)


class class BuddyList(QtGui.QWidget):
def __init__(self,parent=None):
QtGui.QWidget.__init__(self, parent)
#some initialization...#
#username
self.username = LineDisplay(self,self)
self.username.setGeometry(QtCore.QRect(120, 10, 121, 20))

class LineDisplay(QtGui.QLabel):
def __init__(self,buddyList,parent=None):
QtGui.QLabel.__init__(self, parent)
self.buddyList = buddyList
def mousePressEvent(self, event):
self.buddyList.username = None
self.buddyList.usernameInput = QtGui.QLineEdit(self)
self.buddyList.usernameInput.setGeometry(QtCore.QR ect(120, 10, 121, 20))

does anyone have an idea? or do you see anything wrong in my code?
I would very much appreciate for any kind of input.

thank you for your consideration.

best regards
Naoya Makino

nish
16th July 2009, 08:36
just show and hide the label/lineedit on mouseclickevent and editingfinishied signals respectively

naoyamakino
16th July 2009, 18:37
Hi MrDeath, thank you for your reply.

yes it works!!
thank you so much!
here is my code for interests


class nameDisplay(QtGui.QLabel):
def __init__(self,buddyList,parent=None):
QtGui.QLabel.__init__(self, parent)
self.buddyList = buddyList
def mousePressEvent(self, event):
print 'mousePressEvent'
self.buddyList.username.hide()
self.buddyList.usernameInput = QtGui.QLineEdit(self.buddyList)
self.buddyList.usernameInput.setGeometry(QtCore.QR ect(120, 10, 121, 20))
self.buddyList.usernameInput.setText(self.buddyLis t.username.text())
self.buddyList.usernameInput.selectAll()
self.buddyList.usernameInput.show()
self.connect(self.buddyList.usernameInput,QtCore.S IGNAL("returnPressed()"),self.editingFinish)
def editingFinish(self):
self.buddyList.usernameInput.hide()
self.buddyList.username.show()
self.buddyList.username.setText(self.buddyList.use rnameInput.text())

regards