1

I am trying to run this bash command

$pactl list sinks short | grep 10_B7_F6_02_1B_4A

in my c++ project using QProcess and to get the output using readAllStandardOutput() as shown in this post. When I put echo at the beginning of the command and put 10_B7_F6_02_1B_4A before the pipe, I get the correct output into my QByteArray. However, the output format of pactl seems to be different than that of echo. In the terminal it looks like this:

$ pactl list sinks short | grep 10_B7_F6_02_1B_4A
2       bluez_sink.10_B7_F6_02_1B_4A    module-bluez5-device.c  s16le 2ch 44100Hz       SUSPENDED

How can I read this output?

Community
  • 1
  • 1
MrUser
  • 1,187
  • 15
  • 25

1 Answers1

3

The application you are trying to read from may be sending console output to stderr, in which case you have a few options:

  • just read from stderr outright: process.readAllStandardError()

  • set the read channel to read only from stderr: process.setReadChannel(QProcess::StandardError)

  • read from stderr and stdout with reckless abandon! : process.setProcessChannelMode(QProcess::MergedChannels)

Another possibility is that you are feeding incorrect arguments into your QProcess. It seems you are trying to pipe data above, the proper way to do this with QProcess is like this:

#include <QCoreApplication>
#include <QProcess>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    QProcess pactl;
    QProcess grep;
    pactl.setStandardOutputProcess(&grep);
    pactl.start("pactl list sinks short");
    grep.start("grep 10_B7_F6_02_1B_4A");

    pactl.waitForFinished();
    grep.waitForFinished();

    qDebug() << grep.readAll();
    return EXIT_SUCCESS;
}
mbroadst
  • 768
  • 6
  • 8
  • This didn't work for me. When I put the command into the terminal, it seems to print to standard out. Also, if I send it to a file (> /temp/file) from the terminal, the file contains the correct string. However, if I try to send it to the file in my c++ project, the file is empty. Created, but empty. It seems like pactl, when invoked in my project, doesn't output anything. – MrUser Apr 23 '14 at 09:14
  • It was suggested to me that perhaps I need to place a PulseAudio cookie in my home folder in order to run commands from an application. Otherwise, I think this would have worked. Thanks. – MrUser Apr 25 '14 at 13:42