如何将信号valueChanged从QLineEdit连接到Qt中的自定义插槽

fs_*_*gre 8 c++ qt signals-slots qlineedit

我需要以编程方式将QLineEdit中的valueChanged信号连接到自定义插槽.我知道如何使用Qt Designer进行连接并使用图形界面进行连接,但我想以编程方式进行连接,以便我可以了解有关信号和插槽的更多信息.

这就是我所做的不起作用.

.cpp文件

// constructor
connect(myLineEdit, SIGNAL(valueChanged(static QString)), this, SLOT(customSlot()));

void MainWindow::customSlot()
{
    qDebug()<< "Calling Slot";
}
Run Code Online (Sandbox Code Playgroud)

.h文件

private slots:
    void customSlot();
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么?

谢谢

vah*_*cho 20

QLineEdit似乎没有valueChanged信号,但textChanged(请参阅Qt文档以获取支持信号的完整列表).您还需要更改connect()函数调用.它应该是:

connect(myLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(customSlot()));
Run Code Online (Sandbox Code Playgroud)

如果您需要处理插槽中的新文本值,则可以将其定义为customSlot(const QString &newValue),因此您的连接将如下所示:

connect(myLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(customSlot(const QString &)));
Run Code Online (Sandbox Code Playgroud)

  • [这里是 Qt 5 的新连接方式,](https://wiki.qt.io/New_Signal_Slot_Syntax) `connect(myLineEdit, &amp;QLineEdit::textChanged, this, &amp;MainWindow::customSlot);` (2认同)