QFile :: copy静态函数不复制文件 [英] QFile::copy static function not copying file

查看:82
本文介绍了QFile :: copy静态函数不复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要使用以下代码用 QFile 复制文本文件:

I want to copy a text file with QFile with this code:

void MainWindow::on_pushButton_4_clicked()
{
    QFile::copy("C:/p/text.txt", "C:/p/text1.txt");
}

构建它时没有任何错误,但是当我运行该程序时,什么也没发生.

I get no errors when I build it, but when I run the program, nothing happens.

这是完整的源代码:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QfileDialog>
#include <QFile>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

int currentIndex = 0;

void MainWindow::on_pushButton_2_clicked()
{
    ui->lineEdit->setText(QFileDialog::getExistingDirectory());
}

void MainWindow::on_pushButton_clicked()
{
    currentIndex ++;
    ui->stackedWidget->setCurrentIndex(currentIndex);
}

void MainWindow::on_pushButton_3_clicked()
{
    currentIndex --;
    if(currentIndex < 0)
    {
        currentIndex ++;
    }
    ui->stackedWidget->setCurrentIndex(currentIndex);
}

void MainWindow::on_pushButton_4_clicked()
{
    QFile::copy("C:/p/text.txt", "C:/p/text1.txt");
}

什么可能导致这种奇怪的行为?

What could cause this strange behaviour?

推荐答案

我注意到了同样的问题,而且似乎copy()的实现也不是很好.它以某种方式认为您没有足够的复制权限,即使您确实有.Windows权限冲突.

I've noticed same problem and it seems that implementation of copy() is not good. It somehow think you have no enough permission to copy even if you do have. Some Windows permission conflict.

没有好的解决方法,但是您可以尝试通过重做整个过程来复制文件(有时可以):

There is no good workaround but you might try to copy file by redoing whole process (it works sometimes):

  1. 用于读取的开源文件
  2. 打开(创建)目标文件以进行写入.
  3. 将所有数据从第一个文件复制到第二个文件.

远非完美,但有时可行.

Far from perfect but sometimes it works.

这篇关于QFile :: copy静态函数不复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆