将QLineEdit的“enter”事件链接到插槽?

时间:2012-03-10 15:54:35

标签: python qt4 pyqt

我有以下代码:

def init_widgets(self):
        mainLayout = QtGui.QGridLayout()

        self.label1 = QtGui.QLabel("Enter a song name: ")
        self.search_lineEdit = QtGui.QLineEdit()
        self.search_button = QtGui.QPushButton("&Search") # QCommandLinkButton
        self.search_button.clicked.connect(self.search_slot)
        self.table = self.createTable()
        self.label2 = QtGui.QLabel("iQuality v1.00 by Itay Brandes")

        mainLayout.addWidget(self.label1, 0, 0)
        mainLayout.addWidget(self.search_lineEdit, 0, 1)
        mainLayout.addWidget(self.search_button, 0, 2)
        mainLayout.addWidget(self.table, 1, 0, 1, 0)
        mainLayout.addWidget(self.label2, 2, 0)

        self.setLayout(mainLayout)

如果用户按下self.search_slot上的输入按钮,如何运行self.search_lineEdit

1 个答案:

答案 0 :(得分:11)

QLineEdit有一个returnPressed信号。您可以将该信号从search_lineEdit连接到自定义插槽。

不熟悉PyQt语法,但应该是这样的:

 self.search_lineEdit.returnPressed.connect(self.search_slot)