Tony the Pony
Tony the Pony

Reputation: 41447

Why is my slot not being called?

I have this class:


class CustomEdit : public QTextEdit
{
    Q_GADGET

public:
    CustomEdit(QWidget* parent);

public slots:
    void onTextChanged ();
};


CustomEdit::CustomEdit(QWidget* parent)
    : QTextEdit(parent)
{
    connect( this, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
}


void CustomEdit::onTextChanged ()
{
    // ... do stuff
}


The onTextChanged method is never called when I type text into the edit control.
What am I missing?

Upvotes: 0

Views: 904

Answers (3)

Dave
Dave

Reputation: 155

One additional possibility which I just took about a day to solve in my own code:

  • The signal is defined in a superclass AND its subclass. The connect() call was operating on a subclass pointer, but the signal was emitted from the superclass code. The solution was to remove the signal declaration from the subclass, which was there by mistake anyway.

Upvotes: 1

Andrew
Andrew

Reputation: 656

A couple of other possibilities:

1) The object you are emitting the signal from is blocked (see QObject::blockSignals())

2) The receiver has no thread affinity. If the thread object that the receiver was created in goes away and the receiver isn't moved to another thread, it won't process events (slots being a special case).

Upvotes: 1

Pardeep
Pardeep

Reputation: 949

All classes that contain signals or slots must mention Q_OBJECT at the top of their declaration. They must also derive (directly or indirectly) from QObject.

Try using Q_OBJECT

Upvotes: 1

Related Questions