如何在Qt中连接不同对象的信号和插槽? [英] How can I connect signals and slots of different objects in Qt?

查看:744
本文介绍了如何在Qt中连接不同对象的信号和插槽?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对QT c ++有疑问

i have a doubt in QT c++

假设这是main.cpp

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
}

这里是head.h



here is the head.h

class head:public QWidget
{
  Q_OBJECT

  /* some code */

  public slots:
  void change_number();
};

这里是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()),?,?);

};

现在如何连接mytailbutton的信号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.

感谢您的帮助!

推荐答案

您连接实例的信号和插槽,而不是类。

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()));

(假设button是指针)。

(assuming "button" is a pointer).

获取指针是另一个问题,但除非你没有其他好的理由,否则我建议在<$ c的构造函数中构造头对象$ c> QWidget 。

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.

这篇关于如何在Qt中连接不同对象的信号和插槽?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆