Qt Application with CLI Commands



  • Hi there,

    I am interested in making an application which somehow provides similar functionalities with Windows command prompt. To simplify things, I want to be able to create a new folder or rename an existing folder in my hard disk using this application. Is this possible to be done? Can I use the existing commands of the Windows command prompt but through my application?

    Thank you in advance.


  • Moderators

    You may use "QProcess":http://developer.qt.nokia.com/doc/qt-4.8/qprocess.html in general.
    Concerning of file functionalities they are available through "QDir::mkdir":http://developer.qt.nokia.com/doc/qt-4.8/qdir.html#mkdir -or for files through QFile-.

    [Edit] QDir provides probably all functionality you need.



  • Thanks for you reply.

    Indeed those classes look promising to what I am looking for.

    Any ideas about adding a file in a .zip, .rar file from a Qt application?

    Couldn't get much information online concerning that.

    Cheers.


  • Moderators

    If it's a file that already exists on disk, you could use a QProcess to make a call to a standalone .zip or .rar application.

    Edit: (As koahnig already mentioned above.)



  • Will it be possible to open that .zip file later on with a zip utility software like winrar, 7z or winzip?

    Because I have done something similar but the file cannot be opened...


  • Moderators

    If you're using QProcess to access an external zip utility from inside of your program, then it should produce the same results as if you were calling it from the command line; the external application has no idea that it's being called from inside of your program.



  • Aha...is there an example you can give for using the QProcess for zipping a .txt file using for example 7z? Something that I could apply to my own solution maybe?

    Seems there is not a huge amount of information regarding Qt, that's quite sad because I am just starting...


  • Moderators

    There's a ton of information regarding Qt. It's one of the most well documented toolkits around. I'm not sure where you've been looking. Have you tried the "QProcess":http://developer.qt.nokia.com/doc/qt-4.8/qprocess.html docs?

    Here's a brief example lifted directly from those docs. I don't know the command line parameters for 7z, but it should work the same as this example below. Just substitute the 7z executable for "analogclock" and use the proper command line parameters that are expected. Not a difficult exercise.

    @
    QObject *parent;
    ...
    QString program = "./path/to/Qt/examples/widgets/analogclock";
    QStringList arguments;
    arguments << "-style" << "motif";

     QProcess *myProcess = new QProcess(parent);
     myProcess->start(program, arguments);
    

    @



  • Quite simple indeed. Can I hide the command prompt or software interface while this is taking place? I do not want the user to see that a 3rd party application is behind this...


  • Moderators

    I don't believe that a command prompt or anything should appear. It's all handled in the background. It doesn't actually open a command prompt window and type in the commands.



  • Those arguments seem a little tricky...How's the order they should be written? For example the command line command for 7z is "7za a -t7z files.7z text1.txt" to create files.7z with text1.txt inside.

    @QApplication a(argc, argv);

    QObject *parent;
    QString program = "C:\Software\7za920\7za.exe";
    QStringList arguments;
    arguments << "7za a -t7z files.7z text1.txt";
    QProcess *myProcess = new QProcess(parent);
    myProcess->start(program, arguments);
    
    zipfile w;
    w.show();
    
    return a.exec&#40;&#41;;@

  • Moderators

    @
    QString program = "C:\Software\7za920\7za.exe"; // This is the app
    QStringList arguments;
    arguments << "a" << "-t7z" << "files.7z" << "text1.txt"; // Don't need "7za" here, it's already covered up there.
    @



  • Hmm...done but it doesn't work...but where do we specify where the .txt file is located? How does the program know where to get it from?


  • Moderators

    Well, I would assume that you would know where the file is that you're wanting to work with. Shouldn't you probably just use the full path to it? "/path/to/text1.txt"



  • Still...

    @QObject *parent;
    QString program = "/Software/7za920/7za.exe";
    QStringList arguments;
    arguments << "a" << "-t7z" << "/Users/croussou_dm4/Desktop/files.7z" << "/Users/croussou_dm4/Desktop/text1.txt";
    QProcess *myProcess = new QProcess(parent);
    myProcess->start(program, arguments);@

    By the way, thanks for your support...


  • Moderators

    I'd suggest reading through the docs for QProcess. There are various ways to find the return code of the finished process (was it ok?) or to read from stderr (were there any error messages), and that sort of thing.

    In the long run, it will be much more beneficial than me or anyone else trying to guess what the problems may be.

    By the way, you're welcome!



  • Again thank you very much.

    I will look into that.


  • Moderators

    Be sure and let us know what you find out!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.