Search code examples
pythonpyqtmouseeventqlineeditqlabel

How to change a QLabel to a QLineEdit when mouse pressed?


I am trying to create a custom class so that whenever a mouse press event occurs, a QLabel should change to a QLineEdit. So, after searching, I found this code on a Qt website. But, unfortunately, it is not working. A user who posted this question had accepted this code as working, but I am new to Qt, as well as Python, so I need some help.

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.QRect(120, 10, 121, 20))
        self.buddyList.usernameInput.setText(self.buddyList.username.text())
        self.buddyList.usernameInput.selectAll()
        self.buddyList.usernameInput.show()
        self.connect(self.buddyList.usernameInput,QtCore.SIGNAL("returnPressed()"),self.editingFinish)

    def editingFinish(self):
        self.buddyList.usernameInput.hide()
        self.buddyList.username.show()
        self.buddyList.username.setText(self.buddyList.usernameInput.text())

Update:

The error I am getting is:

# Error: line 1: __init__() takes at least 2 arguments (1 given)
# Traceback (most recent call last):
#   File "<maya console>", line 22, in <module>
# TypeError: __init__() takes at least 2 arguments (1 given) # 

Solution

  • Here's an example of how to get a Qlabel to switch over to a QLineEdit. Alternatively you can use QLineEdit.setPlaceholderText() to show some default text when the field is left empty. This may be another option for you to use (and will be way more simple).

    from PySide import QtGui, QtCore
    
    # Make a custom label widget (mostly for its mousePressEvent)
    class BuddyLabel(QtGui.QLabel):
        def __init__(self, buddy, parent = None):
            super(BuddyLabel, self).__init__(parent)
            self.buddy = buddy
    
        # When it's clicked, hide itself and show its buddy
        def mousePressEvent(self, event):
            self.hide()
            self.buddy.show()
            self.buddy.setFocus() # Set focus on buddy so user doesn't have to click again
    
    class Window(QtGui.QWidget):
        def __init__(self, parent = None):
            super(Window, self).__init__(parent)
    
            # Create ui
            self.myEdit = QtGui.QLineEdit()
            self.myEdit.hide() # Hide line edit
            self.myEdit.editingFinished.connect(self.textEdited)
            self.myLabel = BuddyLabel(self.myEdit) # Create our custom label, and assign myEdit as its buddy
            self.myLabel.setText('Nothing has been entered')
            self.myLabel.setSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Fixed) # Change vertical size policy so they both match and you don't get popping when switching
    
            # Put them under a layout together
            hLayout = QtGui.QHBoxLayout()
            hLayout.addWidget(self.myLabel)
            hLayout.addWidget(self.myEdit)
    
            # Add a line edit with a place holder
            self.myEdit2 = QtGui.QLineEdit()
            self.myEdit2.setPlaceholderText('Nothing has been entered')
            self.setFocus() # By default this line edit may have focus and the place holder won't show up on load, so focus on the widget
    
            # Create main layout
            mainLayout = QtGui.QVBoxLayout()
            mainLayout.addLayout(hLayout)
            mainLayout.addWidget(self.myEdit2)
            self.setLayout(mainLayout)
    
            # Resize and show!
            self.resize(300, 200)
            self.show()
    
        def textEdited(self):
            # If the input is left empty, revert back to the label showing
            if not self.myEdit.text():
                self.myEdit.hide()
                self.myLabel.show()
    
    win = Window()