当前位置:网站首页>QT信号和槽
QT信号和槽
2022-07-06 15:32:00 【出现一片乱码】
系列文章目录
提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:第一章 Python 机器学习入门之pandas的使用
提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
一、信号和槽
信号和槽式用于对象之间的通信,类似于后端与前端建立连接的接口地址。
信号是由发送者发送,槽可以看作接收方
信号和槽是QT特有的信号传输机制,他可以让互不干扰的对象建立联系
利用connect函数建立
槽的本质是类的成员函数,其参数可以是任意类型和C++成员函数没什么区别。可以是虚函数,也可被重载,
唯一区别是槽可以与信号连接在一起,,每当和槽连接的信号被发送的时候就会调用槽
也可以信号发送信号,然后在发送给槽做相应的操作
信号的特点:
普通函数,不需要实现,使用signal关键字声明,可以自定义信号,没有返回值,可以带参数,可以适合用emit关键字发送信号
槽的特点
可自定义槽函数
槽函数没有返回值
可以带参数
二、连接
1、QT4
通过宏
SIGNAL SLOT
SLGNAL()/SLOT两个宏江函数转体->字符串
connect(sender,SLGNAL(sig1(int,double,QString)),receiver,SLOT(sil1(int,doublemQString)))
缺点 编译时不会做错误检查
2、QT5
信号槽使用的函数地址
connect(sender,&Sender::sig1,receiver,&Receiver::slot1)
优点 编译过程中会对函数类型,个数做检查
代码如下(示例):
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
四、注意事项
信号/槽函数参数个数是可以不一样的
槽函数参数个数可以少于信号的参数个数
不可逆
五、扩展
信号可以连接信号
一个信号可以连接多个槽函数
一个槽函数可以同时被多个信号连接
槽函数可以使用lambda表达式
信号槽连接之后,可以被断开disconnect
下面是主函数中写的,其他的创建项目自动添加,只需要把下面的代码复制到主cpp就好
实例代码 基于QT4
#include "widget.h"
#include <QPushButton>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QPushButton *btn = new QPushButton("adsdadww ",this);
btn->show();
btn->resize(50,50);
btn->move(60,60);
resize(600,400);
QPushButton *btn2 = new QPushButton("slfeofs",this);
btn2->show();
btn2->resize(80,30);
btn2->move(100,100);
this->zt = new Teacher(this);
this->st = new Student(this);
connect(btn2,SIGNAL(clicked()),zt,SIGNAL(hungry()));
connect(zt,SIGNAL(hungry()),st,SLOT(treat()));
connect(btn2,SIGNAL(clicked()),this,SLOT(close()));
st->treat();
setWindowTitle("sdawadwd");
setFixedSize(600,400);
//connect(st,SLOT(treat()));
}
void Widget::classIsOver(){
}
Widget::~Widget()
{
}
基于QT5的代码
#include "mainwindow.h"
#include <QPushButton>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QPushButton *btn = new QPushButton("adsdadww ",this);
btn->show();
btn->resize(50,50);
btn->move(60,60);
resize(600,400);
QPushButton *btn2 = new QPushButton("slfeofs",this);
btn2->show();
btn2->resize(30,30);
btn2->move(100,100);
connect(btn2,&QPushButton::clicked,this,&MainWindow::close);
setWindowTitle("sdawadwd");
setFixedSize(600,400);
}
MainWindow::~MainWindow()
{
}
边栏推荐
- 如何实现文字动画效果
- Aardio - Method of batch processing attributes and callback functions when encapsulating Libraries
- pytorch_ Yolox pruning [with code]
- Report on technological progress and development prospects of solid oxide fuel cells in China (2022 Edition)
- rust知识思维导图xmind
- Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
- 2022-07-04 mysql的高性能数据库引擎stonedb在centos7.9编译及运行
- Puppeteer连接已有Chrome浏览器
- Applet system update prompt, and force the applet to restart and use the new version
- Comparison between variable and "zero value"
猜你喜欢
Spatial domain and frequency domain image compression of images
signed、unsigned关键字
Aardio - 利用customPlus库+plus构造一个多按钮组件
Learn the principle of database kernel from Oracle log parsing
Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)
Advantages of link local address in IPv6
树的先序中序后序遍历
NPDP认证|产品经理如何跨职能/跨团队沟通?
Leetcode exercise - Sword finger offer 26 Substructure of tree
CocosCreator+TypeScripts自己写一个对象池
随机推荐
Spatial domain and frequency domain image compression of images
【踩坑合辑】Attempting to deserialize object on CUDA device+buff/cache占用过高+pad_sequence
Build op-tee development environment based on qemuv8
Heavyweight news | softing fg-200 has obtained China 3C explosion-proof certification to provide safety assurance for customers' on-site testing
MySQL ---- first acquaintance with MySQL
General implementation and encapsulation of go diversified timing tasks
Aardio - 不声明直接传float数值的方法
signed、unsigned关键字
Aardio - integrate variable values into a string of text through variable names
The SQL response is slow. What are your troubleshooting ideas?
3DMAX assign face map
UVa 11732 – strcmp() Anyone?
That's why you can't understand recursion
雅思口语的具体步骤和时间安排是什么样的?
Aardio - 封装库时批量处理属性与回调函数的方法
Clip +json parsing converts the sound in the video into text
Leetcode exercise - Sword finger offer 26 Substructure of tree
What are the interface tests? What are the general test points?
sizeof关键字
Void keyword