QT的路径格式使用 / 或 \
读写文本
1 2 QFile f("F:/src/1.txt"); 3 if(!f.open(QIODevice::WriteOnly | QIODevice::Text)) 4 { 5 qDebug() << "Open failed." << endl; 6 } 7 8 QTextStream txtOutput(&f); 9 QString s1("123"); 10 quint32 n1(123); 11 12 txtOutput << s1 << endl; 13 txtOutput << n1 << endl; 14 15 f.close(); 16 17 18 19 QFile f("F:/src/1.txt"); 20 if(!f.open(QIODevice::ReadOnly | QIODevice::Text)) 21 { 22 qDebug() << "Open failed." << endl; 23 } 24 25 QTextStream txtInput(&f); 26 QString lineStr; 27 while(!txtInput.atEnd()) 28 { 29 lineStr = txtInput.readLine(); 30 qDebug() << lineStr << endl; 31 } 32 33 f.close();
|
查找路径下所有特定文件(和文件夹)
1 void fllemanage::on_pushButton_clicked() 2 { 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 QStringList list = findALLfiles("C:/Users/00/Desktop/vsrun/123"); 24 25 26 27 QStringList purposelist; 28 purposelist = list.filter(".txt"); 29 int count = 0; 30 for (int i = 0; i < purposelist.size(); i++) 31 { 32 qDebug() << purposelist[i] << endl; 33 count++; 34 } 35 qDebug() << "the max file has " << list.size() << endl; 36 qDebug() << "the purpose file has " << count << endl; 37 38 39 40 41 42 43 QString toDir = "C:/Users/00/Desktop/123"; 44 45 46 QDir dir(toDir); 47 if (!dir.exists(toDir)) { 48 dir.mkdir(toDir); 49 } 50 51 QFile destFile(toDir); 52 bool success = true; 53 success &= destFile.open(QFile::WriteOnly | QFile::Truncate); 54 55 for (int i = 0; i < purposelist.size(); i++) 56 { 57 if (QFile::copy(purposelist[i], toDir)) { 58 { 59 qDebug() << QStringLiteral("复制成功"); 60 } 61 } 62 } 63 } 64 65 66 QStringList fllemanage::findALLfiles(const QString &dir_path) { 67 QStringList get_files; 68 QDir dir(dir_path); 69 if (!dir.exists()) 70 { 71 qDebug() << "it is not true dir_path"; 72 } 73 74 75 QDirIterator dir_iterator(dir_path, QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot, QDirIterator::Subdirectories); 76 77 while (dir_iterator.hasNext()) 78 { 79 dir_iterator.next(); 80 QFileInfo file_info = dir_iterator.fileInfo(); 81 QString files = file_info.absoluteFilePath(); 82 get_files.append(files); 83 } 84 85 return get_files; 86 }
|
QFile copy(报错未实现 QIodevce not opened)
更新:QFile copy 函数写法:
QFile::copy("F:/src/1.txt" , "F:/dst/2.txt");
copy函数不能对string类型的路径进行操作,可从QString转string操作再转QString:
|
std::string pur = purposelist[i].toStdString();
std::string sdir = toDir.toStdString(); int pos = pur.find_last_of('/'); std::string s(pur.substr(pos+1)); sdir.append("/"); sdir.append(s); QString qsdir = QString(QString::fromLocal8Bit(sdir.c_str())); qDebug()<<qsdir<<endl;
|