Results 1 to 6 of 6

Thread: To call a string containing file path in a command

  1. #1
    Join Date
    Jun 2015
    Posts
    6
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default To call a string containing file path in a command

    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. QString filename = QFileDialog::getOpenFileName(this, tr("Open"),
    4. "S://MATLAB/",
    5. "MATLAB Code files (*.m);;All Files (*.*)");
    6. QProcess* p = new QProcess(this);
    7. p->start("C:/\"Program Files\"/MATLAB/R2013a/bin/matlab.exe -nosplash -nojvm -r run('filename');");
    8. }
    To copy to clipboard, switch view to plain text mode 

    But I'm unable to use the string filename to run MATLAB script file. Instead of sending the file path like "S:/MATLAB/test.m" , the variable name "filename" is being executed in the run command. I have to manually enter the file path. Is there any other way to redirect the file path to run it directly on a mouse-click.?
    Last edited by Nick_Neo; 9th June 2015 at 06:59.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: To call a string containing file path in a command

    You need to use the value of variable 'filename' and not the string constant "filename".
    Qt Code:
    1. p->start( QString("C:/\"Program Files\"/MATLAB/R2013a/bin/matlab.exe -nosplash -nojvm -r run('%1');").arg(filename) );
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void function(const QString& s);
    2. ...
    3. QString string = ...;
    4. ...
    5. function(string);
    6. ...
    7. function("string");
    To copy to clipboard, switch view to plain text mode 
    Do you understand the difference between the two above function calls ?

  3. The following user says thank you to stampede for this useful post:

    Nick_Neo (9th June 2015)

  4. #3
    Join Date
    Jun 2015
    Posts
    6
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: To call a string containing file path in a command

    Now I get it. Thank you

  5. #4
    Join Date
    Jun 2015
    Posts
    6
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: To call a string containing file path in a command

    But what if my filename was S:/MATLAB files/test.m
    In console window of MATLAB only the first part of the command i.e., run('S:/MATLAB is being executed giving an error :A MATLAB string constant is not terminated properly.

  6. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: To call a string containing file path in a command

    Use another variant of start method :
    Qt Code:
    1. QString app( "C:/Program Files/MATLAB/R2013a/bin/matlab.exe" );
    2. QStringList arguments;
    3. arguments << "-nosplash" << "-nojvm" << "-r" << QString("run('%1')").arg(filename);
    4. p->start(app,arguments);
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to Lesiok for this useful post:

    Nick_Neo (12th June 2015)

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: To call a string containing file path in a command

    You need to use proper shell escaping or use a variant of QProcess::start() which accepts arguments in separate strings.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. How i Get Path From Command Line Argument
    By METEOR7 in forum Qt Programming
    Replies: 4
    Last Post: 17th November 2011, 18:12
  2. Replies: 3
    Last Post: 8th June 2011, 06:36
  3. Replies: 6
    Last Post: 15th April 2010, 20:02
  4. Replies: 4
    Last Post: 14th July 2009, 03:27
  5. call bash/terminal command with QT
    By myta212 in forum Newbie
    Replies: 1
    Last Post: 13th November 2006, 15:38

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.