PDA

View Full Version : How to copy a file from one directory to another with Qt3.3.3. ?



yellowmat
24th February 2006, 10:00
Hi,

I'm looking for a function to copy a file from a directory to another ... but this function is not located in that class.

Where could I find this function ?

Then I search a little bit and I discover a class nammed QUrlOperator, is that class could do what I want on a windows file system ? If yes how does it works ?

I code the two following slots

void testDialog::init()
{
QString message;
QUrlOperator op("c:\\user\\AFile.txt");
QUrlInfo fi = op.info("c:\\user\\AFile.txt");
message = "Filename = " + fi.name();
write(message);
}

void testDialog::write( const QString &line )
{
lb->insertItem(line);
lb->setBottomItem(lb->count()-1);
}


... but it does not write the file name, what's wrong with my code ?

Thanks in advance.

yellowmat
24th February 2006, 10:11
I made some changes to my code


QUrlOperator *op = new QUrlOperator();
QString src = "c:/user/AFile.txt";
QString target = "c:/user/AFileCopied.txt";
op->copy(src, target, false, false);

QUrlInfo fi = op->info(target);
write(fi.name());


The file is well copied but my url info object does not give me any info, why ?

wysota
24th February 2006, 10:31
Probably because QUrlOperator::copy is an asynchronous operation, so in the time of creating that QUrlInfo object the destination might not exist yet.

yellowmat
24th February 2006, 10:51
Ok ,so I must connect the finished signals to one of my slot to be sure that the operation is over.


void testDialog::init()
{
targetPath = QDir::currentDirPath();
targetFile = targetPath+"/AFile.txt";
op = new QUrlOperator(targetPath);

connect(op, SIGNAL(finished(QNetworkOperation*)), SLOT(displayInfo(QNetworkOperation* _nop)));

nop = op->listChildren();
}


void testDialog::write( const QString &line )
{
lb->insertItem(line);
lb->setBottomItem(lb->count()-1);
}




void testDialog::displayInfo( QNetworkOperation * _nop )
{
QUrlInfo fi = op->info(targetFile);

write(fi.name());
}


but my slot is never called, what's wrong again ?

wysota
24th February 2006, 11:30
I don't see the code for starting the operation itself. Did you have a look at the example from Qt docs?