【问题标题】:How to connect the signal valueChanged from QLineEdit to a custom slot in Qt如何将信号 valueChanged 从 QLineEdit 连接到 Qt 中的自定义插槽
【发布时间】:2014-01-02 08:39:22
【问题描述】:

我需要以编程方式将 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();

我在这里错过了什么?

谢谢

【问题讨论】:

    标签: c++ qt signals-slots qlineedit


    【解决方案1】:

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

    connect(myLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(customSlot()));
    

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

    connect(myLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(customSlot(const QString &)));
    

    【讨论】:

    • 将 valueChanged 更改为 textChanged 并将静态 QString 更改为 const QString & 并且有效。我不知道我是怎么错过的,特别是静态 QString(哇),非常感谢。也非常感谢第二个例子,因为我也想知道参数的用法。非常感谢
    • Here's Qt 5's new way to connect,connect(myLineEdit, &amp;QLineEdit::textChanged, this, &amp;MainWindow::customSlot);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    • 2013-01-10
    • 1970-01-01
    相关资源
    最近更新 更多