当前位置:网站首页>QT signal is automatically associated with the slot
QT signal is automatically associated with the slot
2022-06-13 04:54:00 【qq_ thirty-nine million two hundred and eighty thousand seven h】
The difference between passive Association and automatic association :
Passive Association : Which is what we often use connect function , The designation of the display SIGNAL and SLOT.
Automatic association : Implicitly indicate the relationship between the signal and the slot , Mainly set by components Objectname And signals signal Name Association .
Automatic association description :
Rule requirements 1:
Automatic correlation slot function naming :
void on_<object name>_<signal name>(<signal parameters>);
objectName: Name of the part , adopt setObjectName Member function settings .
signalname: That's our signal name ,
signal parameters: Shape parameter
Rule requirements 2:
call connectSlotsByName
[static] void QMetaObject::connectSlotsByName(QObject *object)
Official translation :
call connectSlotsByName All child objects of this object will be searched recursively , Automatically associate subassemblies according to
void on_<object name>_<signal name>(<signal parameters>); Slot function of .
For example, one of our objects is named button1 Of QPushButton, Then the matching slot will be :
void on_button1_clicked();
Automatic association example :
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
#include <QMetaObject>
class UI;
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
public slots:
void on_ok_btn_clicked();
void on_cancle_btn_clicked();
private:
UI *ui;
};
class UI
{
public:
UI(QWidget * parent = 0);
private:
QPushButton *ok_btn;
QPushButton *cancle_btn;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
ui = new UI(this);
QMetaObject::connectSlotsByName(this);
}
Widget::~Widget()
{
}
void Widget::on_ok_btn_clicked()
{
qDebug()<<"on_ok_btn_clicked";
}
void Widget::on_cancle_btn_clicked()
{
qDebug()<<"on_cancle_btn_clicked";
}
UI::UI(QWidget * parent)
{
ok_btn = new QPushButton(parent);
ok_btn->setObjectName("ok_btn");
ok_btn->setText("OK");
ok_btn->setGeometry(0,0,60,30);
cancle_btn = new QPushButton(parent);
cancle_btn->setObjectName("cancle_btn");
cancle_btn->setText("cancle");
cancle_btn->setGeometry(100,0,60,30);
}
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
Output :
doubt :
Why? UI Class does not inherit Object class , as well as UI There is no Q_OBJECT keyword , It can also correlate signals with slots ?
Hey , You know what? ?
边栏推荐
- Embedded hardware: electronic components (1) resistance capacitance inductance
- Tita绩效宝:远程一对一面谈的问题
- 2022道路运输企业安全生产管理人员操作证考试题库及答案
- Ctfshow SQL injection (211-230)
- SQL notes
- 自动评教脚本使用的配置
- Advanced C - Section 2 - pointers
- Mysql database installation
- Serial communication learning
- 【JS解决】leedcode 117. 填充每个节点的下一个右侧节点指针 II
猜你喜欢
[JS solution] leedcode 200 Number of islands
Hidden implementation and decoupling, knowing Pimpl mode
Brick story
CMB's written test -- data analysis
Design system based on MVC using javeswingjdbc
Mind mapping series - Database
What is the difference between ROM, ram and flash? SRAM、DRAM、PROM、EPROM、EEPROM
约瑟夫问题
Internet people a few years ago vs Internet people today
用navicat将mysql数据转化为postgresql
随机推荐
String()和toString()方法得区别
Draw a hammer
rainbow
E - Lucky Numbers
【JS解决】leedcode 200. 岛屿数量
QT client development -- driver loading problem of connecting to MySQL database
RMQ、LCA
[untitled]
推荐的图片临时在线压缩工具
Vercel 使用 HTTP 缓存
Advanced C - Section 3 - character functions and string functions
Colab tutorial (super detailed version) and colab pro/pro+ evaluation
第三方评论插件
Mind mapping series - Database
On switch() case statement in C language
The processing flow of thread pool depends on the core parameters
Force buckle 92 Reverse linked list II
PowerShell:因为在此系统上禁止运行脚本,解决方法
Configuration used by automatic teaching evaluation script
Design system based on MVC using javeswingjdbc