3

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.

Here's the complete source code:

#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?

nbro
  • 15,395
  • 32
  • 113
  • 196
hardc0der
  • 31
  • 4

1 Answers1

0

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. Open source file for reading
  2. Open (create) destination file for writing.
  3. copy all data from first file to second one.

Far from perfect but sometimes it works.