当前位置:网站首页>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? ?

原网站

版权声明
本文为[qq_ thirty-nine million two hundred and eighty thousand seven h]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280518242201.html