Qt learning notes - signals and slots

Posted by georgen on Sat, 12 Feb 2022 14:03:22 +0100

Default signals and slots

  • case

    • Click my button to close the window
    • The description of the signal may not be found in QPushButton. You can find it from the parent class. The child class will inherit the signal of the parent class
  • signal

  • groove

  • Demonstration

  • code

    •     //Click my button to close the window
          //Parameter 1 sender of signal parameter 2 specific signal sent (address of function) parameter 3 receiver of signal parameter 4 slot function processed
          connect(myBtn,&QPushButton::clicked,this,&QWidget::close);//Method of parent class
          //connect(myBtn,&MyQPushbutton::clicked,this,&MyWindow::close);// Call method of subclass

Custom signals and slots

  • case

    • Teacher class
    • Student class = student class
    • After class, the teacher will trigger a signal - hungry, the students respond to the signal - treat to dinner
  • Custom signal

    • Write custom signals to signals
    • The return value is void. You only need to declare it, not implement it
    • It can have parameters and can be overloaded
  • Slot function

    • Early Qt versions must be written to public slots, while advanced versions can be written to public or global
    • The return value is void, which needs to be declared and implemented
    • It can have parameters or overload
    • Yes Implemented in c
  • Run

  • Code

  •     ui->setupUi(this);
        //Create a teacher object
        this->te = new Teacher(this);//Put it in the object tree for easy release
        //Create a student object
        this->st = new Student(this);
        //The teacher is hungry and the students treat him to dinner
        //It must be connected before triggering

Custom signal and slot overloaded

  • modify

  • Run

    • However, you can find an extra quotation mark, which is undesirable, so you need to change the debug part
  • change

  • Run again

Trigger signal via button

  • modify

  • function

Nonparametric signal and slot connection / signal connection signal

  • code

    •     //Nonparametric signal and slot connection
          void (Teacher::*teSignalW)(void) = &Teacher::hungry;
          void (Student::*stSignalW)(void) = &Student::treat;
          //Signal connection signal
  • Run

  • Disconnect

    •     //Disconnect

One signal connects multiple slot functions

  • modify

  • Run

Multiple signals are connected to a slot function

  • modify

  • Run

be careful

  • The parameter types of signal and slot function must correspond to each other one by one. No slot parameter, i.e. no signal; The signal has a QString parameter, and the slot function must also have a QString parameter
  • The number of parameters of signal and slot function can be inconsistent. The number of parameters of the signal can be more than that of the slot function
  • The parameter order of the signal must be consistent with that of the slot function

for instance

  • Use the click signal of the button to connect a void slot function. There is no problem and it can run (it has just been tested)
  • What if you connect a QString slot function?

Signal clicked

  • Therefore, this is inconsistent with the slot function parameters of QString, and an error is reported

Lambda expression

Function object parameters

  • [] identifies the beginning of a Lambda. This part must exist and cannot be omitted. Function object parameters are passed to the constructor of the function object class automatically generated by the compiler. Function object parameters can only use local variables that are visible within the scope of Lambda when Lambda is defined (including this of Lambda's class). Function object parameters have the following forms
    • empty
      • There are no function object arguments
    • =
      • The function body can use all visible local variables within the scope of Lambda (including this of Lambda's class), and it is the way of value transfer (equivalent to that the compiler automatically passes all local variables for us by value)
    • &
      • All visible local variables within the scope of Lambda (including this of Lambda's class) can be used in the function body, and are passed by reference (equivalent to that the compiler automatically passes all local variables for us by reference)
    • this
      • In the function body, you can use the member variables in the class of Lambda
    • a
      • Pass a by value. When passing by value, the copy of a passed in cannot be modified in the function body, because the function is const by default. To modify the copy of a passed in, you can add the mutable modifier
    • &a
      • Pass a by reference
    • a,&b
      • Pass a by value and b by reference
    • =,&a,&b
      • Except that a and b are passed by reference, other parameters are passed by value
    • &,a,b
      • Except that a and b are passed by value, other parameters are passed by reference
  • Operator overloaded function arguments. Identify the parameters of the overloaded () operator. If there are no parameters, this part can be omitted. The parameters can be passed by value (such as: (a,b)) and by reference (such as (& A, & B))
  • The identifier can be modified. Mutable declaration. When passing a function object by value, you can modify the copy passed in by value after adding the mutable modifier (note that you can only modify the copy, not the value itself)
    • code
      •     QPushButton *myBtn = new QPushButton(this);
            QPushButton *myBtn2 = new QPushButton(this);
            int m = 10;
            connect(myBtn,&QPushButton::clicked,this,[m]()mutable{m = 100 + 10;qDebug() << m;});
            connect(myBtn2,&QPushButton::clicked,this,[=]()mutable{qDebug() << m;});
            qDebug() << m;
    • function
  • Function return value. - > The return value type identifies the type of return. When the return value is void or there is only one return in the function body (at this time, the compiler can automatically infer the type of return value), this part can be omitted
  • Function body. {} identifies the implementation of the function. This part cannot be omitted, but the function body can be empty


Topics: Qt