PDA

View Full Version : Unable to start linux process by Qt



karankumar1609
18th December 2013, 10:24
Hello everybody,

I have got stucked somewhere in my code.
I want to copy all the content from one directory to another, means copy data from source to destination.
I have written the below code, :( But its not working
And i dont know why :(



QScopedPointer<QProcess> my_process(new QProcess());

QString source = parent_path;
source.append(QDir::separator());
source.append("*");

QString destination = map_path;
destination.append(QDir::separator());

qDebug() << "Source : " << source;
qDebug() << "Destination : " << destination;

QStringList arguments;
arguments << "-rf"
<< source
<< destination;
my_process->start("cp", arguments);

qDebug() << "Copied : " << my_process->waitForFinished();
qDebug() << my_process->errorString();


Please tell me what is the problem in my code.,??????

anda_skoa
18th December 2013, 16:59
Your problem is most likely that your wildcards are not expanded. This is usually done by the shell, but you are not invoking the cp command through a shell but directly.

As far as I can see you have a couple of options:
1) create a list of file using QDir and then pass them to the cp command as input followed by the target dir
2) create a helper script that will internall pass its parameters to cp, expanding the in the process
3) call a shell and use its commandline arguments to run cp as a child command
4) copy in Qt by listing all files and calling QFile::copy() on each, probably doing all that in a thread in order not to block the main thread
5) create a small helper program in Qt that uses (4) in its main thread and is called by the main program through QProcess

Cheers,
_

Lesiok
18th December 2013, 17:25
"parent_path" is absolute or relative ? If relative what is current dir in application ?

karankumar1609
19th December 2013, 10:52
I have chnaged my code below



QScopedPointer<QProcess> my_process(new QProcess());

QString source = parent_path;
source.append(QDir::separator());
// source.append("*");

QString destination = map_path;
destination.append(QDir::separator());

qDebug() << "Source : " << source;
qDebug() << "Destination : " << destination;

QStringList arguments;
arguments << "-rf"
<< source
<< destination;
my_process->start("cp", arguments);

qDebug() << "Copied : " << my_process->waitForFinished();
qDebug() << my_process->errorString();


Now it makes the data Directory in the destination directory.

Source = "/home/DATA/"
Destination = "/home/DATA_MAP/"

now it creates the DATA folder in my DATA_MAP folder.
I just want my data content in my DATA_MAP directory

Added after 12 minutes:

"parent_path" is the absolute path.

//////////////////////////////////
Following code wors for me ;)



{
QDir dir(parent_path);

QStringList dirs = dir.entryList(QDir::Dirs | QDir::NoSymLinks | QDir::NoDotAndDotDot);

foreach(QString directory, dirs) {
QString source = parent_path;
source.append(QDir::separator());
source.append(directory);
qDebug() << "Source : " << source;

QString destination = map_path;
destination.append(QDir::separator());

QScopedPointer<QProcess> my_process(new QProcess());

connect(this, SIGNAL(stopProcessing()), my_process.data(), SLOT(kill()), Qt::DirectConnection);
QStringList arguments;
arguments << "-rf"
<< source
<< destination;

my_process->start("cp", arguments);

qDebug() << "Copied : " << my_process->waitForFinished(-1);
}