当前位置:网站首页>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()
{
}
边栏推荐
- Export MySQL table data in pure mode
- Comparison between variable and "zero value"
- The ceiling of MySQL tutorial. Collect it and take your time
- 机试刷题1
- 2022-07-05 use TPCC to conduct sub query test on stonedb
- Web APIs DOM 时间对象
- Senior soft test (Information System Project Manager) high frequency test site: project quality management
- labelimg的安装与使用
- HDR image reconstruction from a single exposure using deep CNN reading notes
- Classification, function and usage of MySQL constraints
猜你喜欢

视图(view)

Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)

新手程序员该不该背代码?

Daily question 1: force deduction: 225: realize stack with queue
![[leetcode] 19. Delete the penultimate node of the linked list](/img/ab/25cb6d6538ad02d78f7d64b2a2df3f.png)
[leetcode] 19. Delete the penultimate node of the linked list

That's why you can't understand recursion

CCNA Cisco network EIGRP protocol

NPDP certification | how do product managers communicate across functions / teams?

Spatial domain and frequency domain image compression of images

Aardio - 不声明直接传float数值的方法
随机推荐
Sizeof keyword
Memorabilia of domestic database in June 2022 - ink Sky Wheel
Installation and use of labelimg
Research and investment strategy report of China's VOCs catalyst industry (2022 Edition)
Senior soft test (Information System Project Manager) high frequency test site: project quality management
做接口测试都测什么?有哪些通用测试点?
【LeetCode】19、 删除链表的倒数第 N 个结点
Learn the principle of database kernel from Oracle log parsing
Unity3d minigame unity webgl transform plug-in converts wechat games to use dlopen, you need to use embedded 's problem
Web APIs DOM time object
树的先序中序后序遍历
2014阿里巴巴web前实习生项目分析(1)
手写ABA遇到的坑
Data storage (1)
[linear algebra] determinant of order 1.3 n
CCNA Cisco network EIGRP protocol
MySQL ---- first acquaintance with MySQL
UE4蓝图学习篇(四)--流程控制ForLoop和WhileLoop
MySQL约束的分类、作用及用法
The difference between enumeration and define macro