Qt 读写文件,并显示到tablewidget

//将表格数据写入文件
void DiagramTableItem::saveTable()
{
    QString fileName = QFileDialog::getSaveFileName(0,tr("保存文件"),
                                                tr("/root/Projects/product/bin/config/未命名.xls"),
                                                tr("file(*.txt *.xls);;Allfile(*.*)"));
    if(!fileName.isEmpty())
    {
        //将表格数据写入文件
        if(QFileInfo(fileName).suffix().isEmpty())
        {
            fileName.append(".xls");
        }
        QFile file;
        file.setFileName(fileName);
        //打开文件
        if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
        {
            QMessageBox msg;
            msg.setText("打开文件失败!");
            msg.exec();
        }
        else
        {
            qDebug("open file success:::");
            QTextStream out(&file);
            int rowCount = m_tablewidget->rowCount();
            int colCount = m_tablewidget->columnCount();
            //将表头写入文件
            qDebug()<<"row count:::"<horizontalHeaderItem(i)->text();
                out << rowHeaderString << "\t";
            }
            out << "\n";
            for(int j = 0; j < rowCount; j++)
            {
                QString colHeaderString = m_tablewidget->verticalHeaderItem(j)->text();
                out << colHeaderString << "\t";
            }
            out << "\n";
            qDebug("saveTable start:::");
            //读取单元格的数据并写入文件
            qDebug()<<"row count:::"<item(row,col)->text();
                    qDebug()<<"text:::"<clear();
                    m_tablewidget->setRowCount(r_count-2);      //前两行是行列表头
                    m_tablewidget->setColumnCount(c_count-1);   //最后一行是“\t"
                    m_tablewidget->setHorizontalHeaderLabels(listRowHeader);
                    m_tablewidget->setVerticalHeaderLabels(listColHeader);
                    qDebug()<<"start importTable row count:::"<setItem(row-2, col, item);
                        }
                    }
                    QMessageBox::information(0,tr("提示"),tr("导入成功!"));
                }
            }
        }
    }
    else
    {
        QMessageBox::warning(0,tr("警告"),tr("文件名未指定!"),QMessageBox::Ok);
    }
}

你可能感兴趣的:(QT)