PDA

View Full Version : Copy file after remove



windkracht8
31st August 2006, 07:27
Can someone explain why this works:


sTemp = QDir::tempPath() + "/exfprogtemp.exf";
fTemp.setFileName(ui.eFile->text());
if(fTemp.exists(sTemp)) fTemp.remove(sTemp);
if(!fTemp.copy(ui.eFile->text(), sTemp)) fout("Can't create local temporary file");


But this doesn't:


sTemp = QDir::tempPath() + "/exfprogtemp.exf";
if(fTemp.exists(sTemp)) fTemp.remove(sTemp);
if(!fTemp.copy(ui.eFile->text(), sTemp)) fout("Can't create local temporary file");


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.

wysota
31st August 2006, 10:42
What is fTemp in the second example? What is it set to?

windkracht8
2nd September 2006, 17:35
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.

wysota
3rd September 2006, 00:59
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?

windkracht8
3rd September 2006, 17:36
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:


QDir dTemp;
dTemp.remove("file1");
dTemp.rename("file1", "file2");


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.

wysota
3rd September 2006, 18:15
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?

windkracht8
4th September 2006, 07:57
I'm using Qt4.1.

This works as well:


QString sTemp,sTemp2;
QFile fIn,fUit;
sTemp = fIn.fileName();
sTemp2 = fUit.fileName();
QFile::remove(sTemp);
QFile::rename(sTemp2, sTemp);


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.

wysota
4th September 2006, 10:53
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.

windkracht8
4th September 2006, 14:17
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.

wysota
4th September 2006, 16:34
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.