当前位置:网站首页>Qt学习之信号和槽机制
Qt学习之信号和槽机制
2022-07-28 15:22:00 【tyrocjl】
概念
信号和槽是Qt中自行定义一种通信机制,实现对象之间的交互;当对象的状态发生改变,将会发送信号,该信号可以被其它对象所接收,接收到信号后会执行一个指定操作函数(槽)。定义
class XX:public QObject{ Q_OBJECT //moc元对象编译器 signals: void sig_func(...);//信号 public slots: void slot_func(...){ ...}//槽 };注:信号函数只需声明,不能写定义
注:槽函数可以连接到某个信号上,通过信号可以触发槽函数的执行;另外槽函数也可以当做一个普通成员函数,直接调用.信号和槽连接
QObject::connect( const QObject* sender,//信号发送对象 const char* signal, //信号函数 const QObject* receiver,//信号的接收对象 const char* method)//槽函数SIGNAL(信号函数(参数表))//将信号函数转换为const char*
SLOT(槽函数(参数表))//将槽函数转换为const char*案例:创建Qt应用程序,里面包含一个标签(QLabel)和一个按钮(QPushButton),实现点击按钮关闭标签功能.
#include <QApplication> #include <QLabel> #include <QPushButton> #include <QFont> #include <QDialog> int main(int argc,char** argv){ QApplication app(argc,argv); QDialog parent;//父窗口 parent.resize(480,320); //创建按钮和标签控件 QPushButton button("关闭标签",&parent); QLabel label("我是标签",&parent); //调整大小和位置 button.move(50,200); label.move(50,50); button.resize(150,50); label.resize(150,50); //设置字体大小 QFont font; font.setPointSize(20); parent.setFont(font); //动态创建按钮对象 QPushButton* button2 = new QPushButton("退出",&parent); button2->move(300,200); button2->resize(150,50); //显示父窗口 parent.show(); //点击按钮关闭标签 QObject::connect( &button,SIGNAL(clicked(void)), &label,SLOT(close(void))); //练习:使用new操作动态创建一个按钮对象,文本显 //示"退出",实现点击该按钮退出应用程序功能. QObject::connect( button2,SIGNAL(clicked(void)), //&app,SLOT(closeAllWindows(void))); &app,SLOT(quit(void))); //&parent,SLOT(close(void))); return app.exec(); }信号和槽连接的语法要求
1)信号和槽参数要一致QObject::connect(A,SIGNAL(sigfun(int)), B,SLOT(slotfun(int)));//ok QObject::connect(A,SIGNAL(sigfun(void)), B,SLOT(slotfun(int)));//error2)可以有缺省参数
class B:public QObject{ Q_OBJECT public slots: void slotfun(int = 0){ } }; QObject::connect(A,SIGNAL(sigfun(void)), B,SLOT(slotfun(void)));//ok3)信号的参数可以多于槽,多余参数将被忽略
QObject::connect(A,SIGNAL(sigfun(int)),B,SLOT(slotfun(void)));//ok
4)一个信号可以同时连接多个槽(一对多)QObject::connect(A,SIGNAL(sigfun(int)),B1,SLOT(slotfun1(int)));QObject::connect(A,SIGNAL(sigfun(int)),B2,SLOT(slotfun2(int)));
注:当A发送信号,B1和B2的槽函数都将被执行,但是多线程下执行顺序不确定.
5)多个信号可以同时连接同一个槽(多对1)
QObject::connect(A1,SIGNAL(sigfun1(int)), B,SLOT(slotfun(int)));
QObject::connect(A2,SIGNAL(sigfun2(int)), B,SLOT(slotfun(int)));
注:无论A1还是A2发送信号,B的槽函数都会被执行
6)两个信号可以直接连接(信号串联)QObject::connect(A1,SIGNAL(sigfun1(int)), A2,SIGNAL(sigfun2(int)));
A1–A2–>B
注:如果A1发送信号,A2的信号也将随之发送案例:事件同步,实现滑块(QSlider)和选值框(QSpinBox)同步运行
1)滑块:QSlider
QSlider(垂直/水平,父窗口指针);//构造函数
void setRange(int min,int max);//设置滑动范围
void valueChanged(int value);//滑块滑动时发送的信号
void setValue(int value);//设置当前滑块位置槽函数
2)选值框:QSpinBox
QSpinBox(父窗口指针);//构造函数
void setRange(int min,int max);//设置选值框数值范围
void valueChanged(int value);//数值改变时发送的信号
void setValue(int value);//设置当前当前数值槽函数#include <QApplication> #include <QSlider>//滑块 #include <QSpinBox>//选值框 #include <QDialog> #include <QFont> int main(int argc,char** argv){ QApplication app(argc,argv); //父窗口 QDialog parent; parent.resize(480,320); QFont font; font.setPointSize(20); parent.setFont(font); //滑块 QSlider slider(Qt::Horizontal,&parent); slider.setRange(1,100); slider.move(100,50); //选值框 QSpinBox spin(&parent); spin.setRange(1,100); spin.move(100,200); //显示父窗口 parent.show(); //滑块滑动,选值框数值随之改变 QObject::connect( &slider,SIGNAL(valueChanged(int)), &spin,SLOT(setValue(int))); //选值框数值改变,滑块随之滑动 QObject::connect( &spin,SIGNAL(valueChanged(int)), &slider,SLOT(setValue(int))); return app.exec(); }
注意以上代码编译时要按照qmake -project qmeke make …
边栏推荐
- Note: numerical accumulation animation
- R语言使用GGally包的ggpairs函数可视化分组多变量的两两关系图、设置alpha参数改变图像透明度、对角线上连续变量密度图、离散变量条形图、两两关系图中包含散点图、直方图、箱图以及相关性数值
- js 双向链表 01
- The epidemic dividend disappeared, and the "home fitness" foam dissipated
- 带你来浅聊一下!单商户功能模块汇总
- JS bidirectional linked list 01
- Is MySQL query limit 1000,10 as fast as limit 10? If I want to page, what should I do?
- Wei Jianjun couldn't catch up with Li Shufu by riding a BMW
- Practical development tutorial of software problem repair tracking system (Part 1)
- Headline article_ signature
猜你喜欢

The deep displacement monitoring system wk813 is used to measure the deep displacement of slopes, dams, embankments, railways and building foundation pit excavation

A tour of grp:05 - GRP server streaming service end stream

关于标准IO缓冲区的问题

laravel

mysql查询 limit 1000,10 和limit 10 速度一样快吗?如果我要分页,我该怎么办?

为什么学编程的人大多数都去了深圳和北京?

1. Simple command line connection to database

食品安全 | 这两类瓜果宜改善便秘 孕妇人群尤其建议

动态规划 --- 数位统计DP

js 双向链表 01
随机推荐
flashfxp 530 User cannot log in. ftp
李宏毅《机器学习》丨4. Deep Learning(深度学习)
带你来浅聊一下!单商户功能模块汇总
Rosen's QT journey 102 listmodel
Temperature measurement and imaging accuracy of ifd-x micro infrared imager (module)
Food safety | these two kinds of melons and fruits should be improved, especially for pregnant women with constipation
JS linked list 02
Leetcode topic
Sudden! MSI CEO Jiang Shengchang fell to death
Automatic conversion and cast
IT远程运维是什么意思?远程运维软件哪个好?
LwIP development | realize TCP server through socket
为什么学编程的人大多数都去了深圳和北京?
Redis系列4:高可用之Sentinel(哨兵模式)
Ffmpeg get the first frame
High precision absolute angle sensor application high speed angle monitoring
QT打包
Is MySQL query limit 1000,10 as fast as limit 10? If I want to page, what should I do?
A tour of grp:05 - GRP server streaming service end stream
laravel