PDA

View Full Version : QProcess doesn't do the same as the terminal would



mr_kazoodle
3rd February 2011, 10:35
Hi,

I took the command to create a simple archive using the terminal and put every argument in a QStringlist, but the result is that the program creates an archive called mx9.7z in the folder where the program is located.
Why doesn't this example program do the same thing as the same command entered in the terminal?
(7z a -t7z mx9 /home/user/Desktop/Desktop.7z /home/user/Desktop/file1.pdf /home/user/Desktop/file2.doc)


#include <QtGui/QApplication>
#include <QPushButton>
#include "program.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPushButton *okButton = new QPushButton("OK");
Program *mystarter = new Program();
a.connect(okButton, SIGNAL(clicked()), mystarter, SLOT(startprocess()));

okButton->setMinimumWidth(150);
okButton->show();
return a.exec();
}

#ifndef PROGRAM_H
#define PROGRAM_H
#include <QObject>
#include <QProcess>

class Program : public QObject
{
Q_OBJECT
public:
QProcess myprocess;
explicit Program(QObject *parent = 0);

public slots:
void startprocess();
};

#endif

#include "program.h"
#include <QString>
#include <QStringList>

Program::Program(QObject *parent) : QObject(parent)
{}

void Program::startprocess()
{
QStringList list;
list << "a" << "-t7z" << "mx9" << "/home/user/Desktop/Desktop.7z" << "/home/user/Desktop/file1.pdf" << "/home/user/Desktop/file2.doc";
myprocess.start("7z", list);
}

mcosta
3rd February 2011, 14:33
Hi,

the problenm is the 7z command not QProcess

try with


list << "a" << "-t7z" << "-mx9" << "/home/user/Desktop/Desktop.7z" << "/home/user/Desktop/file1.pdf" << "/home/user/Desktop/file2.doc";


note "-mx9" instead of "mx9"

mr_kazoodle
3rd February 2011, 15:48
I'm sorry, that was a mistake in my example, but however the example is now working, hm..... I must have made some other mistake somewhere in my real program...
Thanks anyway :)

Added after 25 minutes:

The program is now working as it should have been.
I have made the QProcess object a global variable (just like in the example) instead of creating it in the function which starts it.
And I found that I didn't need to use apostrophe signs around the filenames. I thought I needed to do that because if you don't do that in the terminal you get errors if there are spaces in the filenames or names of the directories. (I think that was also a reason why it didn't work)
Wow, I can't believed I finally fixed it. I've been searching for this for days.