Results 1 to 20 of 37

Thread: QFile::copy copies only 3kb

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2011
    Posts
    53
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QFile::copy copies only 3kb

    Hi I've noticed a very strange problem a use QFileSystemWatcher to monitor one folder, when new files are copied to this folder the signal directoryChanged is emitted. I wrote a slot, which works well, it gives me the confidence that when ALL files are copied to the folder only then the right action is taken:

    Qt Code:
    1. void DServer::newFiles_()
    2. {
    3. QDir input(QDir::homePath() + "/INPUT DICOM");
    4.  
    5. if(input.count() > lastInput)
    6. {
    7. QTextStream(stdout)<<"not all files" << lastInput <<endl;
    8. lastInput = input.count();
    9. return;
    10. }
    11. else
    12. {
    13. QDir inputDir(QDir::homePath() + "/INPUT DICOM");
    14. QTextStream(stdout) << "BEFORE IF "<<inputDir.count() <<endl;
    15. if(inputDir.count() > 1)
    16. {
    17. QTextStream(stdout)<<"Got all files"<< lastInput <<endl;
    18. inputFiles = getPaths(QDir::homePath() + "/INPUT DICOM");
    19. emit this->_moveFiless();
    20. lastPatientName.clear();
    21. lastInput = 1;
    22. }
    23. else
    24. return;
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

    However this slot
    Qt Code:
    1. void DServer::moveFiless_()
    2. {
    3. QString input("/INPUT DICOM");
    4. QDate date = QDate::currentDate();
    5. QTime time = QTime::currentTime();
    6.  
    7. for(int i=0; i<inputFiles.count(); i++)
    8. {
    9. QString old_input_list = inputFiles[i];
    10. inputFiles[i].remove(0,(QDir::homePath().count()) + input.count());
    11.  
    12. int length = inputFiles[i].length();
    13. int startindex = inputFiles[i].lastIndexOf("/",-1) + 1;
    14. QString dir_path = inputFiles[i];
    15.  
    16. dir_path.remove(startindex, length);
    17.  
    18. QDir dir;
    19. if(dir.mkpath(QDir::homePath() + "/DICOM STORAGE/" + date.toString("dd.MM.yyyy") + "/" + time.toString("hh:mm:ss") + dir_path) == true)
    20. {
    21. //anonymize here
    22. anonymous("/" + date.toString("dd.MM.yyyy") + "/" + time.toString("hh:mm:ss") + inputFiles[i], old_input_list);
    23. dir.mkpath(QDir::homePath() + "/TEMPORARY STORAGE/" + date.toString("dd.MM.yyyy") + "/" + time.toString("hh:mm:ss") + dir_path);
    24. date.toString("dd.MM.yyyy") + "/" + time.toString("hh:mm:ss") + input_list[i] + ".tmp");
    25.  
    26. QFile::copy(old_input_list, (QDir::homePath() + "/TEMPORARY STORAGE/" + date.toString("dd.MM.yyyy") + "/" + time.toString("hh:mm:ss") + inputFiles[i] + ".tmp"));
    27. QFile::copy(old_input_list, (QDir::homePath() + "/DICOM STORAGE/" + date.toString("dd.MM.yyyy") + "/" + time.toString("hh:mm:ss") + inputFiles[i]));
    28. QFile::remove(old_input_list);
    29. QTextStream(stdout)<<"source file: "<< old_input_list <<endl;
    30. dir.rmpath(QDir::homePath() + "/INPUT DICOM" + dir_path);
    31. }
    32. else
    33. {
    34. QTextStream(stdout) <<"Couldn't make this dir: ' "<< QDir::homePath() + "/DICOM STORAGE" + dir_path <<endl;
    35. }
    36. }
    37. }
    To copy to clipboard, switch view to plain text mode 
    doesn't work well, the files from source are copied but they only have from 2.6-3.0 kB, when the original ones have about 80kB, that means the QFile::copy works but not good.

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::copy copies only 3kb

    Just reread your post..

    Is "if(input.count() > lastInput)" really correct? Don't you expect the input.count() to increase until all files have been copied?

    In that case you have to keep waiting while it is below the threshold lastinput.

    Joh

  3. #3
    Join Date
    Mar 2011
    Posts
    53
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::copy copies only 3kb

    But the signal directoryChanged() is emitted many times during copying the files to the monitored folder and as far as I know I'm interested in the last one becouse it indicates that the last file occured in the watched folder and after that I am performing the copy action. if(input.count > lastinput) informs if current number of files in the monitored directory is greater then it was during the last run of this slot, so that means the files are still being copied to the folder.

    I tried even sleep for 5 seconds to be sure that all files are in the directory but still all files are copied but only 2.8kB of them.
    Last edited by camol; 8th April 2011 at 13:02.

  4. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::copy copies only 3kb

    Ah! Sorry about that. Now I get it. But it wont work like that, because QFileSystemWatcher fires also when a file is modified. Thus during the copy process your method will be executed multiple times for each file. The watcher supresses to frequent updates but nonetheless: You will get updates where the filecount has not changed but only the file has been modified (a few additional bytes have been copied). Thus you think the file-list is already copied completely and make a copy of incomplete files.

    Joh

  5. #5
    Join Date
    Mar 2011
    Posts
    53
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::copy copies only 3kb

    Do you have any idea to solve that? I really need this functionality which I have described above

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QFile::copy copies only 3kb

    Connect a timer to the file watcher and reset the timer whenever you receive a signal from the watcher. At some point you will stop receiving signals and the timer will timeout. At this point you will know that for some time there haven't been any updates to the file. This is not foul proof but should be enough for you.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Mar 2011
    Posts
    53
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::copy copies only 3kb

    Can you bring some example code of this becouse I am trying to imagine the situation but I can't figure how it should work this. Big thanks in advance for your time here for both of you.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QFile::copy copies only 3kb

    Qt Code:
    1. void Class::setup() {
    2. timer.setSingleShot(true);
    3. timer.setInterval(1000); // 1s
    4. connect(...);
    5. }
    6.  
    7. void Class::onSignalFromFileSystemWatcher() {
    8. timer.start();
    9. }
    10.  
    11. void Class::onTimerTimeout() {
    12. QFile::copy(...);
    13. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Mar 2011
    Posts
    53
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::copy copies only 3kb

    It didn't work still is copied only about 3kB of each file, and also this solution makes sth that I wanted to prevent my application from. Becouse I copy the files from monitored folder to folder created which name is the current time and now for example if I put many files in the monitored folder the whole operation lasts long and the result of that is 2-3 folders with names with different times. My code above didn't do this it only created one folder for each operation of putting files to the monitored folder

  10. #10
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::copy copies only 3kb

    I would suggest, you drop this. And provide the user with a simple button, where he can trigger an import after having copied all files.

    Your concept of monitoring will fail because of so many things. How can you be sure, that the user will not copy files in several packets? That the source medium won't take 10secs to spin up. That the user realizes he has got the wrong files...

    What is this for anyway?

    Joh

  11. #11
    Join Date
    Mar 2011
    Posts
    53
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::copy copies only 3kb

    I am building a server aplication which runs and stays on all the time. It stores files in hierachy of folders thats why when a user brings new files for the server the server is automaticaly archiving them in the right order without any other actions from the user becouse it is an non-gui application. That it is why I need it so much.

    I've tried this at the beginning of the slot connected to directoryChanged:
    this->inputWatcher->blockSignals(true); //to prevent more the one signal
    sleep(40); //to make sure that all files are in the watched folder


    but still it doesn't work


    I don't get it I tried even to make this functionality with a timer but it still the same
    Last edited by camol; 9th April 2011 at 17:37.

Similar Threads

  1. QFile &QFile::operator= is private
    By Fallen_ in forum Newbie
    Replies: 1
    Last Post: 15th March 2011, 15:08
  2. QTemporaryFile and Qfile::copy()
    By ChrisW67 in forum Newbie
    Replies: 2
    Last Post: 23rd April 2009, 23:32
  3. QPrintDialog: settings number of copies
    By nedlab in forum Qt Programming
    Replies: 1
    Last Post: 2nd December 2008, 19:22
  4. copies file doesn't work well
    By dreamer in forum Qt Programming
    Replies: 3
    Last Post: 10th May 2008, 22:00
  5. How many copies of Signal parameters
    By Doug Broadwell in forum Newbie
    Replies: 3
    Last Post: 2nd December 2006, 21:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.