Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Putting double quotes around a QString variable?
QtWS25 Last Chance

Putting double quotes around a QString variable?

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 5 Posters 4.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Darkloud
    wrote on 27 May 2020, 23:13 last edited by aha_1980
    #1

    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]

    1 Reply Last reply
    0
    • D Darkloud
      28 May 2020, 01:58

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

      B Offline
      B Offline
      Bonnie
      wrote on 28 May 2020, 03:44 last edited by Bonnie
      #16

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

      J D 2 Replies Last reply 28 May 2020, 06:59
      5
      • M Offline
        M Offline
        mpergand
        wrote on 27 May 2020, 23:20 last edited by
        #2

        Look at Escape sequences

        1 Reply Last reply
        1
        • D Offline
          D Offline
          Darkloud
          wrote on 27 May 2020, 23:53 last edited by
          #3

          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

          M 1 Reply Last reply 28 May 2020, 00:08
          0
          • D Darkloud
            27 May 2020, 23:53

            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

            M Offline
            M Offline
            mpergand
            wrote on 28 May 2020, 00:08 last edited by mpergand
            #4

            @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 \"

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Darkloud
              wrote on 28 May 2020, 00:20 last edited by Darkloud
              #5
              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\""
              
              1 Reply Last reply
              0
              • M Offline
                M Offline
                mpergand
                wrote on 28 May 2020, 00:41 last edited by mpergand
                #6
                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 ...

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  Darkloud
                  wrote on 28 May 2020, 00:44 last edited by Darkloud
                  #7

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

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    Darkloud
                    wrote on 28 May 2020, 00:54 last edited by
                    #8

                    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.

                    ElusE 1 Reply Last reply 28 May 2020, 01:16
                    0
                    • M Offline
                      M Offline
                      mpergand
                      wrote on 28 May 2020, 01:07 last edited by mpergand
                      #9

                      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"

                      1 Reply Last reply
                      0
                      • D Darkloud
                        28 May 2020, 00:54

                        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.

                        ElusE Offline
                        ElusE Offline
                        Elus
                        wrote on 28 May 2020, 01:16 last edited by
                        #10

                        @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\""; 
                        
                        D 1 Reply Last reply 28 May 2020, 01:22
                        0
                        • ElusE Elus
                          28 May 2020, 01:16

                          @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\""; 
                          
                          D Offline
                          D Offline
                          Darkloud
                          wrote on 28 May 2020, 01:22 last edited by
                          #11

                          @Elus

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

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            mpergand
                            wrote on 28 May 2020, 01:22 last edited by mpergand
                            #12

                            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.

                            1 Reply Last reply
                            0
                            • ElusE Offline
                              ElusE Offline
                              Elus
                              wrote on 28 May 2020, 01:36 last edited by
                              #13

                              @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);   
                              
                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                mpergand
                                wrote on 28 May 2020, 01:42 last edited by
                                #14

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

                                1 Reply Last reply
                                0
                                • D Offline
                                  D Offline
                                  Darkloud
                                  wrote on 28 May 2020, 01:58 last edited by
                                  #15

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

                                  B 1 Reply Last reply 28 May 2020, 03:44
                                  0
                                  • D Darkloud
                                    28 May 2020, 01:58

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

                                    B Offline
                                    B Offline
                                    Bonnie
                                    wrote on 28 May 2020, 03:44 last edited by Bonnie
                                    #16

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

                                    J D 2 Replies Last reply 28 May 2020, 06:59
                                    5
                                    • B Bonnie
                                      28 May 2020, 03:44

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

                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 28 May 2020, 06:59 last edited by JonB
                                      #17

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

                                      1 Reply Last reply
                                      1
                                      • B Bonnie
                                        28 May 2020, 03:44

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

                                        D Offline
                                        D Offline
                                        Darkloud
                                        wrote on 28 May 2020, 13:06 last edited by
                                        #18

                                        @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!
                                        1 Reply Last reply
                                        0
                                        • D Offline
                                          D Offline
                                          Darkloud
                                          wrote on 28 May 2020, 14:42 last edited by Darkloud
                                          #19

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

                                          J 1 Reply Last reply 28 May 2020, 14:51
                                          0
                                          • D Darkloud
                                            28 May 2020, 14:42

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

                                            J Offline
                                            J Offline
                                            JonB
                                            wrote on 28 May 2020, 14:51 last edited by
                                            #20

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

                                            D 1 Reply Last reply 28 May 2020, 15:51
                                            0

                                            1/22

                                            27 May 2020, 23:13

                                            • Login

                                            • Login or register to search.
                                            1 out of 22
                                            • First post
                                              1/22
                                              Last post
                                            0
                                            • Categories
                                            • Recent
                                            • Tags
                                            • Popular
                                            • Users
                                            • Groups
                                            • Search
                                            • Get Qt Extensions
                                            • Unsolved