QT TableWidget的使用1:簡單使用
阿新 • • 發佈:2019-02-14
其實就是幾行.
建立行,從第一行開始建立
ui->tableWidget->insertRow(i); //i代表行數,從0開始
ui->tableWidget->setItem(i,j,newQTableWidgetItem(QString::number(D[i].x))); 第I行第J列,最後傳的數值只能為QString
一個例項,從excel中讀取資料並放進QTableWidget顯示
structdata{
intx;
inty;
QStringmonth;
QStringday;
floatFFMC;
floatDMC;
floatDC;
floatISI;
floattemp;
intRH;
floatwind;
floatrain;
floatarea;
}D[1000];
MainWindow::MainWindow(QWidget*parent):
QMainWindow(parent),
ui(newUi::MainWindow)
{
ui->setupUi(this);
QAxObjectexcel("Excel.Application");
excel.setProperty("Visible",false);
QAxObject*workbooks=excel.querySubObject("WorkBooks");
workbooks->dynamicCall("Open(constQString&)",QString("c:/dd.xls"));
QAxObject*workbook=excel.querySubObject("ActiveWorkBook");//獲取活動工作簿
QAxObject*worksheets=workbook->querySubObject("WorkSheets");//獲取所有的工作表,如圖
intintCount=worksheets->property("Count").toInt();//獲取了表的個數
//qDebug()<<intCount;//輸出工作表的個數
QAxObject*worksheet=workbook->querySubObject("WorkSheets(int)",1);//獲取第一個工作表
//QAxObject*range=worksheet->querySubObject("Cells(int,int)",1,1);//獲取cell的值
QAxObject*used_range=worksheet->querySubObject("UsedRange");//獲得利用的範圍
QAxObject*rows=used_range->querySubObject("Rows");
QAxObject*columns=used_range->querySubObject("Columns");
introw_start=used_range->property("Row").toInt();//獲得開始行
//qDebug()<<row_start;//已經驗證準確
intcolumn_start=used_range->property("Column").toInt();//獲得開始列
introw_count=rows->property("Count").toInt();
//qDebug()<<row_count;//已經驗證準確
intcolumn_count=columns->property("Count").toInt();
//QStringstrVal=range->dynamicCall("Value2()").toString();
for(inti=row_start;i<=row_count;i++){
for(intj=column_start;j<=column_count;j++){
QAxObject*cell=worksheet->querySubObject("Cells(int,int)",i,j);
//QStringcell_value=cell->property("Value").toString();//不好使
//qDebug()<<i<<j<<cell->dynamicCall("Value2()").toString();
if(j==1){
D[i].x=cell->dynamicCall("Value2()").toInt();
}
if(j==2){
D[i].y=cell->dynamicCall("Value2()").toInt();
}
if(j==3){
D[i].month=cell->dynamicCall("Value2()").toString();
}
if(j==4){
D[i].day=cell->dynamicCall("Value2()").toString();
}
if(j==5){
D[i].FFMC=cell->dynamicCall("Value2()").toFloat();
}
if(j==6){
D[i].DMC=cell->dynamicCall("Value2()").toFloat();
}
if(j==7){
D[i].DC=cell->dynamicCall("Value2()").toFloat();
}
if(j==8){
D[i].ISI=cell->dynamicCall("Value2()").toFloat();
}
if(j==9){
D[i].temp=cell->dynamicCall("Value2()").toFloat();
}
if(j==10){
D[i].RH=cell->dynamicCall("Value2()").toInt();
}
if(j==11){
D[i].wind=cell->dynamicCall("Value2()").toFloat();
}
if(j==12){
D[i].rain=cell->dynamicCall("Value2()").toFloat();
}
if(j==13){
D[i].area=cell->dynamicCall("Value2()").toFloat();
}
}
}
//QTableWidget控制元件的相關設定
ui->tableWidget->setColumnCount(13);//設定9行
QStringListm_header;
m_header<<QString("1")<<QString("2")<<QString("3")<<QString("4")<<QString("5")<<QString("6")<<QString("7")<<QString("8")<<QString("9")<<QString("10")<<QString("11")<<QString("12")<<QString("13");
ui->tableWidget->setHorizontalHeaderLabels(m_header);
QTextCodec::setCodecForLocale(QTextCodec::codecForName("system"));
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);;
//顯示某塊
for(inti=row_start+1;i<=row_count;i++){
ui->tableWidget->insertRow(i-2);
ui->tableWidget->setItem(i-2,0,newQTableWidgetItem(QString::number(D[i].x)));
ui->tableWidget->setItem(i-2,1,newQTableWidgetItem(QString::number(D[i].y)));
ui->tableWidget->setItem(i-2,2,newQTableWidgetItem(D[i].month));
ui->tableWidget->setItem(i-2,3,newQTableWidgetItem(D[i].day));
ui->tableWidget->setItem(i-2,4,newQTableWidgetItem(QString::number(D[i].FFMC)));
ui->tableWidget->setItem(i-2,5,newQTableWidgetItem(QString::number(D[i].DMC)));
ui->tableWidget->setItem(i-2,6,newQTableWidgetItem(QString::number(D[i].DC)));
ui->tableWidget->setItem(i-2,7,newQTableWidgetItem(QString::number(D[i].ISI)));
ui->tableWidget->setItem(i-2,8,newQTableWidgetItem(QString::number(D[i].temp)));
ui->tableWidget->setItem(i-2,9,newQTableWidgetItem(QString::number(D[i].RH)));
ui->tableWidget->setItem(i-2,10,newQTableWidgetItem(QString::number(D[i].wind)));
ui->tableWidget->setItem(i-2,11,newQTableWidgetItem(QString::number(D[i].rain)));
ui->tableWidget->setItem(i-2,12,newQTableWidgetItem(QString::number(D[i].area)));
//qDebug()<<D[i].x<<D[i].y<<D[i].month<<D[i].day<<D[i].FFMC<<D[i].DMC<<D[i].DC<<D[i].ISI<<D[i].temp<<D[i].RH<<D[i].wind<<D[i].rain<<D[i].area;
//qDebug()<<endl;
}
}