一:Qt带参数的信号
main.cpp
#include "widget.h" #include "slot.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); //查找QApplication()的相关资料 Widget w; w.show(); return a.exec(); }
widget.cpp
#include "widget.h" #include "ui_widget.h" #include "QPushButton" #include <QDebug> //#include "mypushbutton.h" //自定义的按钮 Widget::Widget(QWidget *parent):QWidget(parent), ui(new Ui::Widget) //Widget为继承自QWidget类,该处为类外定义构造函数. { ui->setupUi(this); QPushButton *btn1 = new QPushButton; btn1->setParent(this); btn1->setText("处理带参数的信号");
QPushButton *btn2 = new QPushButton;
btn1->setParent(this); btn1->setText("处理不带参数的信号");
/*由于两个信号采用的是函数重载,所以这里需要使用函数指针来区别开.
* 函数指针:其本质是一个指向函数的指针,
* //简介下面的语句:“::”:用来限定作用域的
* void (myWidget::*myWidget_singals)(函数指针的参数) = &myWidget::my_singals;
* &myWidget::my_singals:表示信号(函数)的名字:我们把他看成一个变量名.而myWidget::是用来告诉编译器函数是myWidget类中的
* (myWidget::*myWidget_singals):为函数指针名.
* myWidget::指明他的作用域.
* int c = 5; int *p = &c;
*/
void (Widget::*myWidget_singals)() = &Widget::my_singals; //不带参数的信号
void (Widget::*myWidget_singals1)(int,QString) = &Widget::my_singals; //带参数的信号
connect(btn1,Widget_singals,this,&Widget::N_canshu); //处理不带参数信号.
connect(btn2,Widget_singals1,this,&Widget::print_singals); //处理带参数的信号.
}
void Widget::print_singals(int c, QString a) //处理带参数的信号.
{
//str.toUtf8().data()由于是中文需使用.
qDebug()<<"from is son Widget singals c and a "<<c<<a.toUtf8().data()<<endl;
}
void Widget::N_canshu()
{
qDebug()<<"没有参数";
}
widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> QT_BEGIN_NAMESPACE namespace Ui { class Widget; } //命名空间 Ui QT_END_NAMESPACE class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); void print_singals(int c,QString a); //处理带参数的槽函数 void N_canshu(); //处理不带参数的槽函数 signals:
/*定义信号时必须加上关键字signals.
* 信号没有返回值,但可以有参数.
* 信号就是函数声明,只需声明,无需定义。
* 使用emit发送.
* 信号可以重载.
*/
void my_singals(); //自定义信号
void my_singals(int,QString); //带参数的信号.当发送这个信号的时候就在
private: Ui::Widget *ui;
};
#endif // WIDGET_H
二:指针函数和函数指针
(1):指针函数:#include <iostream>
//#include<cstdlio.h>
using namespace std; /* 指针函数:简单的说,就是一个返回指针的函数其本质是一个函数。声明格式:类型说明符 *函数名(参数表) */ typedef struct _Data { int a; int b; }Data; //结构体对象. Data *fun(int a,int b) //指针函数 { //struct为结构体类型,class为类类型. Data *data=new Data; //动态分配内存 data ->a=a; data->b=b; cout<<data<<endl; return data;//返回的地址 。输出地址123 } int main(int argc, char** argv) { Data *p; p=fun(3,4); //p用来接收返回的地址,记住需要是同类型的 cout<<p<<endl; //输出地址123
cout<<p->a<<endl; //输出的是3 // cout<<data;
return 0; }
//26 8 E:\c++\main函数指针和指针函数.cpp [Error] 'data' was not declared in this scope,注意作用域,fun函数调用完后内存空间就被释放
二:函数指针:本质是一个指针,一个带有形参,像函数的指针,且用他来指向函数,从而就相当于引用了他.
#include <iostream>
//#include<cstdlio.h> using namespace std; typedef struct _Data { int a; int b; }Data; Data *fun(int a,int b) //指针函数 { Data *data=new Data; data ->a=a; data->b=b; cout<<data<<endl; return data;//返回的地址 。输出地址123 } int add(int x,int y) //定义 { return x+y; } int sub(int x,int y) //定义 { return x-y; } int (*fu)(int x,int y); //函数指针 int main(int argc, char** argv) { int add(int x,int y); int sub(int x,int y); Data *p; p=fun(3,4); //p用来接收返回的地址 cout<<p<<endl; //输出地址123 // cout<<data; //26 8 E:\c++\main函数指针和指针函数.cpp [Error] 'data' was not declared in this scope,注意作用域,fun函数调用完后内存空间就被释放 fu=add; cout<<(*fu)(4,5); fu=sub;
//41 3 E:\c++\main函数指针和指针函数.cpp [Error] invalid conversion from 'double (*)(double, double)' to 'int (*)(int, int)' [-fpermissive] cout<<(*fu)(10.12,5.11); return 0; }