Results 1 to 8 of 8

Thread: System Command QProcess

  1. #1
    Join Date
    Feb 2008
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default System Command QProcess

    Hello folks,

    I'll execute a system command on a Linux maschine. I've created this in a QProcess, but the result was very small.

    Qt Code:
    1. QString cmd("dpkg");
    2.  
    3. QProcess* process = new QProcess(this);
    4. process->start(QFile::encodeName(cmd).data(), QStringList() << "-l" << "| grep" << "xorg");
    5. process->waitForFinished();
    6.  
    7. QString tmp = process->readAll();
    8. qDebug() << tmp;
    To copy to clipboard, switch view to plain text mode 

    The return has a length from one line. This is wrong if I type dpgk -l | grep xorg I becomes over 20 Lines.

    If I try this:
    Qt Code:
    1. process->start(QFile::encodeName(cmd).data(), QStringList() << "-l");
    To copy to clipboard, switch view to plain text mode 

    So, with one argument all works fine, if I had more as one argument, the result ist not correctly.


    Can someone tell me how to fix the argument list?

    Thanks.

    so long
    jd

    // Sorry wrong forum, can someone move my post.
    Last edited by jd; 10th February 2009 at 14:58.
    The most difficult in the world is to live in it

  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: System Command QProcess

    That's not a single command. You need to launch a shell to interpret the commands. See for example this post.
    J-P Nurmi

  3. #3
    Join Date
    Feb 2008
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: System Command QProcess

    Hello,

    I've modified my function, but it doesn't work. I've no result...

    Qt Code:
    1. QStringList arguments;
    2. arguments << "-c" << "dpkg" << "-l" << "|" << "grep" << "xorg";
    3. QProcess* process = new QProcess(this);
    4. process->start("/bin/sh", arguments);
    5. process->waitForFinished();
    6.  
    7. QString tmp = process->readAll();
    8. qDebug() << tmp;
    To copy to clipboard, switch view to plain text mode 


    I've executed the command in the shell manually and this works fine:
    /bin/sh -c "dpkg -l | grep xorg"
    So, were is the problem in my code.

    so long and thanks for help

    jd
    The most difficult in the world is to live in it

  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: System Command QProcess

    Try
    Qt Code:
    1. arguments << "-c" << "dpkg -l | grep xorg";
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    jd (10th February 2009)

  6. #5
    Join Date
    Feb 2008
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: System Command QProcess

    Thanks, works fine now.
    The most difficult in the world is to live in it

  7. #6
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: System Command QProcess

    Hi jpn,

    though the query is solved now, i would appreciate it if u tell me why it worked with combining last 5 arguments into one string argument and not otherwise

    thanks a heap!

  8. #7
    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: System Command QProcess

    Quote Originally Posted by talk2amulya View Post
    i would appreciate it if u tell me why it worked with combining last 5 arguments into one string argument and not otherwise
    Because it's a single argument for /bin/sh. From command line you'd run it like this:
    bash Code:
    1. /bin/sh -c "dpkg -l | grep xorg"
    To copy to clipboard, switch view to plain text mode 
    not like this:
    bash Code:
    1. /bin/sh -c dpkg -l | grep xorg
    To copy to clipboard, switch view to plain text mode 
    Right?
    J-P Nurmi

  9. #8
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: System Command QProcess

    i m gonna hide under my bed..that was so stupid, i should've caught that..thanks jpn!

Similar Threads

  1. How to get system drive?
    By Raccoon29 in forum General Programming
    Replies: 2
    Last Post: 23rd November 2008, 14:13
  2. Using QT for Digital TV menu system?
    By mittalpa in forum Newbie
    Replies: 4
    Last Post: 2nd July 2008, 19:23
  3. system() function and QProgressBar
    By rajeshs in forum Qt Programming
    Replies: 10
    Last Post: 4th June 2008, 11:50
  4. System environment variable
    By fahlen in forum Qt Programming
    Replies: 4
    Last Post: 27th November 2007, 19:02
  5. Qt Cryptographic Architecture
    By vermarajeev in forum Qt Programming
    Replies: 6
    Last Post: 9th February 2007, 13:15

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.