当前位置:网站首页>Qt自定义类使用自定义含参信号与槽
Qt自定义类使用自定义含参信号与槽
2022-07-24 03:05:00 【谛听misa】
最近在自定义类中使用了信号来连接到MainWindow类中的槽函数,具体操作如下:
要点一:继承QObject
在头文件中初始化自定义类MyThread1,这里由于继承的QThread已经继承了QObject,无需嵌套继承,故不必public QObject,但仍需加入Q_OBJECT来保证moc编译器可以连接信号和槽函数
//位置:mainwindow.h
//一个线程类的实例,单纯使用自定义信号的话除了signals以外的部分都不用关心
class MyThread1 : public QThread
{
Q_OBJECT
public:
MyThread1();
void stop();
QTcpSocket* position;
bool pos_connected;
signals:
void pos_update(QString x,QString y);
protected:
void run();
private:
volatile bool stopped;
};
要点二:自定义信号与槽
在自定义的类里面声明信号pos_update(QString x,QString y),但其只是信号,无需在cpp文件中给出实现
在MainWindow中加入信号对应的槽函数
注意:信号与槽的参数数量、类型与顺序要保持一致(可以出现信号参数比槽函数多,但也不建议这么做,反之槽函数参数比信号参数多则是绝对不行的)至于使用自定义的数据类型作为信号参数可以参考链接
槽函数必须给出实现:
//位置:mainwindow.cpp
//接收另一个线程传来的消息后将其展示在ui界面上
void MainWindow::update(QString x,QString y)
{
ui->pos_x->setText(x);
ui->pos_y->setText(y);
}
要点三:连接信号与槽
实例化类时最好使用指针,便于信号连接,下图中有两种信号连接的表达方式,本质上都是connect(const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method,
Qt::ConnectionType type = Qt::AutoConnection)
更多连接写法见[链接](https://blog.csdn.net/qq_27898413/article/details/11967
7345)
//位置:mainwindow.cpp声明的全局变量
MyThread1 *thread1 = new MyThread1();
//位置:mainwindow.cpp中MainWinw类的初始化函数中
connect(thread1,&MyThread1::pos_update,this,&MainWindow::update);
要点四:发送信号
直接在需要发送信号的地方emit即可
//位置:mainwindow.cpp中任何需要调用update的地方
emit pos_update(posx,posy);
边栏推荐
- Attack and defense world web practice area (webshell, command_execution, simple_js)
- Babylon.js cool canvas background animation JS special effects
- LeetCode-栈和队列刷题
- Lumberyard game engine of o3de
- Tweenmax+svg Pikachu transformation ball
- AcWing 4498. 指针 (DFS)
- Hcip --- BGP comprehensive experiment
- 攻防世界WEB練習區(view_source、get_post、robots)
- [AMC] federal quantification
- Ways to improve the utilization of openeuler resources 01: Introduction
猜你喜欢

Babylon.js cool canvas background animation JS special effects

Attack and defense world web practice area (webshell, command_execution, simple_js)

攻防世界WEB练习区(weak_auth、simple_php、xff_referer)

攻防世界WEB练习区(webshell、command_execution、simple_js)
![[hdlbits questions] Verilog language (2) vectors](/img/eb/125c9a7781391dc53e37ce347a475d.png)
[hdlbits questions] Verilog language (2) vectors

String.split() the most detailed source code interpretation and precautions

攻防世界WEB练习区(backup、cookie、disabled_button)

在openEuler社区开源的Embedded SIG,来聊聊它的多 OS 混合部署框架
[email protected]使用原理"/>(六)装饰器扩展之[email protected]使用原理

Summernote rich text editor
随机推荐
Daily gossip (I)
移动通信的新定义:R&SCMX500 将提高5G设备的IP数据吞吐量
Rules for generating 13 digit barcode EAN-13 Code: the thirteenth digit is the verification code obtained by calculating the first twelve digits.
Liveqing live broadcast on demand streaming media OBS streaming live broadcast how to obtain interface verification token video verification streamtoken and configure token validity
openEuler 资源利用率提升之道 01:概论
老公,我们现在无家可归了
SSM家庭理财个人理财管理系统记账系统
SkyWalking分布式系统应用程序性能监控工具-上
Job hunting and recruitment system of SSM part-time job hunting
AcWing 4498. 指针 (DFS)
软考---程序设计语言基础(上)
The new idea 2022.2 was officially released, and the new features are nice
(六)装饰器扩展之[email protected]使用原理
Unity 消息推送
JVM初始
PMP first-hand data and information acquisition
Go log package
JS small game running bear and cat source code
MySQL sub database and sub table and its smooth expansion scheme
Open source embedded sig in the openeuler community. Let's talk about its multi OS hybrid deployment framework