Putting double quotes around a QString variable?
-
Hello folks! Having fun with Qt so far, but running into an issue in my code when a folder with spaces is selected. How would I go about adding double quotes to the code below?
QProcess *process = new QProcess(this); deploystring = binFolderName + "/windeployqt.exe " + exeName; // Append windeployqt to binFolderName, append exeName ui->statusbar->showMessage("Running: " + deploystring); // Set status bar message to reflect running command process->startDetached("cmd /c \"" + deploystring); // Starts process
Specifically, I need double quotes around the variables binFolderName, and exeName.
Any help is appreciated!
[edit aha_1980: Fixed code tags]
-
@Darkloud Do you actually try @mpergand 's advice? I also think the problem is that extra
\"
aftercmd /c
.
And since you've struggled a lot with quotes, why don't you just use program + arguments as params?process->startDetached("cmd", QStringList() << "/c" << "C:/Qt/5.14.2/mingw73_64/bin/windeployqt.exe" << "C:/Users/wjbro/Desktop/New folder/Release001.exe");
Then you won't need to add extra quotes to paths.
-
Look at Escape sequences
-
I tried " like this:
deploystring = "\"" + binFolderName + "/windeployqt.exe\" " + "\"" + exeName + "\"";
And that seems to produce exactly what I wanted, however when I run my process, (using cmd) I still get an error that the file cannot be found when the folder it is in contains a space..
"C:\Users\wjbro\Desktop\New" does not exist.
Should be C:\Users\wjbro\Desktop\New folder\Release001.exe
-
deploystring = "\"" + binFolderName + "/windeployqt.exe\" " + "\"" + exeName + "\"";
I thought I did?
if
binFolderName = C:\Qt\5.14.2\mingw73_64\bin
exeName = C:\Users\wjbro\Desktop\New folder\Release001.exeShouldn't my output be:
deploystring = "C:\Qt\5.14.2\mingw73_64\bin\windeployqt.exe" "C:\Users\wjbro\Desktop\New folder\Release001.exe"This is the output of qDebug:
Folder with space: (does not work)
"\"C:/Qt/5.14.2/mingw73_64/bin/windeployqt.exe\" \"C:/Users/wjbro/Desktop/New folder/Release001.exe\""
Folder with no space: (works)
"\"C:/Qt/5.14.2/mingw73_64/bin/windeployqt.exe\" \"C:/Users/wjbro/Desktop/test/Release001.exe\""
-
QString str="C:/Qt/5.14.2/mingw73_64/bin/windeployqt.exe \"C:/Users/wjbro/Desktop/New folder/Release001.exe\""; qDebug().noquote() <<str;
C:/Qt/5.14.2/mingw73_64/bin/windeployqt.exe "C:/Users/wjbro/Desktop/New folder/Release001.exe"
Should work, i'm not a cmd line guru though ...
-
Tried this:
deploystring = "C:/Qt/5.14.2/mingw73_64/bin/windeployqt.exe \"C:/Users/wjbro/Desktop/New folder/Release001.exe\""; process->startDetached("cmd /c \"" + deploystring); }
And I still get the same error... :(
But yet again, if i do it in my 'test' folder, (with no spaces) it works fine with your above example.
I have also tested within cmd itself, and enclosing everything in double quotes works fine... I have no idea what is going wrong.
-
@Darkloud said in Putting double quotes around a QString variable?:
Tried this:
deploystring = "C:/Qt/5.14.2/mingw73_64/bin/windeployqt.exe \"C:/Users/wjbro/Desktop/New folder/Release001.exe\""; process->startDetached("cmd /c \"" + deploystring); }
And I still get the same error... :(
But yet again, if i do it in my 'test' folder, (with no spaces) it works fine with your above example.
I have also tested within cmd itself, and enclosing everything in double quotes works fine... I have no idea what is going wrong.
Can you try:
deploystring = "C:/Qt/5.14.2/mingw73_64/bin/windeployqt.exe \"C:/Users/wjbro/Desktop/New\ folder/Release001.exe\"";
-
@Elus said in Putting double quotes around a QString variable?:
deploystring = "C:/Qt/5.14.2/mingw73_64/bin/windeployqt.exe "C:/Users/wjbro/Desktop/New folder/Release001.exe"";
process->startDetached("cmd /c "" + deploystring);I think carets are sometimes used in Windows to escape spaces, no?
deploystring = "C:/Qt/5.14.2/mingw73_64/bin/windeployqt.exe \"C:/Users/wjbro/Desktop/New^ folder/Release001.exe\""; process->startDetached("cmd /c \"" + deploystring);
-
@Darkloud Do you actually try @mpergand 's advice? I also think the problem is that extra
\"
aftercmd /c
.
And since you've struggled a lot with quotes, why don't you just use program + arguments as params?process->startDetached("cmd", QStringList() << "/c" << "C:/Qt/5.14.2/mingw73_64/bin/windeployqt.exe" << "C:/Users/wjbro/Desktop/New folder/Release001.exe");
Then you won't need to add extra quotes to paths.
-
@Bonnie
I think @Darkloud should indeed run the process via theQStringList()
overload you suggest.However, personally I would call
QString QDir::toNativeSeparators(const QString &pathName)
(https://doc.qt.io/qt-5/qdir.html#toNativeSeparators) on each of those file path arguments. Unlesswindeployqt.exe
states that it wants file paths to be passed with forward slashes, relying on Windows programs to treat/
as the same as\
is dubious, IMHO. Although not this example, I have said before look at the difference between, say,dir /w
versusdir \w
, which do not do the same thing! -
@Bonnie said in Putting double quotes around a QString variable?:
@Darkloud Do you actually try @mpergand 's advice? I also think the problem is that extra
\"
aftercmd /c
.
And since you've struggled a lot with quotes, why don't you just use program + arguments as params?process->startDetached("cmd", QStringList() << "/c" << "C:/Qt/5.14.2/mingw73_64/bin/windeployqt.exe" << "C:/Users/wjbro/Desktop/New folder/Release001.exe");
Then you won't need to add extra quotes to paths.
Yes I did... However it was still not working for me, as stated... However your solution worked! During my research last night, I found using QStringList() was a cleaner and safer way of passing arguments to the command line, however I suppose I was implementing it inorrectly. Not sure what I did incorrectly, but after taking the line you suggested and substituting the hard coded directories with my variables "exeName and binFolderName like so:
```
process->startDetached("cmd", QStringList() << "/c" << binFolderName << exeName);
Everything is working as it should! Thank you all for the help!
-
@Darkloud said in Putting double quotes around a QString variable?:
process->startDetached("cmd", QStringList() << "/c" << binFolderName << exeName);
Actually, it isn't working... I thought it was, but I am still getting the same error :/
If I run:
```
process->startDetached("cmd", QStringList() << "/c" << "C:/Qt/5.14.2/mingw73_64/bin/windeployqt.exe" << "C:/Users/wjbro/Desktop/New folder/Release001.exe");
it works correctly. If I run: ``` process->startDetached("cmd", QStringList() << "/c" << binFolderName << exeName);
it gives me the same
" C:\Users\wjbro\Desktop\New" does not exist.
errorHere is my whole .cpp file if it helps:
#include "deployqt.h" #include "ui_deployqt.h" #include <QDebug> DeployQt::DeployQt(QWidget *parent) : QMainWindow(parent), ui(new Ui::DeployQt) { ui->setupUi(this); } DeployQt::~DeployQt() { delete ui; } void DeployQt::on_CancelButton_clicked() { this->close(); } void DeployQt::on_QtBinDirBrowse_clicked() { binFolderName = QFileDialog::getExistingDirectory(this, // Open folder select dialog, default to 5.14.2\mingw73_64\bin, store in binFolderName tr("Open Bin Directory"), "C:\\Qt\\5.14.2\\mingw73_64\\bin",QFileDialog::DontResolveSymlinks); if(!binFolderName.isEmpty()){ // If binFolderName is not empty, set it to QtBindirOut LineOutput ui->QtBinDirOut->setText(binFolderName); binFolderName = binFolderName + "/windeployqt.exe "; // append /windeployqt.exe to binFolderName } } void DeployQt::on_QtInputExeBrowse_clicked() { exeName = QFileDialog::getOpenFileName(this, // Open exe to be deployed select dialog, sets to exeName tr("Select Exe to Deploy"), "C:\\"); if(!exeName.isEmpty()){ // If exeName is not empty, set it to QtInputExeOut LineOutput ui->QtInputExeOut->setText(exeName); } } void DeployQt::on_DeployButton_clicked() { QProcess *process = new QProcess(this); ui->statusbar->showMessage("Running: " + binFolderName + exeName); // Set status bar message to reflect running command process->startDetached("cmd", QStringList() << "/c" << binFolderName << exeName); if ( !process->waitForFinished(-1)) // Wait for process to finish { return; // Return from if statement, to continue } if(process->exitCode() != 0){ QString errorMessage; if(binFolderName == ""){ errorMessage.append("No Bin Folder Selected!\n"); } if(exeName == ""){ errorMessage.append("No Exe File Selected! "); } msgBox.setText(errorMessage); msgBox.setWindowTitle("Error - Deploy Qt Exe (Windows)"); msgBox.exec(); } else{ ui->statusbar->showMessage("Complete!"); } }
Really not sure what happened.. Could have sworn I tested it and it worked fine.
EDIT: facepalm In order to get the QString to display correctly in the status bar with a space between binFolderName and exeFile, i put a space after the /windeploy append.. That was my issue! Working fine!
-
@Darkloud said in Putting double quotes around a QString variable?:
process->startDetached("cmd", QStringList() << "/c" << binFolderName << exeName);
So
qDebug() << binFolderName
andqDebug() << exeName
, and copy & paste so that we can clearly read them. If they were identical to your original"C:/Qt/5.14.2/mingw73_64/bin/windeployqt.exe"
&"C:/Users/wjbro/Desktop/New folder/Release001.exe"
, then it would behave the same, there's nothing magical about literal strings vs variables.