Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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 \" after cmd /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.





  • 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



  • @Darkloud said in Putting double quotes around a QString variable?:

    I still get an error that the file cannot be found when the folder it is in contains a space..

    Same thing, you need to enclosed it with \"



  • 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.exe

    Shouldn'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 ...



  • Could you enclose that with the Code modifier in the post editor? I can't see where you have the escape sequences



  • 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.



  • I don't understand cmd /c \""
    The string is wrong, there is a quote in the middle :
    cmd /c "C:/Qt/5.14.2/mingw73_64/bin/windeployqt.exe "C:/Users/wjbro/Desktop/New folder/Release001.exe"



  • @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

    Says unknown escape symbol, and it underlines the \ after New



  • Yes I tried \+space, but QCreator doesn't seem to like it :)
    So I don't know if it 's a valid escape in C.

    But if I look with a terminal; spaces are escaped like that.



  • @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);   
    


  • For me the right concatenation sentence is:
    process->startDetached("cmd /c " + deploystring);



  • None of these work... :( Heading to bed. Will try again tomorrow



  • @Darkloud Do you actually try @mpergand 's advice? I also think the problem is that extra \" after cmd /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 the QStringList() 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. Unless windeployqt.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 versus dir \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 \" after cmd /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.
    error

    Here 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 and qDebug() << 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.



  • @JonB said in Putting double quotes around a QString variable?:

    @Darkloud said in Putting double quotes around a QString variable?:

    process->startDetached("cmd", QStringList() << "/c" << binFolderName << exeName);

    So qDebug() << binFolderName and qDebug() << 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.

    See above! I added a space after the first argument, which messed everything up. It is working now!



  • @Darkloud
    Yeah, I think you discovered/typed that while I was answering. Gotta love them spaces... :)


Log in to reply