Results 1 to 3 of 3

Thread: Change QLabel to QLineEdit when mousePressEvent happens

  1. #1
    Join Date
    Jul 2009
    Posts
    14
    Thanks
    1
    Qt products
    Platforms
    MacOS X Windows

    Default Change QLabel to QLineEdit when mousePressEvent happens

    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)

    Qt Code:
    1. class class BuddyList(QtGui.QWidget):
    2. def __init__(self,parent=None):
    3. QtGui.QWidget.__init__(self, parent)
    4. #some initialization...#
    5. #username
    6. self.username = LineDisplay(self,self)
    7. self.username.setGeometry(QtCore.QRect(120, 10, 121, 20))
    8.  
    9. class LineDisplay(QtGui.QLabel):
    10. def __init__(self,buddyList,parent=None):
    11. QtGui.QLabel.__init__(self, parent)
    12. self.buddyList = buddyList
    13. def mousePressEvent(self, event):
    14. self.buddyList.username = None
    15. self.buddyList.usernameInput = QtGui.QLineEdit(self)
    16. self.buddyList.usernameInput.setGeometry(QtCore.QRect(120, 10, 121, 20))
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by naoyamakino; 16th July 2009 at 07:52.

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Change QLabel to QLineEdit when mousePressEvent happens

    just show and hide the label/lineedit on mouseclickevent and editingfinishied signals respectively

  3. #3
    Join Date
    Jul 2009
    Posts
    14
    Thanks
    1
    Qt products
    Platforms
    MacOS X Windows

    Default Re: Change QLabel to QLineEdit when mousePressEvent happens

    Hi MrDeath, thank you for your reply.

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

    Qt Code:
    1. class nameDisplay(QtGui.QLabel):
    2. def __init__(self,buddyList,parent=None):
    3. QtGui.QLabel.__init__(self, parent)
    4. self.buddyList = buddyList
    5. def mousePressEvent(self, event):
    6. print 'mousePressEvent'
    7. self.buddyList.username.hide()
    8. self.buddyList.usernameInput = QtGui.QLineEdit(self.buddyList)
    9. self.buddyList.usernameInput.setGeometry(QtCore.QRect(120, 10, 121, 20))
    10. self.buddyList.usernameInput.setText(self.buddyList.username.text())
    11. self.buddyList.usernameInput.selectAll()
    12. self.buddyList.usernameInput.show()
    13. self.connect(self.buddyList.usernameInput,QtCore.SIGNAL("returnPressed()"),self.editingFinish)
    14. def editingFinish(self):
    15. self.buddyList.usernameInput.hide()
    16. self.buddyList.username.show()
    17. self.buddyList.username.setText(self.buddyList.usernameInput.text())
    To copy to clipboard, switch view to plain text mode 

    regards

Similar Threads

  1. Replies: 5
    Last Post: 10th July 2009, 12:11
  2. Pointer Question related to QLineEdit
    By ChrisReath in forum Qt Programming
    Replies: 1
    Last Post: 23rd May 2008, 15:13
  3. Change color of a link in QLabel using Style Sheets?
    By codeslicer in forum Qt Programming
    Replies: 2
    Last Post: 15th April 2008, 11:00
  4. Connect QLabel and QLineEdit
    By abbapatris in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2008, 19:00
  5. Replies: 1
    Last Post: 26th November 2006, 09:32

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.