How can I connect signals and slots of different objects in Qt?

后端 未结 2 1719
伪装坚强ぢ
伪装坚强ぢ 2021-01-20 06:17

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 *ta         


        
相关标签:
2条回答
  • 2021-01-20 07:12

    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.

    0 讨论(0)
  • 2021-01-20 07:20

    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.

    0 讨论(0)
提交回复
热议问题