Results 1 to 3 of 3

Thread: Trouble with QProcess and getting back standard output

  1. #1
    Join Date
    Jun 2011
    Posts
    23
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Question Trouble with QProcess and getting back standard output

    Hi,

    I have a script that returns a number that is the percentage of partition used, it looks like

    Qt Code:
    1. #!/bin/bash
    2. case $1 in
    3. root_partition)
    4. devname="/dev/sda1"
    5. let p=`df -k $devname | grep -v ^File | awk '{printf ("%i",$3*100 / $2); }'`
    6. echo $p
    7. ;;
    8. default)
    9. echo "Unknown command"
    10. ;;
    11. esac
    12. exit 0
    To copy to clipboard, switch view to plain text mode 

    I have a QProcess that looks like this
    Qt Code:
    1. QStringList arguments;
    2. QString program = "script.sh";
    3. arguments.clear();
    4. _proc = new QProcess(this);
    5. qDebug("proc running");
    6. arguments << " root_partition ";
    7.  
    8. if (_proc->state() == QProcess::NotRunning)
    9. {
    10. _proc->disconnect();
    11. connect(_proc, SIGNAL(readyReadStandardOutput()), this, SLOT(outLog()));
    12. _proc->start(program, arguments);
    13. _proc->setReadChannel(QProcess::StandardOutput);
    14.  
    15. _proc->waitForFinished();
    16. }
    17. }
    18.  
    19. void MyClass::outLog()
    20. {
    21. qDebug("outLog");
    22.  
    23. QByteArray array = _proc->readAllStandardOutput();
    24. }
    To copy to clipboard, switch view to plain text mode 


    My problem is that running the script by hand returns 87 - the % used on that partition. But running the script in my QT code does not work. If I replace the line
    Qt Code:
    1. QString program = "script.sh";
    To copy to clipboard, switch view to plain text mode 
    with
    Qt Code:
    1. QString program = "echo";
    To copy to clipboard, switch view to plain text mode 

    I get "root partition" returned - as expected. But why cannot I not anything back from running the script in Qt? The outLog function never gets called? Is echo not standard output?

    I have done quite a bit of Googling but other people working solutions are not working for me?

    Thank you very much for helping.
    cheers,
    Tara

    Qt4 on Linux

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Trouble with QProcess and getting back standard output

    You are using a relative path to the script. Ensure the current working directory of your Qt program is where you think it is, that the script is there, and that the current working directory is in PATH. You could use a fully qualified path to the script or ensure the script is on the system PATH (like echo) which quite often does not include the current working directory.

    Also check what QProcess::error() returns.

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

    taraj (7th June 2013)

  4. #3
    Join Date
    Jun 2011
    Posts
    23
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Trouble with QProcess and getting back standard output

    Thanks!

    Changed
    Qt Code:
    1. QString program = "script.sh";
    To copy to clipboard, switch view to plain text mode 

    to

    Qt Code:
    1. QString program = QDir().absoluteFilePath("script.sh");
    To copy to clipboard, switch view to plain text mode 

    and all working now!

Similar Threads

  1. QProcess Standard Output
    By Decessus in forum Qt Programming
    Replies: 4
    Last Post: 15th August 2012, 17:10
  2. Replies: 2
    Last Post: 29th November 2009, 17:22
  3. Replies: 7
    Last Post: 8th May 2009, 10:26
  4. Replies: 2
    Last Post: 2nd June 2008, 08:45
  5. redirection of standard input and output
    By guestgulkan in forum Qt Programming
    Replies: 2
    Last Post: 15th October 2006, 20:30

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.