Results 1 to 10 of 10

Thread: Copy file after remove

  1. #1
    Join Date
    Aug 2006
    Posts
    27
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Copy file after remove

    Can someone explain why this works:
    Qt Code:
    1. sTemp = QDir::tempPath() + "/exfprogtemp.exf";
    2. fTemp.setFileName(ui.eFile->text());
    3. if(fTemp.exists(sTemp)) fTemp.remove(sTemp);
    4. if(!fTemp.copy(ui.eFile->text(), sTemp)) fout("Can't create local temporary file");
    To copy to clipboard, switch view to plain text mode 

    But this doesn't:
    Qt Code:
    1. sTemp = QDir::tempPath() + "/exfprogtemp.exf";
    2. if(fTemp.exists(sTemp)) fTemp.remove(sTemp);
    3. if(!fTemp.copy(ui.eFile->text(), sTemp)) fout("Can't create local temporary file");
    To copy to clipboard, switch view to plain text mode 

    As far as I can tell the problem is that Qt can not handle to much copy/move/remove of a file if you haven't set a filename.
    Is this how it's intended?

    For me I would like to see the option to move/copy/remove a file without the need to create an QFile object.

    Thanks in advance,
    Bart.

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

    Default Re: Copy file after remove

    What is fTemp in the second example? What is it set to?

  3. #3
    Join Date
    Aug 2006
    Posts
    27
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Copy file after remove

    It's just used to remove a file and copy another over it.
    The two examples are supposed to do the same, only the second crashes.

    Cheers,
    Bart.

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

    Default Re: Copy file after remove

    But what kind of object it is? QDir? QFile? And what does it point to? It seems you are using it like a QDir object but what is it initialised to?

  5. #5
    Join Date
    Aug 2006
    Posts
    27
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Copy file after remove

    Declaration of the variables is like this:

    QString sTemp;
    QFile fTemp;
    QLineEdit eFile;

    and fout() is something like this:

    void frmMain::fout(QString error){
    fprintf(stderr, "Error Message: " + error.toAscii() + "\n");
    }

    It doesn't give any compile error, it runs, but crashes if I copy over an existing file, after removing it with the same QFile object, which doesn't have a filename set.

    But maybe I should use a QDir object, this code works:
    Qt Code:
    1. QDir dTemp;
    2. dTemp.remove("file1");
    3. dTemp.rename("file1", "file2");
    To copy to clipboard, switch view to plain text mode 

    And I don't have to set an directoryname for the QDir object for it to work.

    Still it's strange behaviour for QFile. It should not crash.
    Bug in Qt I imagine, where can I post this?

    Cheers,
    Bart.

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

    Default Re: Copy file after remove

    First of all QFile::exists(const QString &) and QFile::remove(const QString &) are both static and you are using them as regular methods. Maybe that's causing the problems you have.

    BTW. Which Qt release are you using? Did you have a look at the task-tracker to see if a simmilar problem was reported?

  7. The following user says thank you to wysota for this useful post:

    frankiefrank (19th April 2011)

  8. #7
    Join Date
    Aug 2006
    Posts
    27
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Copy file after remove

    I'm using Qt4.1.

    This works as well:
    Qt Code:
    1. QString sTemp,sTemp2;
    2. QFile fIn,fUit;
    3. sTemp = fIn.fileName();
    4. sTemp2 = fUit.fileName();
    5. QFile::remove(sTemp);
    6. QFile::rename(sTemp2, sTemp);
    To copy to clipboard, switch view to plain text mode 

    I didn't realize there static.

    No, I did not look at the task-tracker if a similar problem is reported, I don't know where it is.
    And if this is something that needs to be fixed.
    But a warning of some kind would have helpt me, normally I don't look if a function is static.

    Thanks,
    Bart.

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

    Default Re: Copy file after remove

    Quote Originally Posted by windkracht8
    But a warning of some kind would have helpt me, normally I don't look if a function is static.
    Marking the function as static is a good warning. Don't blaim Qt, it is you who misused the API.

    And the tasktracker is here.

  10. #9
    Join Date
    Aug 2006
    Posts
    27
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Copy file after remove

    I didn't know static members can not be used as non-static members.
    This is my fault, I agree.

    I did know you can't use non-static members as static ones. But using static as non-static is ok by the c++ standard. And I have never seen a remark that Qt doesn't support this.

    But maybe I should have done a tutorial or something.

    Thanks for your explanation,
    Bart.

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

    Default Re: Copy file after remove

    Quote Originally Posted by windkracht8
    I didn't know static members can not be used as non-static members.
    Sometimes you can, sometimes you can't.
    I did know you can't use non-static members as static ones. But using static as non-static is ok by the c++ standard. And I have never seen a remark that Qt doesn't support this.
    I guess they are static for a reason (they are provided for convenience). There are non-static equivalents available.

Similar Threads

  1. Copy progress bar
    By safknw in forum Newbie
    Replies: 13
    Last Post: 16th September 2006, 09:02
  2. File permission QFile::WriteOther on Win Dos
    By patrik08 in forum Newbie
    Replies: 1
    Last Post: 13th June 2006, 14:16
  3. file copy in Qt 3.3.4 ?
    By npc in forum Newbie
    Replies: 6
    Last Post: 31st March 2006, 14:43
  4. Replies: 4
    Last Post: 24th February 2006, 10:30
  5. QSettings again ... how to remove array elements
    By Mike in forum Qt Programming
    Replies: 4
    Last Post: 11th January 2006, 08:58

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.