Copying the file content
-
I don't know either, you have not shown any code or stated how you want it all to work... so all I can say is "read the docs" :D QThread has a nice example and detailed description: https://doc.qt.io/qt-5/qthread.html#details
-
@suslucoder
QThread has a started signal you connect that signal to your slot -
@suslucoder So, what does copy() return?
-
@suslucoder said in Copying the file content:
it returns nothing, Either true or false
?
It returns a boolean.
Again: what does it return?!qDebug() << QFile::copy("C:/Users/user/Documents/MyThread/deneme.txt", "C:/Users/user/Documents/MyThread/new.txt");
-
@suslucoder then the function isn't called.
Please post what you actually have in your code, otherwise no-one will be able to help you
-
@jsulm it returns false.
#include "mythread.h" #include "mainwindow.h" #include <QtCore> #include <QDebug> #include <QFile> #include <QTimer> #include <QThread> #include <QMutex> #include <QQueue> #include <QMessageBox> #include <QApplication> #include <QtWidgets/QApplication> #include <QtWidgets/QMainWindow> #include <QFileDialog> #include <QMainWindow> QFile file("C:/Users/ilknu/Documents/MyThread/deneme.txt"); MyThread::MyThread(QObject* parent) : QThread(parent) { writeData(); } MyThread::~MyThread() { } void MyThread::writeData() { QFile file2("C:/Users/ilknu/Documents/MyThread/new.txt"); QFile::copy("C:/Users/ilknu/Documents/MyThread/deneme.txt","C:/Users/ilknu/Documents/MyThread/new.txt" ); qDebug() << QFile::copy("C:/Users/user/Documents/MyThread/deneme.txt", "C:/Users/user/Documents/MyThread/new.txt"); qDebug() << "im working in writing thread"; emit writingDone(); } void MyThread::run() //Reading file from txt with thread1 { if (file.open(QIODevice::ReadOnly)) { QTextStream in (&file); while (!in.atEnd()) { QString line = in.readAll(); QStringList list = line.split(QLatin1Char(' '), Qt::SkipEmptyParts); for(const QString &entry : list) { double num = entry.toDouble(); queue.enqueue(num); } qDebug()<< "im running on worker thread " <<line; } // for } // while file.close(); } // if
-
Check if it exists:
qDebug() << "Source exists?" << QFile::exists("C:/Users/ilknu/Documents/MyThread/deneme.txt"); qDebug() << "Destination exists?" << QFile::exists("C:/Users/ilknu/Documents/MyThread/new.txt");
You need to check destination because:
If a file with the name newName already exists, copy() returns false (i.e., QFile will not overwrite it).
-
@suslucoder
Did you read the documentation ofQFile::Copy()
for yourself?Note that if a file with the name newName already exists, copy() returns false (i.e. QFile will not overwrite it).
So, does the destination file already exist? Does the source file also exist?
-
Hi,
@suslucoder said in Copying the file content:
QFile::copy("C:/Users/ilknu/Documents/MyThread/deneme.txt","C:/Users/ilknu/Documents/MyThread/new.txt" );
qDebug() << QFile::copy("C:/Users/user/Documents/MyThread/deneme.txt", "C:/Users/user/Documents/MyThread/new.txt");You do not have the same paths in the qDebug and the previous line.
You do realise that you are copying it twice ? -
@suslucoder said in Copying the file content:
They exists.
And? We have all said
QFile::copy()
says:Note that if a file with the name newName already exists, copy() returns false (i.e. QFile will not overwrite it).
So you (not we) need to do something about that.
Also, as @SGaist has pointed out, you don't even have the same paths on your two
QFile::copy()
calls. -
Then the cause is clear: destination exists, so copy() will not overwrite it. You need to remove the destination file first, before calling copy().
-
@JonB said in Copying the file content:
Also, as @SGaist has pointed out, you don't even have the same paths on your two
QFile::copy()
calls. I've fixed it, it is my mistake.Even i delete the destination file, for returning true by creating new destination, it returns false.
-
@suslucoder Did you check what @SGaist said? Do NOT call copy twice! Call it once like before but put qDebug() in front of it...