如何连接Qt中不同对象的信号和槽?
我对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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您连接实例的信号和槽,而不是类的信号和槽。
您需要接收器和发射器对象的地址才能将它们连接在一起。
(假设“按钮”是一个指针)。
获取该指针是另一个问题,但除非您没有充分的理由这样做,否则我建议在您派生的 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.
(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.好吧,假设一切都很简单,就像您向它展示您真正的缩写代码一样,它应该很简单
但是,使用您在此处显示的代码,无法确定您想要什么类型的功能,并且不清楚到底是什么你在问。
Well, assuming everything is as simple as you show it your really really abbriated code, it should be simple
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.