Qt——读入txt文件,写入到表格

本文介绍了如何在Qt环境中使用readData()函数读取txt文件,并将内容填充到一个56*60的表格中。由于初始表格显示过小,作者通过resize函数调整了大小。此外,还分享了将十进制数字转换为字符串的方法,以便在Qstring中输入,并提到了QString::arg()函数的使用,该函数用于格式化字符串,参数包括字符宽度、进制和填充字符。
摘要由CSDN通过智能技术生成
整体程序:
#include "tablewidget.h" 
#include <QtWidgets>
#include <QFile>
#include <vector>
#include<iostream>
#include <fstream>


void readData()				//读入一张图的数据,将txt的二维数据传输到二维数组中       
{
	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();
	}

}

int x=1;
int y=20;			//原本三维数组(225,355,33)转换成(85425,33)的矩阵
int location = (x-1)*355+y;<span style="white-space:pre">			</span>//225*355的图上坐标为(x,y)的点数据存储在第location行
double imageData[85425][33];		<span style="white-space:pre">	</span>//(1,1)的点在第一行,(1,2)的点在第二行。。。(1,355)点在第355行,(2,1)点在第356行,以此类推

TableWidget::TableWidget()

{ 
        table = new QTableWidget(this); 
        table->setColumnCount(56); 
        table->setRowCount(60);			//创建了一个60*56的表格


        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"<&l
  • 2
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值