Reputation: 3931
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
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:
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
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