C++ 如何将 QLineEdit 中的信号 valueChanged 连接到 Qt 中的自定义插槽
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20585795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect the signal valueChanged from QLineEdit to a custom slot in Qt
提问by fs_tigre
I need to connect the valueChanged signal from QLineEdit to a custom slot programatically. I know how to do the connection by using Qt Designer and doing the connection with graphical interface but I would like to do it programmatically so I can learn more about the Signals and Slots.
我需要以编程方式将 QLineEdit 中的 valueChanged 信号连接到自定义插槽。我知道如何使用 Qt Designer 进行连接并使用图形界面进行连接,但我想以编程方式进行连接,以便我可以了解有关信号和插槽的更多信息。
This is what I have that doesn't work.
这是我所拥有的不起作用的东西。
.cpp file
.cpp 文件
// constructor
connect(myLineEdit, SIGNAL(valueChanged(static QString)), this, SLOT(customSlot()));
void MainWindow::customSlot()
{
qDebug()<< "Calling Slot";
}
.h file
.h 文件
private slots:
void customSlot();
What am I missing here?
我在这里缺少什么?
Thanks
谢谢
回答by vahancho
QLineEdit
does not seem to have valueChanged
signal, but textChanged
(refer to the Qt documentation for complete list of supported signals).
You need to change your connect()
function call too. It should be:
QLineEdit
似乎没有valueChanged
信号,但是textChanged
(有关受支持信号的完整列表,请参阅 Qt 文档)。您也需要更改connect()
函数调用。它应该是:
connect(myLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(customSlot()));
If you need the handle the new text value in your slot, you can define it as customSlot(const QString &newValue)
instead, so your connection will look like:
如果您需要处理槽中的新文本值,您可以将其定义为customSlot(const QString &newValue)
,因此您的连接将如下所示:
connect(myLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(customSlot(const QString &)));