我在QT疑问C++如何连接Qt中不同对象的信号和插槽?
假设这是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()连接到head class slot change_number? 我只是觉得这是不可能的。
谢谢你的帮助!
您是否正在手动删除分配的内存?如果不是,那么这是一个问题。对于QObject派生类,您可以在构造函数中添加一个父对象,并在析构时释放子对象(例如,您正在添加'QPushButton'的QWidget实例)。 – 2011-05-10 16:24:06