问题描述
点击ui界面的按钮,调用自定义类(myTest)中的函数pB_mytest_Slot()
Qt报错:
类中没有此项成员变量
错误代码
myTest* mytest = new myTest();
connect(this->ui->pB_mytest, &QPushButton::clicked, [this]{this->mytest->pB_mytest_Slot(); });
思路
MainWindow类中没有成员,可能是在成员变量(public、private)中出现问题
solving
.h
public:
myTest* mytest;
.cpp
mytest = new myTest();
connect(this->ui->pB_mytest, &QPushButton::clicked, [this]{this->mytest->pB_mytest_Slot(); });
this指代当前类。
如果以"myTest* mytest = new myTest();"这种方式构建一个指针,是没有问题的。
但是下方的connect上,"this->mytest"是不可以的,this指代当前类,this直接指向的必须是已经在成员变量定义好的,所以在public中定义。
拓展
如果在程序的源函数中,使用this指向成员变量,就需要在头文件中定义;
public:
Data();
vector<double> gyb_16_Ziz;
array<vector<double>, 6> xcjz_16_Ziz;
如果头文件无法定义,this指向的函数要改变