Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Copying the file content

Copying the file content

Scheduled Pinned Locked Moved Solved General and Desktop
copyfileqt5file readfile write
32 Posts 6 Posters 10.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J.HilkJ J.Hilk

    @suslucoder
    QThread has a started signal you connect that signal to your slot

    D Offline
    D Offline
    deleted286
    wrote on last edited by
    #15

    @J-Hilk My thread has working now. But copying does not occur

    jsulmJ 1 Reply Last reply
    0
    • D deleted286

      @J-Hilk My thread has working now. But copying does not occur

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #16

      @suslucoder So, what does copy() return?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      1
      • jsulmJ jsulm

        @suslucoder So, what does copy() return?

        D Offline
        D Offline
        deleted286
        wrote on last edited by
        #17

        @jsulm it returns nothing, Either true or false

        jsulmJ J.HilkJ 2 Replies Last reply
        0
        • D deleted286

          @jsulm it returns nothing, Either true or false

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #18

          @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");
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          D 1 Reply Last reply
          0
          • D deleted286

            @jsulm it returns nothing, Either true or false

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #19

            @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


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            0
            • jsulmJ jsulm

              @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");
              
              D Offline
              D Offline
              deleted286
              wrote on last edited by
              #20

              @jsulm it returns false. Ekran görüntüsü 2021-01-11 111741.png

              #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
              
              JonBJ SGaistS 2 Replies Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #21

                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).

                (Z(:^

                D 1 Reply Last reply
                1
                • D deleted286

                  @jsulm it returns false. Ekran görüntüsü 2021-01-11 111741.png

                  #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
                  
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #22

                  @suslucoder
                  Did you read the documentation of QFile::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?

                  1 Reply Last reply
                  1
                  • D deleted286

                    @jsulm it returns false. Ekran görüntüsü 2021-01-11 111741.png

                    #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
                    
                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #23

                    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 ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    D 1 Reply Last reply
                    4
                    • sierdzioS sierdzio

                      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).

                      D Offline
                      D Offline
                      deleted286
                      wrote on last edited by
                      #24

                      @sierdzio
                      They exists.

                      Ekran görüntüsü 2021-01-11 112331.png

                      JonBJ sierdzioS 2 Replies Last reply
                      0
                      • JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #25

                        @SGaist , @sierdzio , @J-Hilk
                        If QFile::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?

                        1 Reply Last reply
                        0
                        • SGaistS SGaist

                          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 ?

                          D Offline
                          D Offline
                          deleted286
                          wrote on last edited by
                          #26

                          @SGaist I fixed it

                          1 Reply Last reply
                          0
                          • D deleted286

                            @sierdzio
                            They exists.

                            Ekran görüntüsü 2021-01-11 112331.png

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #27

                            @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.

                            D 1 Reply Last reply
                            1
                            • D deleted286

                              @sierdzio
                              They exists.

                              Ekran görüntüsü 2021-01-11 112331.png

                              sierdzioS Offline
                              sierdzioS Offline
                              sierdzio
                              Moderators
                              wrote on last edited by
                              #28

                              @suslucoder

                              Then the cause is clear: destination exists, so copy() will not overwrite it. You need to remove the destination file first, before calling copy().

                              (Z(:^

                              D 1 Reply Last reply
                              1
                              • JonBJ JonB

                                @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.

                                D Offline
                                D Offline
                                deleted286
                                wrote on last edited by
                                #29

                                @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.

                                jsulmJ 1 Reply Last reply
                                0
                                • D deleted286

                                  @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.

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #30

                                  @suslucoder Did you check what @SGaist said? Do NOT call copy twice! Call it once like before but put qDebug() in front of it...

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  0
                                  • sierdzioS sierdzio

                                    @suslucoder

                                    Then the cause is clear: destination exists, so copy() will not overwrite it. You need to remove the destination file first, before calling copy().

                                    D Offline
                                    D Offline
                                    deleted286
                                    wrote on last edited by
                                    #31

                                    @sierdzio I'll try it again thank you

                                    D 1 Reply Last reply
                                    0
                                    • D deleted286

                                      @sierdzio I'll try it again thank you

                                      D Offline
                                      D Offline
                                      deleted286
                                      wrote on last edited by
                                      #32

                                      @suslucoder it works. thank you

                                      1 Reply Last reply
                                      0

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved