Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946
Copying the file content
-
For copyint one txt content, to another txt writing that code script is enough?
It does not work ı guess there might be somethings elseQFile::copy(old file path, new file path);
-
Yes, this code is enough to copy a file.
It will not add contents from source file to destination. It copies the whole file.
-
@suslucoder This copies a file (doesn't matter what it contains).
If you want us to tell you why it does not work then please provide more information: what exactly is the old and the new paths?
-
@jsulm ```
"C:/Users/user/Documents/MyThread/deneme.txt"
This is my old file path, the new one is in different name
-
@suslucoder And what exactly does not work: it does not copy the file?
Please post the new apth also, so we can make sure it is not wrong.
Does the old file exist?
Also, what does copy return (true/false)?
-
@jsulm It does not copy the file. It is in my thread.
void MyThread::writeData() { QFile::copy("C:/Users/user/Documents/MyThread/deneme.txt", "C:/Users/user/Documents/MyThread/new.txt"); if(!file.open(QIODevice::ReadOnly)) { QTextStream in(&file2); while(!in.atEnd()) { QString line2=in.readAll(); QStringList list = line2.split(QLatin1Char(' '), Qt::SkipEmptyParts); for(const QString &entry : list) { double num = entry.toDouble(); queue.enqueue(num); qDebug() << num; } } } qDebug() << "im working in writing thread"; emit writingDone();
}
qDebug does not give anytghing on the secreen. And yes the old file exist. I can read its contents
-
@suslucoder said in Copying the fle content:
It does not copy the file
That I already understood.
Again: what does QFile::copy return?I also do not understand what how this file copy is related to the other code:
if(!file.open(QIODevice::ReadOnly)) // What is file here? What file are you trying to pen here?
-
@suslucoder said in Copying the fle content:
qDebug does not give anytghing on the secreen
This means that
MyThread::writeData()
function is not called at all! And that implies the problem lies somewhere else (not inQFile::copy()
).
-
@sierdzio I've already called it with start() function. Doesnt it started the thread?
-
Yes,
start()
starts the thread, but it does not mean yourwriteData()
is called. If that were true, you would see yourqDebug()
output, because it is not in anyif
conditions - it should be printed every timewriteData()
is called.
-
@sierdzio yes you are right. I've called the function. Thread is working now. But copying issue does not work.
-
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
-
@sierdzio I just want, when writeData() thread is called, copy the contents into new txt.
-
@suslucoder
QThread has a started signal you connect that signal to your slot
-
@J-Hilk My thread has working now. But copying does not occur
-
@suslucoder So, what does copy() return?
-
@jsulm it returns nothing, Either true or false
-
@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 ?
-
@sierdzio
They exists.
-
@SGaist , @sierdzio , @J-Hilk
IfQFile::copy()
returns false, I don't see anywhere the user can obtain any kind of error message/reason for the failure. Does Qt not supply this by any means?
-
@SGaist I fixed it
-
@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...
-
@sierdzio I'll try it again thank you
-
@suslucoder it works. thank you