How to connect signals and slots of different objects in Qt?

I have doubts about QT C ++

Assume this is main.cpp

#include "head.h"
#include "tail.h"

int main()
{
  head *head_obj = new head();
  tail *tail_obj = new tail();
  //some code
}

here is head.h

class head:public QWidget
{
  Q_OBJECT

  /* some code */

  public slots:
  void change_number();
};

here is the tail .h

class tail:public QWidget
{
Q_OBJECT

  /* some code */
  /* some code */
  QPushButton *mytailbutton = new QPushButton("clickme");

  //this is where i need help
  connect(button,SIGNAL(clicked()),?,?);

};

Now, how to connect the mytailbutton clicked () signal to the class change_number slot? I just feel that this is impossible.

Thanks for the help!

+3
source share
2 answers

You connect signals and slots to instances, not classes.

You need the address of both the recipient and the emitter objects to connect them.

connect(button, SIGNAL(clicked()),
        pointer_to_instance_of_head, SLOT(change_number()));

(assuming that "button" is a pointer).

Getting this pointer is another question, but if you have no good reason for doing this, I suggest creating a head object in the constructor QWidgetthat you are retrieving.

+1
source

, , , ,

connect( aTailInstance->tailButon, SIGNAL( clicked() ), aHeadInstance, SLOT( change_number() ) );

, , , , , , .

0

All Articles