Laurent Crivello
Laurent Crivello

Reputation: 3931

QLineEdit textChanged signal not called

I want to connect a textChanged signal to a QLineEdit, but it never gets called:

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog();
    virtual ~Dialog();
    QLineEdit *nameEdit;

public slot:
    void nameChanged(const QString &);
};

.c file:

Dialog::Dialog()
{

    nameEdit=new QLineEdit;
    connect(nameEdit, SIGNAL(textChanged(const QString &)), this, SLOT(nameChanged(const QString &)));
    ...
}

void Dialog::nameChanged(const QString & txt)
{
// NEVER CALLED
}

What do I do wrong ?

Upvotes: 3

Views: 10013

Answers (2)

The Quantum Physicist
The Quantum Physicist

Reputation: 26276

The old style usually writes a debug message in the console/terminal if something is wrong with the connection. Since you don't have a console, I highly recommend that you immediately stop using the old style of signals and slots, and use the new form which uses modern function binding:

connect(nameEdit, &QLineEdit::textChanged, this, &Dialog::nameChanged);

Try this, and see if it compiles. If it doesn't, it would mean that one of the signals/slots is overloaded, and in that case you'll need to statically cast to the overload you want to connect to. Another reason for a compilation error is that your connections are not compatible.

Advantages:

  1. Once this compiles, it works
  2. It's resolved at compile time, not run-time, so it's faster
  3. It doesn't use strings to resolve function names

PS: For full disclosure, the guy had that nameChanged() as a public slot (check his edit), and he changed it to a signal... no idea what's going on there.

Upvotes: 8

Avi
Avi

Reputation: 2069

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog();
    virtual ~Dialog();
    QLineEdit *nameEdit;

signals:
    void nameChanged(const QString &);
};

You want to use nameChanged() as a slot and not a signal

so the correct way is :

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog();
    virtual ~Dialog();
    QLineEdit *nameEdit;

public slots:
    void nameChanged(const QString &);
};

Upvotes: 1

Related Questions