Qt 文件处理

1.删除目录下所有的文件

 

void deleteAllFiles(const QString& fileDir)
{
   
    
    QDir dir(fileDir);
    if(!dir.exists())
    {
        return;
    }
    
    QFileInfoList fileInfofList=dir.entryInfoList(QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot | QDir::NoSymLinks | QDir::AllDirs);
    
    foreach (QFileInfo fileInfo, fileInfofList) 
    {
        if(fileInfo.isDir())
        {
           deleteAllFiles(fileInfo.absoluteFilePath()); 
        }
        else if(fileInfo.isFile())
        {
            QFile file(fileInfo.absoluteFilePath());
            file.remove();
        }
            
        
    }
   
    QDir tempDir;
    tempDir.rmdir(fileDir);//删除空的目录
}

 2.QWidgetList导出excel

 

void ReportDlg::onExport()
{

     QString file_path = QFileDialog::getSaveFileName(this,"Excel file",qApp->applicationDirPath(),
                                                       tr("Files (*.csv)"));


       if(file_path.isEmpty())
       {
           return;
       }

       QFile file(file_path);
       if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
       {
            qDebug()<< "Cannot open file for writing";

            return;
       }

       QTextStream out(&file);
       int row_count=tw->rowCount();
       int col_count=tw->columnCount();
       QString tw_value;


       for(int h=0;h<col_count;h++)
       {

           tw_value=tw->horizontalHeaderItem(h)->text();
           out<<tw_value<<",";
       }
       out<<"\n";
       for(int i=0;i<row_count;i++)
       {

           for(int j=0;j<col_count;j++)
           {

               tw_value=tw->item(i,j)->text();
               out<<tw_value<<",";
           }
           out<<"\n";
       }

       file.close();


}

 

你可能感兴趣的:(Qt 文件处理)