Qt's custom signals and slots

        The custom signal should be written under signal, and the return value is void. It only needs to be declared, and it does not need to be implemented. It can have parameters and can be overloaded. The early version of the slot function must be written under public slots, and the advanced version can be written under public or global. The return value is void, which needs to be declared (treat), needs to be implemented (in cpp), can have parameters, and can be overloaded. When using it, you need to connect the signal and the slot, and give the trigger (emit).

The requirement this time is: after class, I will send a signal --> hungry --> my friend receives the signal --> respond and invite me to dinner . Two classes are needed this time: My class and Friend class, corresponding to me and my friend respectively. This time only the operations with parameters are performed. process:

① First add two classes, belonging to QObject, named My and MyFriend respectively.

② Declare the object and dismissal signal in widget.h

    // 对象声明
    My * my;
    MyFriend * fri;

    void classover();

③ Slot function declaration in firend.h

public:
    explicit MyFriend(QObject *parent = nullptr);
    
    // 槽函数:早期版本必须写到public slots下    高级版本可以写到public或者全局下
    // 返回值void,需要声明(treat),需要实现(cpp中)
    // 可以有参数,可以发生重载
    void treat(QString foodName);

④ Declare the signal in my.h

signals:
    // 自定义信号 写到signal下
    // 返回值是 void ,只需要声明,不需要实现
    // 可以有参数,可以重载
    void hungry(QString foodName);  // 说饿了并且说吃什么

⑤ Define the function in friend.cpp

void MyFriend::treat(QString foodName)
{
    // 把QString转换成 char * 形式在输出的foodname就不会带""  
    // 先用(.toUtf8)转成QByteArray(字节数组型)再(.data())转char *
    qDebug()<<"我朋友请我吃饭,我要吃:" << foodName.toUtf8().data();
}

⑥ Connect in widget.cpp:

        (1) Create objects of me and my friend

    // 创建一个我的对象
    this->my = new My(this);   // 指定好父类后,new之后就不需要管释放了
    // 创建一个朋友的对象
    this->fri = new MyFriennd(this);

        (2) Creation of get out of class dismissal function

void Widget::classover()
{
// 下课后函数,调用后 触发我饿了的信号
    // 触发的关键字 emit
    emit my->hungry("肥牛鱼粉");
}

        (3) Connect custom signals and slots with parameters

    // 连接含参的 信号和槽
    // 指针可以指向地址,函数指针指向函数地址
    void( My:: *mySingal )(QString) = &My::hungry;
    void( MyFriend:: *friSlot )(QString) = &MyFriend::treat;
    connect(my,mySingal,fri,friSlot);

        (4) Button setting and connection

    // 点击 下课按钮  触发下课
    QPushButton * btn = new QPushButton("下课",this);
    this->resize(600,400);  // 重置窗口大小,resize是窗口this下的方法

    // 点击按钮,触发下课
    connect(btn,&QPushButton::clicked,this,&Widget::classover);   // 信号连接槽

        (5) test

        Click the get out of class button and observe the output in the application bar.

Supplementary part:

① Signal connection signal

    connect(te,teacherSingal,st,studentSlot);    
    connect(btn,&QPushButton::clicked,my,mySingal);
    // 先连接 friSlot 和 mySingal ,再连接 mySingal 和 &QPushButton::clicked
    // ① 在触发QPushButton::clicked(点击按钮)时   ② 通过中介 mySingal    ③ 触发 friSlot

② Disconnect

// 断开 friSlot 和 mySingal 之间的连接
   disconnect(my,mySingal,fri,friSlot);

Note : ① A signal can be connected to a signal; ② A signal can be connected to multiple slot functions; ③ Multiple signals can be connected to a slot function;   

        ④The parameter types of signal and slot functions must match one by one; ⑤The number of signal parameters ≥ the number of slot function parameters .  

③ lambda expression:

Used to define and create anonymous function objects, the basic format is as follows:

connect(btn,&QPushButton::clicked,this,[x](){

});

1. [ x ] is the beginning of the expression and cannot be omitted. x has the following values:

        (1) [=], is the value transfer method, which means that all visible local variables in the scope of the scope can be used, including this, which is the most commonly used method, and the following are not commonly used;

        (2) The effect of [=] is equivalent to [&], where & represents the way of passing by reference;

        (3) [this] All member variables of the class where the lambda is located can be used in the body of the function;

        (4) [a] Pass a by value, only a can be passed inside {}, and other variables are not recognized. It is generally used to modify only one of the members. [&b] Pass b by reference;

        (5) [=,&a,&b] except a, b is passed by reference, and others are passed by value;

        (6) [=, a, b] Except a, b is passed by reference value, and others are passed by reference;

2. Function return value: -> return value type, the data type before and after must be consistent;

3. The content in the implementation body { } is the part that needs to be implemented, which is equivalent to putting a bunch of signal functions in the slot function.

Guess you like

Origin blog.csdn.net/weixin_58351753/article/details/127460453