Results 1 to 7 of 7

Thread: Read stdout to a widget

  1. #1
    Join Date
    Apr 2020
    Posts
    14
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Read stdout to a widget

    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:

    Qt Code:
    1. 5A 174928000
    2. 5B 176640000
    3. 5C 178352000
    4. [LOCKED]
    5. 5D 180064000
    6. 6A 181936000
    7. 6B 183648000
    8. 6C 185360000
    9. 6D 187072000
    10. 7A 188928000
    11. 7B 190640000
    12. 7C 192352000
    13. 7D 194064000
    14. 8A 195936000
    15. 8B 197648000
    16. [LOCKED]
    17. 8C 199360000
    18. 8D 201072000
    19. 9A 202928000
    20. 9B 204640000
    21. 9C 206352000
    22. 9D 208064000
    23. 10A 209936000
    24. 10B 211648000
    25. 10C 213360000
    26. 10D 215072000
    27. 11A 216928000
    28. 11B 218640000
    29. 11C 220352000
    30. 11D 222064000
    31. 12A 223936000
    32. 12B 225648000
    33. 12C 227360000
    34. 12D 229072000
    35. 13A 230748000
    36. 13B 232496000
    37. 13C 234208000
    38. 13D 235776000
    39. 13E 237448000
    40. 13F 239200000
    41.  
    42. Scan completed
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. QProcess::execute("/opt/bin/mediaclient --scandabfrequencies /dev/dab0");
    To copy to clipboard, switch view to plain text mode 

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

    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    502
    Thanked 75 Times in 71 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read stdout to a widget

    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

  3. #3
    Join Date
    Apr 2020
    Posts
    14
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Read stdout to a widget

    Thx, i'll check that.

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

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,695
    Thanks
    259
    Thanked 753 Times in 743 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Read stdout to a widget

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Apr 2020
    Posts
    14
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Read stdout to a widget

    Quote Originally Posted by d_stranz View Post
    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:

    Qt Code:
    1. void DAB_FM::on_btnScan_clicked()
    2. {
    3. //DAB
    4. if (ui->dabButton->isChecked()){
    5.  
    6. QObject *parent;
    7.  
    8. QString program = "/opt/bin/mediaclient";
    9. QStringList arguments;
    10. arguments << "--scandabfrequencies" << "/dev/dab0";
    11.  
    12. QProcess *myProcess = new QProcess(parent);
    13. myProcess->start(program, arguments);
    14.  
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by vitalic; 19th April 2020 at 11:11.

  6. #6
    Join Date
    Apr 2020
    Posts
    14
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Read stdout to a widget

    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.

    Qt Code:
    1. void DAB_FM::on_btnScan_clicked()
    2. {
    3. //DAB
    4.  
    5. if (ui->dabButton->isChecked()){
    6.  
    7. QProcess process;
    8. process.start("/opt/bin/mediaclient --scandabfrequencies /dev/dab0");
    9. process.waitForFinished();
    10. QByteArray output(process.readAllStandardOutput());
    11. ui->listScan->addItem(output);
    12.  
    13. }
    14.  
    15. //FM
    16.  
    17. if (ui->fmButton->isChecked()){
    18.  
    19. QProcess process;
    20. process.start("/opt/bin/mediaclient --scanfmfrequencies /dev/radio0");
    21. process.waitForFinished();
    22. QByteArray output(process.readAllStandardOutput());
    23. ui->listScan->addItem(output);
    24.  
    25. }
    26.  
    27. }
    To copy to clipboard, switch view to plain text mode 

    Result for FM Scan:

    Auswahl_013.png

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,695
    Thanks
    259
    Thanked 753 Times in 743 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Read stdout to a widget

    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.

    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).
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QProcess - read from stdout lively
    By dotjan in forum Newbie
    Replies: 8
    Last Post: 6th April 2014, 23:07
  2. Replies: 1
    Last Post: 19th September 2011, 11:43
  3. QProcess can read stderr but no stdout
    By mastupristi in forum Qt Programming
    Replies: 3
    Last Post: 21st October 2010, 10:47
  4. Read most recent output from QProcess's stdout
    By Lawand in forum Qt Programming
    Replies: 1
    Last Post: 8th September 2010, 23:29
  5. read stdout with QProcess under Windows
    By jlbrd in forum Qt Programming
    Replies: 4
    Last Post: 1st September 2006, 18:29

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.