Qt Creator module learning-signals and slots

Qt Creator module summary-signals and slots

What are signals and slots

I want to prove the whole process through examples. For example, when the light is turned on, the button of the switch is equivalent to the address of the signal. The button is pressed is the way the signal is triggered, and the
light is the result. That is to run a function through the trigger of a button.

Characteristics of signals and slots

1. Signals and slots can have parameters but no return value.
2. When the number of parameters of the signal and the slot function is the same, their parameter types must be exactly the same.
3. The parameter table of the signal is more than the parameter table of the slot. However, starting from the first parameter of the parameter table of the signal, it can completely correspond to the parameter table of the slot. The extra parameters in the signal will be ignored by the slot.
4. In general, function pointers cannot be passed as parameters; template class objects cannot be passed as parameters;
but using the qRegistMetaType function to register template classes as meta types, you can pass:

#include<QMetaType>

qRegistMetaType<QList<void*>>("QList<void*>)

connect function

What is the connect function? You can search on Baidu. I will only explain the usage here.
connect (the address sent by the signal is the location of the light switch, the way the signal is sent is to turn on the light, the receiver address is the location of the light, and the signal processing function is the light on event); `
Example:

 p1.setText(" close");
 p1.move(100,100);
 connect(&p1,&QPushButton::pressed,this,&MainWindow::close);

The p1 in the example is declared in the private variable of the class. This is the standard signal and slot (pressed and close are available in QT)

Custom signals and slots

I made a small program to illustrate this point. First, I created two windows, one is the child window and the other is the parent window, and then each window has a button. At the beginning of the program, only the parent window can be seen. When the button is clicked, the parent window disappears and the child window appears. When the child window button is clicked, the child window disappears and the parent window appears. The disappearance is realized by our custom slot function, and the signal is realized when the child window is sent to the parent window.

 setWindowTitle("主窗口");
    b1.setParent(this);
    b1.setText("切换子窗口");
    b1.move(100,100);
    //点击按钮
    connect(&b1,&QPushButton::released,this,&Widget::a);
    //处理子窗口信号
    connect(&w, &laoer::mysignal,this,&Widget::deal);
//主窗口按钮点击后槽函数
void Widget::a(){
    
    
    w.show();
    this->hide();
}
//用来处理子窗口发送的信号
void Widget::deal(){
    
    
    w.hide();
    this->show();
}

Here is to declare a custom signal, the signal must have the signals keyword declaration

signals:
void mysignal();

Here is the signal transmission after clicking the button, and the main window starts processing after transmitting to the main window. Look at code one. The purpose of this writing is just to make it easier for everyone to take a look after copying.

void laoer::sendslot(){
    
    
    emit mysignal();
}

Signals and slots with parameters

This is because I haven't studied much by myself, so I can only talk about my understanding in order not to be ashamed.
At the beginning of the article, I probably wrote about the characteristics of signals and slots. So here I only use a simple code as a demonstration.
I personally think that the slot function with parameters is to pass the value...
First, add a signal in the sub-window (for more convenient recognition I used function overloading):

signals:
   
    void mysignal();
    void mysignal(int a,QString str);

Then we send this signal:

void laoer::sendslot(){
    
    
  emit mysignal();
  emit mysignal(5,"s");
}
connect(&b2,&QPushButton::clicked,this,&laoer::sendslot);

The received signal is not easy to understand, first upload the code and then explain in detail:

 void (laoer::*funsignal)()= &laoer::mysignal;
    connect(&w,funsignal,this,&Widget::dealle);
    void (laoer::*textsignal)(int,QString)= &laoer::mysignal;
    connect(&w,textsignal,this,&Widget::dealsignal);

Because the two signal names are the same here, for the connect function, he doesn't know which signal to accept, so we add pointers on the basis of function overloading, that is, function pointers are used here. You can search it on the Internet. I don’t know what I said, so sorry.

Lambda expression

Conditions of use: config += c++11 (in the pro file)
Advantages: It is very convenient to cooperate with the signal
Format: connect (signal sending address, signal sending method,
{ } ); [] The middle of the symbol indicates the variable in the following {} and if there is no variable in [], even if defined in {}, it cannot be called one by one. It is troublesome to write one by one. Therefore, some symbols indicate a certain set, as follows: "= ": Means to transfer all external local variables and all members of the class by value "this": Means to transfer all members of the class by value "&": Means to transfer all external local variables by value





The () symbol indicates the parameter.

How to use:

  b1.setParent(this);
    b1.setText("@_@");
    b1.move(100,100);
    connect(&b1,&QPushButton::released,
            [=](){
    
    
 b1.setText("^_^");
           }
);

Guess you like

Origin blog.csdn.net/m0_50210478/article/details/108126729