一: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; }