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);//删除空的目录 }
删除超过多长时间的旧文件。toTime_t() 秒
const int OVERTIME=60*60*24*30; void MainWindow::deleteOldFile(QString fileDir) { QDir dir(fileDir); if(dir.exists()) { dir.setFilter(QDir::Files | QDir::NoSymLinks); QDateTime nowDateTime = QDateTime::currentDateTime(); QFileInfoList list = dir.entryInfoList(); for (int i = 0; i < list.size(); ++i) { QFileInfo fileInfo = list.at(i); //秒 int passTime=nowDateTime.toTime_t()-fileInfo.created().toTime_t(); if(passTime>OVERTIME) { QFile file(fileInfo.absoluteFilePath()); file.remove(); } } } }
QString fileDir="/home/likewei/公共的/dispatch_log";
deleteOldFile(fileDir);
2.读文件
QFile file(yamlFile); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { log::d("yamle open file fail!"); return false; } QTextStream in(&file); while (!in.atEnd()) { QString line = in.readLine(); log::d(line); } file.close ();
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<<" "; 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<<" "; } file.close(); }
3.记录错误信息,每天用新的文件记录信息,以防止记录的信息太多。
void MainWindow::log(QString msg) { QString fileName=QDateTime::currentDateTime ().toString ("yyyy_MM_dd")+".log"; QString currentDir= QCoreApplication::applicationDirPath()+"/logs"; QDir dir; if(!dir.exists (currentDir)) { dir.mkpath (currentDir); } fileName=currentDir+"/"+fileName; QFile file(fileName); if(!file.exists (fileName)) { file.setFileName (fileName); } file.open(QIODevice::WriteOnly|QFile::Append); QTextStream in(&file); in<<QDateTime::currentDateTime ().toString ("[hh:mm:ss:zzz]")+" "+msg+" "; file.close (); }
4.选择文件对话框
QString defaultDir=QCoreApplication::applicationDirPath()+"/data"; QString fileName= QFileDialog::getOpenFileName( this, "Import File", defaultDir, "file (*.txt *.bin);;all files (*.*)"); if(!fileName.isEmpty()) { emit sigImportFile(fileName); }
5.在指定的目录查找指定的文件
QString binFileDir=QCoreApplication::applicationDirPath()+"/file"; QDir *dir=new QDir(binFileDir); if(!dir->exists()) { qDebug()<<"no found dir"; return -1; } //只查找设置过滤的文件,指定多个过滤,这样写filter<<"*.dat"<<"*.txt QStringList filter; filter<<"*.bin"; dir->setNameFilters(filter); QList<QFileInfo> *fileInfo=new QList<QFileInfo>(dir->entryInfoList(filter)); int fileCount=fileInfo->count(); qDebug()<<"fileCount"<<fileCount; for(int i=0;i<fileCount;i++) { qDebug()<<"fileName:"<<fileInfo->at(i).fileName(); qDebug()<<"filePath:"<<fileInfo->at(i).filePath(); }