13

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

fs_tigre
  • 10,650
  • 13
  • 73
  • 146

2 Answers2

27

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 &)));
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
vahancho
  • 20,808
  • 3
  • 47
  • 55
  • Changed valueChanged to textChanged and static QString to const QString & and worked. I don't know how I missed that, specially static QString (wow), thanks a lot. Also thanks a lot for the second example since I was also wondering about the usage of the parameter. Thanks a LOT – fs_tigre Dec 14 '13 at 17:38
  • 2
    [Here's Qt 5's new way to connect,](https://wiki.qt.io/New_Signal_Slot_Syntax) `connect(myLineEdit, &QLineEdit::textChanged, this, &MainWindow::customSlot);` – Logesh G Aug 15 '18 at 05:11
0

Here also with lambda if interested:

connect(myLineEdit, &QLineEdit::textChanged, [=](QString obj) { customSlot(obj); });