QFile和QDir

    QString path = QString(R"(D:\JA\model.jatmod)");
    QFile file(path);
    //文件不存在会自动创建,但不会创建文件夹
    if(file.open(QIODevice::WriteOnly);){
    //写文件的方式
    file.write("hello\n");
    file.close();
    }


    if(file.open(QIODevice::ReadOnly);){
    //读文件的方式
    //全文读
    QByteArray text = file.readAll();
    //字节流读
    QTextStream inFile(&file);
    inFile.setCodec("UTF-8");
    QString strLine = inFile.readAll();
    ui->textEdit->setPlainText(strLine);
    //单行读
    while(!inFile.atEnd())
    {
        strLine = inFile.readLine();
        ui->textEdit->append(strLine);
    }
    
    file.close();
    }

    QString sourceFolder = QFileDialog::getExistingDirectory(nullptr, "选择文件夹", "", QFileDialog::ShowDirsOnly);
        if (sourceFolder.isEmpty()) {
            return 0; // 用户取消操作
        }

        QString destinationFolder = QFileDialog::getExistingDirectory(nullptr, "选择保存位置", "", QFileDialog::ShowDirsOnly);
        if (destinationFolder.isEmpty()) {
            return 0; // 用户取消操作
        }

QFileInfo读取文件信息

    QString path = QString("E:/test/s.txt");
    QFile file(path);
    if(file.open(QIODevice::WriteOnly)){
        file.write("hello\n");
        file.write("world\n");
        file.close();
    }
    QFileInfo info(file);
    qDebug()<<info.fileName();
    qDebug()<<info.path();
    qDebug()<<info.absoluteFilePath();
    qDebug()<<info.absolutePath();
    qDebug()<<info.size();

"s.txt"
"E:/test"
"E:/test/s.txt"
"E:/test"
12

QAppllication::appllicationDirPath()

     而无论你通过何种途径去运行exe文件,QAppllication::appllicationDirPath()的路径始终都是exe文件所在的绝对路径。

"exe文件所在目录:" "D:/Qt/build-treeFile-Desktop_Qt_5_4_2_MinGW_32bit-Debug/debug"

../代表上级目录。/代表根目录

       QString str = "Some text  begin value1 end , another text  begin value2 end , and so                 on.";
       // 定义正则表达式,匹配所有 (begin) 和 (end) 之间的内容
       QRegularExpression regex(R"(begin(.*?)end)");
       // 创建正则表达式匹配对象
       QRegularExpressionMatchIterator matchIterator = regex.globalMatch(str);
       // 遍历所有匹配项
       while (matchIterator.hasNext()) {
           QRegularExpressionMatch match = matchIterator.next();
           QString extractedValue = match.captured(1); // 提取捕获组1中的内容
           qDebug() << "Extracted value:" << extractedValue;
       }


       QRegularExpressionMatch match = regex.match(str);
       if(match.hasMatch()){
           QString extractedValue = match.captured(1); // 提取捕获组1中的内容
           qDebug() << "Extracted value:" << extractedValue;
       }
 QRegularExpression regex("TYPE=([^\\s]+)");
[^\\s]:这是一个字符类(character class),表示匹配任何非空白字符。^ 在字符类内部表示取反,所以 [^\\s] 表示除了空白字符外的任意字符。
+:这是一个量词,表示前面的模式至少出现一次。也就是说,它匹配一个或多个非空白字符。
 QRegularExpression regex3("RANGE=\\[\\]");
原始字符串字面量R不会转义,需要加一个\,不用R加两个\\
QVariant var = QVariant::fromValue(QPair<QString,int>("name",18));
QPair<QString,int> pair = var.value<QPair<QString,int>>();
QString name = var.first;

 按照目录顺序创建文件夹

QString path = "D:\\tt\\model_manage_plugin\\x64";
QDir dir(path);
if(!dir.exists()){
    dir.mkpath(".");
}

更改文件名 

QDir dir(path);
if(dir.exists()){
    dir.rename(old_path,new_path);
}
QDir dir("");
dir.cdUp();
dir.cd("models");
QString path = dir.absolutePath();

递归删除文件夹

QDir dir(path);
bool flag = dir.removeRecursively();
    QString path = QString("E:/temp");
    QDir dir(path);
    if(!dir.exists()){
        dir.mkdir(path);
    }
    QStringList list = dir.entryList();
    for(int i = 0;i<list.count();++i){
        qDebug()<<list[i];
    }

"."
".."

QStringList list = dir.entryList(QDir::Dirs|QDir::NoDotAndDotDot);
QFileInfoList list = dir.entryInfoList(QStringList()<<"*.cpp"<<"*.h",QDir::Files);

QMap

 QMapIterator<QString, QString> it(replacementMap);
    while (it.hasNext()) {
        it.next();
        QString original = "@" + it.key() + "@";
        QString replacement = it.value();
        result.replace(original, replacement);
    }
QDir::currentPath() 返回一个 QString 对象,包含当前应用程序的工作目录的路径。
这个路径通常是启动应用程序时操作系统指定的路径,一般是应用程序的执行路径。

在 Windows 上,如果你的应用程序在 C:\Users\Username\Documents\MyApp\ 目录下运行,那么 QDir::currentPath() 将返回 C:\Users\Username\Documents\MyApp\。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值