Results 1 to 12 of 12

Thread: C++/Qt5 - Adding quotes " into a QString

  1. #1
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default C++/Qt5 - Adding quotes " into a QString

    Qt5.5.1
    QtCreator 3.5.1
    Windows 7

    Hello,

    I'm trying to build a QString for a QProcess command.
    I'm getting stray '\' (back slashes) in my output where I try to insert a ".
    It appears that I need the " in 'cmd' as there are spaces in the path and file names.
    I can't work out how to remove the '\' from the 'cmd' QString (I've tried various variations)
    or not to get them there in the first place.
    I think its something to do with 'escaping' but not sure what or how.
    Help would be appriciated.

    Regards
    Qt Code:
    1. QString cmd = ("7z x -y ");
    2. cmd.append(fileinfo.fileName());
    3. cmd.append(" *.* ");
    4. cmd.append("-o");
    5.  
    6. cmd.append('"'); //QString output = "Hello \"world\"!"; //string literal - works
    7.  
    8. cmd.append(destination);
    9. cmd.append("/Files to copy");
    10.  
    11. cmd.append('"');
    12.  
    13. cmd.append(" -r");
    14. qDebug() << cmd;
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. qDebug output:-
    2. "7z x -y myFile.zip *.* -o\"H:/my dir/systems/output files\" -r"
    3. ^ ^
    4.  
    5. Terminal commmand, works ok.
    6. 7z x -y myFile.zip *.* -o"H:/my dir/systems/output files" -r
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: C++/Qt5 - Adding quotes " into a QString

    You are trying to create a command string that contains the command and its arguments.

    But QProcess takes the command without arguments and lets you pass the arguments using a QStringList and takes care of escaping for you.

    Cheers,
    _

  3. #3
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: C++/Qt5 - Adding quotes " into a QString

    Hello anda_skoda,

    I use a similar format elsewhere and it works, difference being no quotes in the command.
    Qt Code:
    1. cmd1 = "7z";
    2. cmd1.append(" e -bsp2 ").append(fileinfo.fileName());
    3.  
    4. qDebug() << "cmd1 - " << cmd1;
    5. cmd1 - "7z e -bsp2 myFile.zip"
    To copy to clipboard, switch view to plain text mode 
    However, I'll have a play with QStringList.

    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: C++/Qt5 - Adding quotes " into a QString

    While manual escaping may work, it is just unnecessary complexity.

    Cheers,
    _

  5. #5
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: C++/Qt5 - Adding quotes " into a QString

    Hello anda_skoda,

    I think maybe there is a little confusion from my previous posts.
    Qt Code:
    1. QStringList arguments;
    2. QString cmd = ("7z");
    3. arguments << " x " << "-y" << " *.* " << "-o" << (destination + "/output files") << " -r";
    4. qDebug() << "Cmd - " << cmd;
    5. qDebug() << "Arg - " << (arguments.join(""));
    6.  
    7. Cmd - "7z"
    8. Arg - " x -y *.* -oH:/my dir/systems/output files -r"
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. process->start(cmd, arguments);
    2.  
    3. Arg has to look like:-
    4. " x -y *.* -o"H:/my dir/systems/output files" -r"
    5. ^ ^
    To copy to clipboard, switch view to plain text mode 
    Note - the extra 'quotes', these are needed by the program I'm calling.

    This is the terminal commmand that works ok.
    Qt Code:
    1. 7z x -y myFile.zip *.* -o"H:/my dir/systems/output files" -r
    2. ^ ^
    To copy to clipboard, switch view to plain text mode 
    What I can't work out is how to add the quotes to the arguments list.

    Regards
    Last edited by jimbo; 7th January 2016 at 11:20.

  6. #6
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: C++/Qt5 - Adding quotes " into a QString

    Quote Originally Posted by jimbo View Post
    Arg has to look like:-
    " x -y *.* -o"H:/my dir/systems/output files" -r"
    ^ ^[/code]Note - the extra 'quotes', these are needed by the program I'm calling.

    This is the terminal commmand that works ok.
    Qt Code:
    1. 7z x -y myFile.zip *.* -o"H:/my dir/systems/output files" -r
    2. ^ ^
    To copy to clipboard, switch view to plain text mode 
    If this is a command you type in a terminal, then it is interpreted by the shell, which means that those quotes are probably only there to yield a single argument -oH:/my dir/systems/output files.

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: C++/Qt5 - Adding quotes " into a QString

    Why did you put spaces into the arguments?

    " x " is going to end up as " x ", are you sure 7z is still capable of understanding that as x?

    Cheers,
    _

  8. #8
    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: C++/Qt5 - Adding quotes " into a QString

    Similar problem with " *.* " and " -r".

  9. #9
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: C++/Qt5 - Adding quotes " into a QString

    Helllo,

    Sorry for the delay in replying.
    Thanks to all who have responded.

    I think the problem with the quotes being needed is that Windows allows paths and filenames with spaces,
    and from what I've read it seems that if there are spaces the string needs to be in enclosed quotes.
    I think the program I'm calling requires them for this reason or maybe needs them for its own syntax handling.
    I cleaned the spaces in the args, no help.

    Regards

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: C++/Qt5 - Adding quotes " into a QString

    Maybe "*.*" is not interpreted by the program but by the shell?

    Have you tried calling a simple test program instead of 7z and see how it receives the arguments?
    I.e. to determine if the path is passed as several arguments?
    Because spaces in file names is expected (on all platforms), the argument quoting should take care of that.

    Cheers,
    _

  11. #11
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: C++/Qt5 - Adding quotes " into a QString [Solved)

    Hello anda_skoa,

    I've found a solution (sort of) by using relative paths (now quotes not needed).
    The only thing is, I can't have any spaces in the QString 'NewDir'.
    The spaces in the args are needed.
    Thanks for your help.

    Regards

    Qt Code:
    1. process->start(QString("%1%2%3%4%5%6").arg("7z x -y ",
    2. fileinfo.fileName(),
    3. " *.*",
    4. " -o",
    5. newDir,
    6. " -r"));
    To copy to clipboard, switch view to plain text mode 

  12. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: C++/Qt5 - Adding quotes " into a QString [Solved)

    Since you made no indication if you have actually tested the quoting, I assume you did not.

    The arguments are passed, with spaces and all, as-is to the called program as far as I can tell (Qt 5.5.1, Linux)
    Qt Code:
    1. #include <QtCore>
    2.  
    3. #include <iostream>
    4.  
    5. using namespace std;
    6.  
    7. int main(int argc, char **argv)
    8. {
    9. QCoreApplication app(argc, argv);
    10.  
    11. QStringList args = app.arguments();
    12.  
    13. // if called with args, just write them to stdout, one per line
    14. if (args.count() > 1) {
    15. args.takeFirst(); // remove the program itself
    16. foreach (const QString &arg, args) {
    17. std::cout << qPrintable(arg) << std::endl;
    18. }
    19. return 0;
    20. }
    21.  
    22. args.clear();
    23.  
    24. // otherwise start ourselves with the test arguments
    25. args << "1" << " 2 " << "3 4 5";
    26.  
    27. QProcess process;
    28. process.start(app.applicationFilePath(), args),
    29. process.waitForFinished(10000);
    30.  
    31. QByteArray data = process.readAllStandardOutput();
    32. QTextStream stream(&data, QIODevice::ReadOnly);
    33. int lineNumber = 0;
    34. do {
    35. const QString line = stream.readLine();
    36. ++lineNumber;
    37. std::cout << lineNumber << ": '" << qPrintable(line) << "'" << std::endl;
    38. } while (!stream.atEnd());
    39.  
    40. return 0;
    41. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. Replies: 7
    Last Post: 21st March 2014, 10:24
  2. Replies: 3
    Last Post: 27th December 2013, 18:04
  3. Replies: 2
    Last Post: 13th November 2013, 08:03
  4. Replies: 3
    Last Post: 17th April 2012, 16:05
  5. Replies: 0
    Last Post: 20th September 2010, 09:58

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.