如何将 QLineEdit 的文本放入方法中?

How do I get the text of an QLineEdit into a method?

我仍然无法理解如何正确地将 Qt_pushButton 或 Qt_LineEdit 连接到方法。我会很高兴得到一个连我都真正理解的解释...

我用 Qt Designer 组合了一个非常基本的 UI。它包含一个名为“lineEditTest”的 lineEdit,我确实可以通过键入

来更改它
self.lineEditTest.setText("changed Text")

但是,我完全无法将用户输入的文本返回到我的程序中。我想自动将输入的文本提交到我的函数中,该函数将 var 设置为此值,并将其 returns 提交到我的 UI class 中。我猜 QLineEdit 的信号 editingFinished 听起来很完美?但是它不会传递用户输入到我的函数中的文本。

QLineEdit 确实有一个 属性 叫做“文本”对吧?所以对我来说,我只需要将另一个 arg - 除了 self - 传递给我的函数 text 似乎是合乎逻辑的。

我的代码看起来确实像这样,但显然根本行不通:

from PyQt5 import QtWidgets, uic
import sys


class Ui(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui, self).__init__()
        uic.loadUi('test.ui', self)

        self.lineEditTest.setText("test")
        self.lineEditTest.editingFinished.connect(self.changeText(text))
        self.show()

    def changeText(self):
        currentText = text
        print (currentText)
        return currentText

app = QtWidgets.QApplication(sys.argv)
window = Ui()
app.exec_()

这只会崩溃:

NameError: name 'text' is not defined`

您绑定回调的方式不正确。当你绑定一个回调时(这对于其他框架也是如此,而不仅仅是 PyQt 中的绑定回调),你想要绑定在某个事件发生时应该被触发的函数。那不是你在做什么 - 你明确地调用回调 self.changeText(text) (注意:括号调用函数)。因此,您正在调用(调用)该函数,对其求值并将 return 值放入 ...editingFinished.connect().

不要显式调用函数。只需传递函数的名称。如果您考虑一下,这是有道理的:我们将一个可调用对象传递给 connect - 这是应该在稍后的时间点调用的函数,当 editingFinished 事件发生时。

您也不需要将 lineEditTest 的文本传递到回调中,因为您可以通过 self.lineEditTest.text() 从回调内部获取小部件中的任何文本。

from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QMainWindow


class MainWindow(QMainWindow):

    def __init__(self):

        from PyQt5 import uic

        super(MainWindow, self).__init__()

        uic.loadUi("mainwindow.ui", self)

        self.lineEditTest.setPlaceholderText("Type something...")
        self.lineEditTest.editingFinished.connect(self.on_line_edit_finished_editing)

    @pyqtSlot()
    def on_line_edit_finished_editing(self):
        text = self.lineEditTest.text()
        print(f"You finished editing, and you entered {text}")


def main():
    from PyQt5.QtWidgets import QApplication

    application = QApplication([])
    window = MainWindow()

    window.show()

    return application.exec()


if __name__ == "__main__":
    import sys
    sys.exit(main())

问题似乎是OP不理解信号和槽的逻辑(我建议你检查here). The signals are objects that emit information, and the slots are functions (generally callable) that are connected to the signals to receive that information. And the information transmitted by the signal depends on each case, for example if you check the docs of editingFinished signal:

void QLineEdit::editingFinished()
This signal is emitted when the Return or Enter key is pressed or the line edit loses focus. Note that if there is a validator() or inputMask() set on the line edit and enter/return is pressed, the editingFinished() signal will only be emitted if the input follows the inputMask() and the validator() returns QValidator::Acceptable.

该信号不发送任何信息,因此不要指望收到任何信息,除非知道该版本已被用户终止。那么我怎样才能得到文本呢?嗯,通过QLineEdit的text()方法:

class Ui(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui, self).__init__()
        uic.loadUi("test.ui", self)

        self.lineEditTest.setText("test")
        self.lineEditTest.editingFinished.connect(self.changeText)
        self.show()

    def changeText(self):
        text = self.lineEditTest.text()
        print(text)

信号发送信息怎么办?然后这个连接的插槽(函数)将作为该信息的参数,例如,如果您使用每次更改 QLineEdit 的文本时发出的 textChanged 信号,它应该如下所示:

class Ui(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui, self).__init__()
        uic.loadUi("test.ui", self)

        self.lineEditTest.setText("test")
        self.lineEditTest.textChanged.connect(self.changeText)
        self.show()

    def changeText(self, text):
        print(text)
        # or
        # text = self.lineEditTest.text()
        # print(text)