Qt signal with parameters, functions and function pointers and pointers

A: Qt band signal parameters

main.cpp

#include "widget.h"
#include "slot.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    A QApplication (argc, argv); // Find QApplication () relevant information 
    Widget w;
    w.show();
    return a.exec();
}

widget.cpp

#include " widget.h " 
#include " ui_widget.h " 
#include " QPushButton " 
#include <QDebug>
 // #include "mypushbutton.h"   // custom buttons 
Widget :: Widget (QWidget * parent) : QWidget (parent), UI ( new new Ui :: the Widget) // the Widget class is inherited from QWidget, there is defined outside the class constructor. 
{
         ui->setupUi(this);
            QPushButton *btn1 = new QPushButton;
            btn1->setParent(this);
            btn1 -> the setText ( "Signal Processing with arguments " );
       
the QPushButton * = new new btn2 the QPushButton;
            btn1->setParent(this); btn1-> setText ( "processed signal without parameters" );
/ * Since the two signals are used in function overloading, so there need to be distinguished from the use of function pointers. 
* Function pointer: a pointer which is essentially a function,
* // profile following statement: "::": with to define the scope
* void (myWidget :: * myWidget_singals) ( parameter function pointer) = & myWidget :: my_singals;
* & myWidget :: my_singals: represents the name of a signal (function): we regard him as a variable name and. & myWidget :: function is used to tell the compiler is myWidget class
* (myWidget :: * myWidget_singals): function pointer name: myWidget :: pinpoint his scope * int c = 5; * int * p =.. c ;, & *
/
 void (Widget :: * myWidget_singals) () = & Widget :: my_singals; // no parameter signals
 void (Widget :: * myWidget_singals1) (int, QString) = & Widget :: my_singals; // signal parameters
 connect (btn1, Widget_singals, this, & Widget :: N_canshu); // process the signal without parameters.
 connect (btn2, Widget_singals1, this, & Widget :: print_singals); // signal processing parameters.
}
void Widget :: print_singals (int c, QString a) // signal processing parameters.
{
    //str.toUtf8 (). data () because it is Chinese need to use.
    qDebug()<<"from is son Widget singals c and a "<<c<<a.toUtf8().data()<<endl;
}
:: N_canshu the Widget void () 
{
qDebug () << "no parameter";
}

widget.h

#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class the Widget;} // namespace Ui 
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    The Widget (the QWidget * parent = nullptr a);
     ~ the Widget ();
     void print_singals ( int C, QString A); // process a function with parameters groove 
    void N_canshu (); // treatment tank with no arguments of 
signals:
/ * Keyword signals must be added the definition signal.
 * No return value signal, but you can have parameters.
 * Signal is a function declaration, simply declare without definition.
 * Use emit sent.
 * Signal can be overloaded.
*/
void my_singals (); // custom signal
void my_singals (int, QString);. // parameter signal band when the signal is transmitted in
private: Ui::Widget *ui;
};
#endif // WIDGET_H

II: Pointer functions and function pointers

 

Guess you like

Origin www.cnblogs.com/1314bjwg/p/12317229.html