Results 1 to 7 of 7

Thread: QProcess ping

  1. #1
    Join Date
    Dec 2018
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Unhappy QProcess ping

    Hello there. I'm trying to make a ping application. The application start but nothing is shown.
    Here is my code.
    Qt Code:
    1. #include "ping.h"
    2. #include "ui_ping.h"
    3.  
    4. //#include <iostream>
    5. //#include <stdio.h>
    6. //#include <stdlib.h>
    7.  
    8. #include <QtGui>
    9. #include <QApplication>
    10. #include <QMessageBox>
    11. #include <QTextEdit>
    12. #include <QtNetwork/QTcpSocket>
    13. #include <QPlainTextEdit>
    14. #include <QtNetwork>
    15.  
    16. //using namespace std;
    17.  
    18. ping::ping(QWidget *parent) :
    19. QMainWindow(parent),
    20. ui(new Ui::ping)
    21. {
    22. ui->setupUi(this);
    23.  
    24. }
    25.  
    26. ping::~ping()
    27. {
    28. delete ui;
    29. }
    30.  
    31.  
    32. void ping::on_pushButton_clicked()
    33. {
    34. QString m_sHostName = ui->lineEdit->text();
    35.  
    36.  
    37.  
    38. // Create QProcess object
    39. QProcess *proc;
    40. proc = new QProcess();
    41. proc->start("ping", QStringList() << QString(m_sHostName), QIODevice::ReadOnly);
    42. proc->waitForFinished(-1);
    43.  
    44.  
    45.  
    46.  
    47. // Show output
    48. ui->plainTextEdit->connect(proc, SIGNAL(readyReadStandardOutput()), this, SLOT(show()));
    49. ui->plainTextEdit->connect(proc, SIGNAL(readyReadStandardError()), this, SLOT(show()));
    50.  
    51.  
    52.  
    53. }
    54.  
    55.  
    56. // Show right message
    57. void ping::rightMessage()
    58. {
    59. QProcess *proc;
    60. proc = new QProcess();
    61.  
    62.  
    63. QByteArray strdata = proc->readAllStandardOutput();
    64. strdata = strdata.simplified();
    65. strdata = strdata.trimmed();
    66.  
    67. //ui->TextEditResult->setTextColor(Qt::black);
    68. ui->plainTextEdit->appendPlainText(strdata);
    69. // ui->plainTextEdit->appendPlainText("<br>");
    70.  
    71. }
    72.  
    73.  
    74. // Show wrong message
    75. void ping::wrongMessage()
    76. {
    77. QProcess *proc;
    78. proc = new QProcess();
    79.  
    80. QByteArray strdata = proc->readAllStandardError();
    81. //ui->TextEditResult->setTextColor(Qt::red);
    82. ui->plainTextEdit->appendPlainText(strdata);
    83. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QProcess ping

    You connect to the QProgress signals after it has finished.

    You also connected to the window's show() slot and never read any data from the process.

    Cheers,
    _

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

    soulmaster17 (8th January 2019)

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QProcess ping

    I think you want to connect to the "rightMessage()" and "wrongMessage()" slots, not "show()". Maybe you got a little confused, thinking that you wanted to "show" the results of the ping operations...
    <=== 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. The following user says thank you to d_stranz for this useful post:

    soulmaster17 (8th January 2019)

  6. #4
    Join Date
    Dec 2018
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QProcess ping

    Thank you for the answers.


    I uncomment the line proc->waitForFinished(-1);
    Then i write in my main.cpp file, w.rightMessage(); instead of w.show();
    But now i get QIODevice::read (QProcess): device not open.
    Any idea?


    Added after 20 minutes:


    I also replace the word show by rightMessage() and wrongMessage() respectively, just like this
    Qt Code:
    1. ui->plainTextEdit->connect(proc, SIGNAL(readyReadStandardOutput()), this, SLOT(rightMessage()));
    To copy to clipboard, switch view to plain text mode 
    but i get
    Starting /root/build-ping-QtKit-Debug/ping...
    QIODevice::read (QProcess): device not open
    /root/build-ping-QtKit-Debug/ping exited with code 0
    Last edited by soulmaster17; 8th January 2019 at 12:47.

  7. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QProcess ping

    You connect to the QProgress signals after it has finished.
    Did you read anda_skoa's comment?
    <=== 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.

  8. #6
    Join Date
    Dec 2018
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QProcess ping

    Yes i did. But escuse me Mister, i'm a total newbie, and i really want to know how to execute linux or windows commands in a window like this. I will apreciate any suggestions from you. Take care

  9. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QProcess ping

    In both slots, rightMessage() and wrongMessage(), you are creating a new QProcess and try to read from that instead from the process that was actually executed.

    You need a QProcess as a member of your class and refer to that instance from all your methods.

    Cheers,
    _

Similar Threads

  1. Trying to ping the server via QSsl
    By gunturrohith in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 23rd October 2017, 11:17
  2. Ping from Qt Application
    By Axtroz in forum Qt Programming
    Replies: 2
    Last Post: 1st October 2012, 02:13
  3. Ping and QProcess (exitcode() question)
    By hakermania in forum Qt Programming
    Replies: 3
    Last Post: 31st August 2011, 00:29
  4. Replies: 1
    Last Post: 13th November 2008, 11:46
  5. How to ping a server ?
    By Nyphel in forum Newbie
    Replies: 2
    Last Post: 23rd April 2007, 12:27

Tags for this Thread

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.