如何将信号值Changed从QLineEdit连接到Qt中的自定义插槽
How to connect the signal valueChanged from QLineEdit to a custom slot in Qt
我需要用程序将QLineEdit的valueChanged信号连接到自定义插槽。我知道如何使用Qt Designer进行连接,并使用图形界面进行连接,但我想以编程方式进行连接,这样我就可以了解更多关于信号和插槽的信息。
这就是我所拥有的不起作用的东西。
.cpp文件
// constructor
connect(myLineEdit, SIGNAL(valueChanged(static QString)), this, SLOT(customSlot()));
void MainWindow::customSlot()
{
qDebug()<< "Calling Slot";
}
.h文件
private slots:
void customSlot();
我在这里错过了什么?
感谢
QLineEdit
似乎没有valueChanged
信号,但有textChanged
信号(有关支持信号的完整列表,请参阅Qt文档)。您还需要更改connect()
函数调用。应该是:
connect(myLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(customSlot()));
如果您需要处理插槽中的新文本值,您可以将其定义为customSlot(const QString &newValue)
,因此您的连接将看起来像:
connect(myLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(customSlot(const QString &)));
如果感兴趣,这里还有lambda:
connect(myLineEdit, &QLineEdit::textChanged, [=](QString obj) { customSlot(obj); });
相关文章:
- QT 插槽未在主线程上调用
- Qt插槽调用了两次
- 当再次触发信号时,从Qt插槽执行的功能被第二次调用时会发生什么?
- 连接来自 C++98 接口的 Qt 插槽和信号,无需 Qt、STL 或 Boost
- 有没有办法调试排队等待对象的Qt插槽调用
- QT插槽未激活
- QT- QT插槽问题
- QT插槽和信号,显示第二形式/窗口
- QT插槽未在多线程DLL中执行
- 调试QT插槽未调用
- Qt插槽参数已损坏
- 如何使用Qt插槽中的QwtPplot绘制
- 如何将 QAction 从 QMenu 传递到 Qt 插槽
- Qt 插槽和信号语法
- 如何映射到重载Qt插槽
- Qt插槽,没有用于调用的匹配功能
- Qt插槽调用两次
- 调用未连接的Qt插槽.调用未连接的
- 从线程应用程序中的QtScript调用的Qt插槽
- QT插槽问题,链接错误