按下鼠标时如何将 QLabel 更改为 QLineEdit?

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

我正在尝试创建自定义 class 以便每当发生鼠标按下事件时,QLabel 应更改为 QLineEdit。所以,经过搜索,我在一个 Qt 网站上找到了这段代码。但是,不幸的是,它不起作用。发布此问题的用户已接受此代码作为工作,但我是 Qt 的新手,也是 Python,所以我需要一些帮助。

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())

更新:

我得到的错误是:

# 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) # 

这是一个如何让 Qlabel 切换到 QLineEdit 的示例。或者,您可以使用 QLineEdit.setPlaceholderText() 在字段留空时显示一些默认文本。这可能是您使用的另一种选择(并且会更简单)。

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()