如何连接Qt中不同对象的信号和槽?

发布于 2024-11-06 09:25:52 字数 680 浏览 0 评论 0原文

我对QT c++有疑问

假设这是main.cpp

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

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

这里是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()连接到头类插槽change_number? 我只是觉得这是不可能的。

谢谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

各自安好 2024-11-13 09:25:52

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

您需要接收器和发射器对象的地址才能将它们连接在一起。

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

(假设“按钮”是一个指针)。

获取该指针是另一个问题,但除非您没有充分的理由这样做,否则我建议在您派生的 QWidget 的构造函数中构造头对象。

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.

客…行舟 2024-11-13 09:25:52

好吧,假设一切都很简单,就像您向它展示您真正的缩写代码一样,它应该很简单

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

但是,使用您在此处显示的代码,无法确定您想要什么类型的功能,并且不清楚到底是什么你在问。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文