How to connect the signal valueChanged from QLineEdit to a custom slot in Qt

fs_tigre picture fs_tigre · Dec 14, 2013 · Viewed 27.9k times · Source

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.

This is what I have that doesn't work.

.cpp file

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

void MainWindow::customSlot()
{
    qDebug()<< "Calling Slot";
}

.h file

private slots:
    void customSlot();

What am I missing here?

Thanks

Answer

vahancho picture vahancho · Dec 14, 2013

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:

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:

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