当前位置:网站首页>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_Hwidget.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? ?
边栏推荐
- Advanced C language - Section 1 - data storage
- 前几年的互联网人vs现在的互联网人
- Analysis of scoped attribute principle and depth action selector
- Chinese trumpet creeper
- 2021TPAMI/图像处理:Exploiting Deep Generative Prior for Versatile Image Restoration and Manipulation
- PHP development 16 exit module
- Vercel uses HTTP caching
- Collection of some compatibility issues
- E - Lucky Numbers
- 约瑟夫问题
猜你喜欢

PostgreSQL Guide: Insider exploration (Chapter 7 heap tuples and index only scanning) - Notes

Win8.1和Win10各自的優勢

PowerShell: because running scripts is prohibited on this system, the solution

QT client development -- driver loading problem of connecting to MySQL database

Ctfshow SQL injection (211-230)

Createanonymousthreadx passes parameters to anonymous threads

Infinite cycle scrolling code Alibaba international station store decoration code base map scrolling black translucent display effect custom content decoration code full screen display

Section 7 - structures

Section 5 - Operator details

【JS解决】leedcode 117. 填充每个节点的下一个右侧节点指针 II
随机推荐
Createanonymousthreadx passes parameters to anonymous threads
Keil uses j-link to burn the code, and an error occurs: Flash download failed - one of the "Cortex-M3" solutions
Conception d'un système basé sur MVC avec javaswing JDBC
2022年氧化工艺操作证考试题库及模拟考试
Powershell 加域 Add-Computer模块
Implementation of article list function on PHP 18 home page
Advanced C language - Section 1 - data storage
Spice story
CreateAnonymousThreadX给匿名线程传递参数
Explain the role of key attribute in V-for
String()和toString()方法得区别
promise处理js多线程全部获得结果后同一处理结果
Tita performance treasure: remote one-on-one discussion
D 小红的构造题
Handwritten promise and its method, with detailed notes
Embedded hardware - read schematic
Configuration used by automatic teaching evaluation script
rainbow
Nodejs parsing get request URL string
Promise processing JS multithreads get the same processing result after all the results are obtained