Vollmilchbb
Vollmilchbb

Reputation: 481

Refer to the Sender Object in Qt

I'm having some trouble, I'm fairly new to Qt and C++ and was testing the waters so to say. Ill try to describe my problem as follows. I have a LineEdit QLineEdit and this edit has a connection which looks like this:

connect(my_lineedit, SIGNAL (textEdited(QString)),this, SLOT (handleEdits()));

The handleEdits() method gets called and does the following:

  1. Disconnect the previous Signal from my_lineedit
  2. Create a new QLineEdit which gets a new signal and calls handleAddedEdits()
  3. Adds the newely created Edit to my layout.

The stated above works fine Im just telling you this so you get the picture. Now in the new method which I called handleAddedEdits() I want kinda the same procedure to happen again.

  1. Disconnect the Signal which calls handleAddedEdits() from the Edit which invoked this method in the first place.
  2. Create a fresh QLineEdit
  3. Add this to my layout.

The problem is: in the first case my_lineedit is declared in my class so I can freely refer to it and and remove the signal as I wish. In the second case I have a QLineEdit which was created dynamically in the handleEdits() method and is the "Sender". My Question is, how can I refer to the "Sender Object" ro remove the Signal from the dynamically created edit?

Upvotes: 4

Views: 7623

Answers (1)

Vladimir Bershov
Vladimir Bershov

Reputation: 2832

You need to use QObject::sender() method in your receiver's slot:

For cases where you may require information on the sender of the signal, Qt provides the QObject::sender() function, which returns a pointer to the object that sent the signal.

handleAddedEdits()
{
    QObject* obj = sender();
    disconnect( obj, SIGNAL(textEdited(QString)), 0, 0 );
    //...
}

Upvotes: 10

Related Questions