3

i have a doubt in QT c++

Suppose this is the main.cpp

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

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

here is the 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 do i connect the mytailbutton's signal clicked() to head class slot change_number? i just kind of feel there is no way this is possible.

Thank you for the help!

Tamás Szelei
  • 23,169
  • 18
  • 105
  • 180
wenn
  • 31
  • 1
  • 2
  • Are you deleting the allocated memory manually? If not, then it's a problem. For QObject-derived classes you can add a parent object in the constructor and it will release its children when destructed (e.g. the `QWidget` instance that you are adding your `QPushButton` to). – Tamás Szelei May 10 '11 at 16:24

2 Answers2

1

You connect signals and slots of instances, not of classes.

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

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

(assuming "button" is a pointer).

Getting that pointer is another question, but unless you don't have a good reason to do otherwise, I suggest constructing the head object in the constructor of the QWidget you are deriving.

Tamás Szelei
  • 23,169
  • 18
  • 105
  • 180
0

Well, assuming everything is as simple as you show it your really really abbriated code, it should be simple

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

However, with the code you have shown here it is impossible to determine what kind of functionality you are after and it isn't clear exactly what you are asking.

dusktreader
  • 3,845
  • 7
  • 30
  • 40