QT 信号与槽 最简单例子
main.cpp 和 my_head.h源码:
- #ifndef MY_HEAD_H
- #define MY_HEAD_H
- #include
- #include
-
-
- class A : public QObject
- {
- Q_OBJECT
-
- public:
- void test()
- {
- send_msg();
- }
- signals:
- void send_msg();
- };
-
- class B : public QObject
- {
- Q_OBJECT
-
- public slots:
- void recv_msg()
- {
- printf("hello\n");
- }
- };
-
-
- #endif // MY_HEAD_H
- #include "my_head.h"
-
- int main()
- {
- printf("start...\n");
-
- A send_a;
- B recv_b;
- QObject::connect(&send_a,SIGNAL(send_msg()),&recv_b,SLOT(recv_msg()));
-
- send_a.test();
- return 0;
- }
qmain一下:
运行结果:
/home/user/hello/hello 启动中...
start...
hello
/home/user/hello/hello 退出,退出代码: 0
{1 ?}