Results 1 to 2 of 2

Thread: special characters as argument of QProcess

  1. #1
    Join Date
    Jan 2012
    Posts
    29
    Thanks
    7

    Default special characters as argument of QProcess

    Hi, in my app it seems I cannot properly pass special characters as argument to the external program started with a QProcess instance.

    The final command I have to execute is something like:

    /usr/bin/rsync -n -v -a --exclude "/*/*/" --include "*/" --include "*.txt" --exclude "*" /dir1/ /dir2

    --exclude and --include are rsync's filter rules which must be written using special characters as you can see.

    A snip of the code to generate them:

    Qt Code:
    1. args << "--exclude";
    2. args << "\"/\*/\*/\"";
    3. args << "--include";
    4. args << "\"\*/\"";
    5. args << "--include";
    6. args << "\"\*.txt\"";
    7. args << "--exclude";
    8. args << "\"\*\"";
    To copy to clipboard, switch view to plain text mode 

    You see I use \ as escape character for "" and *. However it seems it is not enough... when I then run the application, I can intercept the rsync command on the system (ps aux | grep rsync) and I can see it running exactly as expected:
    Qt Code:
    1. /usr/bin/rsync -n -v -a --exclude "/*/*/" --include "*/" --include "*.txt" --exclude "*" /dir1/ /dir2
    To copy to clipboard, switch view to plain text mode 
    Also, if I debug with qdebug the command executed I see:
    Qt Code:
    1. "/usr/bin/rsync" ("-n", "-v", "-a", "--exclude", ""/*/*/"", "--include", ""*/"", "--include", ""*.txt"", "--exclude", ""*"", "/dir1/", "/dir2")
    To copy to clipboard, switch view to plain text mode 
    everything seems to be ok.However, the output generated in my app does not reflect it, just like the filter rules are not applied. I suspect there is something with special characters and escaping... but really do not know how to go ahead.. I am struggling...

    Thanks,
    Jan

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: special characters as argument of QProcess

    You don't need to escape the asterisk for C++'s sake. Shouldn't do any harm except to readability. I found on my Linux machine the escaped quotes are not required.

    Qt Code:
    1. StringList args;
    2. args << "-a"
    3. << "--exclude" << "/*/*/"
    4. << "--include" << "*/"
    5. << "--include" << "*.txt"
    6. << "--exclude" << "*"
    7. << "/tmp/dir1/"
    8. << "/tmp/dir2";
    9.  
    10. QProcess proc;
    11. proc.start("/usr/bin/rsync", args);
    To copy to clipboard, switch view to plain text mode 
    Does the trick here, copying any txt file in the first or second level of the dir1 directory structure, plus folders for any immediate child folder of dir1.

Similar Threads

  1. Remap special characters?
    By PaladinKnight in forum Qt Programming
    Replies: 0
    Last Post: 28th September 2010, 06:23
  2. special characters in xml
    By khcbabu in forum Qt Programming
    Replies: 3
    Last Post: 6th November 2008, 22:10
  3. QtXml and special characters
    By supergillis in forum Qt Programming
    Replies: 4
    Last Post: 29th September 2008, 17:44
  4. QT 4 Xml parsing with special characters
    By KrishnaKishan in forum Qt Programming
    Replies: 1
    Last Post: 18th June 2007, 10:53
  5. Using special characters in QT dialogs
    By hvengel in forum Qt Tools
    Replies: 2
    Last Post: 10th April 2007, 00:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.