不再是所有函数都在一个类里,在改进版中,将原先的一堆命令分为两个大的函数clear()和readData(),再分别调用这两个函数即可
#include "tablewidget.h"
#include <QtWidgets>
//#include <QFile>
//#include <vector>
#include<iostream>
#include <fstream>
int x=1;
int y=20;
int location = (x-1)*355+y;
double imageData[85425][33];
TableWidget::TableWidget()
{
readData();
clear();
}
void TableWidget::clear()
{
table = new QTableWidget(this);
table->setColumnCount(56);
table->setRowCount(60);
table->resize(1366,200);
QStringList headers;
headers <<"400nm"<<"410nm"<<"420nm"<<"430nm"<<"440nm"<<"450nm"<<"460nm"<<"470nm"<<"480nm"<<"490nm"
<<"500nm"<<"510nm"<<"520nm"<<"530nm"<<"540nm"<<"550nm"<<"560nm"<<"570nm"<<"580nm"<<"590nm"
<<"600nm"<<"610nm"<<"620nm"<<"630nm"<<"640nm"<<"650nm"<<"660nm"<<"670nm"<<"680nm"<<"690nm"
<<"700nm"<<"710nm"<<"720nm"<<"730nm"<<"740nm"<<"750nm"<<"760nm"<<"770nm"<<"780nm"<<"790nm"
<<"800nm"<<"810nm"<<"820nm"<<"830nm"<<"840nm"<<"850nm"<<"860nm"<<"870nm"<<"880nm"<<"890nm"
<<"900nm"<<"910nm"<<"920nm"<<"930nm"<<"940nm"<<"950nm";
table->setHorizontalHeaderLabels(headers); //设置行标题,列标题不用设置,会自动排序
for (int column = 0; column < 33; ++column)
{
QTableWidgetItem *item0;
item0 = new QTableWidgetItem;
double m=imageData[location][column];
QString txt = QString("%1").arg(m); //把数字十进制地转换成string型
item0->setText(txt);
table->setItem( 0 , column , item0); //把txt内容填进表格里
}
}
void TableWidget::readData() //读入一张图的数据
{
std::ifstream fin;
fin.open("Imagedata.txt");
if (fin.fail())
{
std::cout << "File open error!";
int m;
std::cin >> m;
}
else{
for (int i = 0; i < 85425; i++)
{
for (int j = 0; j < 33; j++){
fin >> imageData[i][j];
}
}
fin.close();
}
}
另外,需要注意的是,如果把imageData定义成头文件中类的私有项,会因栈溢出而导致调试中断。中断停留在语句“
test dword ptr [eax],eax ; probe page
”处,解决方法1.减小申请空间的大小 2.大栈空间的大小 方法为:项目->属性->链接器->系统->堆栈保留大小
【注】:这里填的是字节数,如果你想把他扩大为2M的话,1024*1024*2 = 2097152
我这里就直接把它声明放在cpp文件里了。