PDA

View Full Version : Read stdout to a widget



vitalic
18th April 2020, 17:27
Hi,

I started to prog my own FM/DAB Radio app for a specifix dongle.

Right now I start a process to scan for frequencies. The results should be listed vertically in a widget, but everything I tried failed.

Output from terminal when command has finished:



5A 174928000
5B 176640000
5C 178352000
[LOCKED]
5D 180064000
6A 181936000
6B 183648000
6C 185360000
6D 187072000
7A 188928000
7B 190640000
7C 192352000
7D 194064000
8A 195936000
8B 197648000
[LOCKED]
8C 199360000
8D 201072000
9A 202928000
9B 204640000
9C 206352000
9D 208064000
10A 209936000
10B 211648000
10C 213360000
10D 215072000
11A 216928000
11B 218640000
11C 220352000
11D 222064000
12A 223936000
12B 225648000
12C 227360000
12D 229072000
13A 230748000
13B 232496000
13C 234208000
13D 235776000
13E 237448000
13F 239200000

Scan completed


The way i start the command when scan button is pushed:



QProcess::execute("/opt/bin/mediaclient --scandabfrequencies /dev/dab0");


I'd like to have these results except the empty line and "Scan completed" in an editable list.

Regards

ChristianEhrlicher
18th April 2020, 18:10
Please take a look at the documentation: https://doc.qt.io/qt-5/qprocess.html#details

There you will find how to read from stdout

vitalic
18th April 2020, 19:13
Thx, i'll check that.

Which widget would be the best to show the results and make each line clickable for the user?

d_stranz
18th April 2020, 23:07
make each line clickable for the user?

And what is supposed to happen when the user clicks on a line?

I would probably start by looking at QListWidget or QTableWidget depending on whether you want one or two columns in your display.

vitalic
19th April 2020, 07:56
And what is supposed to happen when the user clicks on a line?

I would probably start by looking at QListWidget or QTableWidget depending on whether you want one or two columns in your display.

The aim is to click on the found frequency in the list and then to tune to the station.

Trying to build the example for QProcess but getting an error:



void DAB_FM::on_btnScan_clicked()
{
//DAB
if (ui->dabButton->isChecked()){

QObject *parent;

QString program = "/opt/bin/mediaclient";
QStringList arguments;
arguments << "--scandabfrequencies" << "/dev/dab0";

QProcess *myProcess = new QProcess(parent);
myProcess->start(program, arguments);

}
}


Warning says: /home/ubuntu/DAB_FM/dab_fm.cpp:98: Warnung: variable 'parent' is uninitialized when used here

When clicking on scan, the app crashes.

Edit:

From some tutorials i decided to use the ListWidget. The question for me is, how can I read the stdout from QProcess::execute and pass the returned stdout to the ListWidget.

vitalic
19th April 2020, 11:13
Ok, so far I'm able to read the output now, but the result is shown as one item in the widget. I'd like to have each line of the output as one item.



void DAB_FM::on_btnScan_clicked()
{
//DAB

if (ui->dabButton->isChecked()){

QProcess process;
process.start("/opt/bin/mediaclient --scandabfrequencies /dev/dab0");
process.waitForFinished();
QByteArray output(process.readAllStandardOutput());
ui->listScan->addItem(output);

}

//FM

if (ui->fmButton->isChecked()){

QProcess process;
process.start("/opt/bin/mediaclient --scanfmfrequencies /dev/radio0");
process.waitForFinished();
QByteArray output(process.readAllStandardOutput());
ui->listScan->addItem(output);

}

}


Result for FM Scan:

13400

d_stranz
19th April 2020, 17:01
QByteArray output(process.readAllStandardOutput());
ui->listScan->addItem(output);


Well if you are reading it all into a single QByteArray, and passing that entire array to addItem(), the list widget is doing exactly what you told it to - add a single item.

Most of this basic stuff is covered in the Qt documentation and the Qt examples and tutorials. (https://doc.qt.io/qt-5/qtexamplesandtutorials.html)

Look at QString and its constructor that takes a QByteArray as input, QString::split(), QStringList, and QListWidget::addItems(), QListWidget::itemClicked() (or itemDoubleClicked() depending on the behavior you want).