Results 1 to 7 of 7

Thread: Call script shell with Qt

  1. #1
    Join Date
    Feb 2009
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Call script shell with Qt

    Hi,
    why this code doesn't work ???

    Qt Code:
    1. QStringList arguments;
    2. arguments << "-c" << "who|cut -d' ' -f1|sort -u";
    3. QProcess *exec;
    4. exec =new QProcess(this);
    5. exec->start("/bin/sh", arguments);
    6. ui->label->setText(exec->readLine(10));
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Call script shell with Qt

    QProcess works asynchronously. Please read QProcess docs. You might be interested in "Synchronous Process API".
    J-P Nurmi

  3. #3
    Join Date
    Feb 2009
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Call script shell with Qt

    ok i do ... but the real problem it is ...

    for Run matlab in batch mode the command in the shell is

    Qt Code:
    1. matlab -nojvm -nosplash -r MyCommand
    To copy to clipboard, switch view to plain text mode 

    where MyCommand is the name of m file ...
    first question :
    how to call this in my Qt app...???
    if i do
    Qt Code:
    1. arguments << "matlab -nojvm -nosplash -nodisplay -r addmatrix";
    2. exec->start("/bin/bash", arguments);
    To copy to clipboard, switch view to plain text mode 
    don't work
    and second question :
    if i open the terminal can i send the comand from my qt app???
    sorry for the trouble and thanks in advance

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Call script shell with Qt

    Quote Originally Posted by zeeb100 View Post
    Qt Code:
    1. matlab -nojvm -nosplash -r MyCommand
    To copy to clipboard, switch view to plain text mode 
    These look like individual command line arguments to me.

    Qt Code:
    1. arguments << "matlab -nojvm -nosplash -nodisplay -r addmatrix";
    2. exec->start("/bin/bash", arguments);
    To copy to clipboard, switch view to plain text mode 
    Here you are not doing anything that would have to be interpret by a shell like you did in the first post.

    if i open the terminal can i send the comand from my qt app???
    Sorry, I don't understand. What do you mean?
    J-P Nurmi

  5. #5
    Join Date
    Feb 2009
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Call script shell with Qt

    before I have used
    Qt Code:
    1. while(exec->waitForReadyRead())
    2. ui->label->setText(exec->readLine(10));
    To copy to clipboard, switch view to plain text mode 
    and this works

    Here you are not doing anything that would have to be interpret by a shell like you did in the first post.
    What can i do ??


    Sorry, I don't understand. What do you mean?
    I manually launch shell and manually the start matlab in bash mode.
    after start my Qt program and redirect the standartd input on the standard input of the shell as if the program wrote in shell to the place mine....CAN I DO THIS ???

  6. #6
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Call script shell with Qt

    I think this:
    Qt Code:
    1. matlab -nojvm -nosplash -r MyCommand
    To copy to clipboard, switch view to plain text mode 
    should look:
    Qt Code:
    1. arguments << "-nojvm" << "-nosplash" << "-nodisplay" << "-r" << "addmatrix";
    2. exec->start("matlab", arguments);
    To copy to clipboard, switch view to plain text mode 

    I manually launch shell and manually the start matlab in bash mode.
    after start my Qt program and redirect the standartd input on the standard input of the shell as if the program wrote in shell to the place mine....CAN I DO THIS ???
    As I understand, you want to pass the input from you Qt app to the matlab?
    I would try starting matlab with a QProcess, and then writing and reading it's input/output, like sample code in assistant:
    Qt Code:
    1. QProcess gzip;
    2. gzip.start("gzip", QStringList() << "-c");
    3. if (!gzip.waitForStarted())
    4. return false;
    5.  
    6. gzip.write("Qt rocks!");
    7. gzip.closeWriteChannel();
    8.  
    9. if (!gzip.waitForFinished())
    10. return false;
    11.  
    12. QByteArray result = gzip.readAll();
    To copy to clipboard, switch view to plain text mode 
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  7. #7
    Join Date
    Feb 2009
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Call script shell with Qt

    thanks for help ... it work... the right code is:

    Qt Code:
    1. QStringList argo, list;
    2. QProcess *exec;
    3. exec =new QProcess(this);
    4. argo <<"/Applications/MATLAB_R2008a/bin/matlab"<<"-nojvm -nosplash -nodisplay -r 'addmatrix(2,7)'";
    5. list <<"PATH=/sw/bin:/sw/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/X11R6/bin";
    6. exec->setEnvironment(list);
    7.  
    8. exec->start("/bin/bash", argo);
    To copy to clipboard, switch view to plain text mode 

    if there are a different mode more easy please write

Similar Threads

  1. How to use shell script files ?
    By npc in forum Newbie
    Replies: 3
    Last Post: 15th February 2007, 08:26

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.