Results 1 to 14 of 14

Thread: QProcess again ..

  1. #1
    Join Date
    May 2006
    Posts
    14
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default QProcess again ..

    hey guys.
    i'm already really confused ...
    i have read very nice tutorial to client - server application.
    http://www.codeskipper.com/article/3/
    I can understand it well. Now I would like to implement on the server side following feature:

    -i send a command from the client: for example ipconfig
    -server side recieve this text, recognize it as some command, for example using if() and sends it to the console
    -than the stdout output will be send to the client back


    I have tried system("ipconfig") - but it still executes command window(under windows xp)
    I have tried qprocess ( i guess that is the way) but may be I can just use this class, it doesn't work.

    Could anybody help me please with this or give me some idea? May be I did not understood how to use streams or how to send it to the socket or I don't know. I'm really

    thank you guys.

    i use qt3

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess again ..

    QProcess does work. See the "process" example that comes with Qt 3.

  3. #3
    Join Date
    May 2006
    Posts
    14
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QProcess again ..

    I have had a look on it. Process is started correctly, but the stdout data are not transfered to the string.

    Qt Code:
    1. void ServerMainWindow::readFromStdout(){
    2. QByteArray data = proc->readStdout();
    3. lineEdit1->setText(QString(data));
    4. sendToClients(QString(data));
    5. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess again ..

    Is that ServerMainWindow::readFromStdout() method being invoked at all?

  5. #5
    Join Date
    May 2006
    Posts
    14
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QProcess again ..

    Yes, I had a QMessage there

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess again ..

    What is the size of that "data" byte array after readStdout()? What signal did you connect that ServerMainWindow::readFromStdout() slot to?

  7. #7
    Join Date
    May 2006
    Posts
    14
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QProcess again ..

    signal is coorect i think, here is a constructor for the main class:

    Qt Code:
    1. ServerMainWindow::ServerMainWindow(QWidget* parent, const char* name)
    2. : MainWindowBase(parent, name),
    3. m_server(0)
    4. {
    5. m_clients.setAutoDelete(true);
    6.  
    7.  
    8. proc = new QProcess(this);
    9.  
    10. proc->addArgument("ipconfig");
    11.  
    12.  
    13. QObject::connect(proc,SIGNAL(readyReadStdout()),this,SLOT(readFromStdout()));
    14. QObject::connect(m_start, SIGNAL(clicked()), this, SLOT(slotStartClicked()));
    15. QObject::connect(m_stop, SIGNAL(clicked()), this, SLOT(slotStopClicked()));
    16. }
    To copy to clipboard, switch view to plain text mode 



    size of the byte array ... i think i cant understand... if you are asking regarding how much data will flow throuh ... just write ipconfig in you console

    actually i could send you files if you like ... i have no problem with

  8. #8
    Join Date
    May 2006
    Posts
    14
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QProcess again ..

    may be question: when should I execute proc->start()

    -is my proc->readStdout() still ready to read stdout?

    im

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess again ..

    Quote Originally Posted by cmeliak
    size of the byte array ... i think i cant understand...
    I wanted you to check if you actually read something with readStdout():
    Qt Code:
    1. void ServerMainWindow::readFromStdout(){
    2. QByteArray data = proc->readStdout();
    3. qWarning( "%d", data.size() );
    4. lineEdit1->setText(QString(data));
    5. sendToClients(QString(data));
    6. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by cmeliak
    when should I execute proc->start()
    After you connect to readyReadStdout() signal.

  10. #10
    Join Date
    May 2006
    Posts
    14
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QProcess again ..

    oou ... i always started process before i connected it to the signal ...

    now i am able to see output in my server application window

    jacek i still can't see output from qWarning ... should be in a cmd window? i am sure it is executed, because it runs line with lineEdit1..

    Qt Code:
    1. void ServerMainWindow::readFromStdout(){
    2. QByteArray data = proc->readStdout();
    3. qWarning( "%d", data.size() );
    4. lineEdit1->setText(QString(data));
    5. sendToClients(QString(data));
    6. }
    To copy to clipboard, switch view to plain text mode 


    and there is maybe issue with sendToClients ...

    Qt Code:
    1. void ServerMainWindow::sendToClients(const QString& text)
    2. {
    3. if (text.isNull()) return;
    4.  
    5.  
    6. // iterate over all clients and send them the text
    7. QPtrDictIterator<Client> iter(m_clients);
    8. for (; iter.current() != 0; ++iter)
    9. {
    10. QSocket* sock = iter.current()->socket();
    11. QTextStream stream(sock);
    12. stream << text;
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    i call this function from ServerMainWindow::readFromStdout(), is it correct overloaded?

  11. #11
    Join Date
    May 2006
    Posts
    14
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QProcess again ..

    bardzo diekuju za twoja pomoc

  12. #12
    Join Date
    May 2006
    Posts
    14
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QProcess again ..

    it works fine now:

    Qt Code:
    1. ServerMainWindow::ServerMainWindow(QWidget* parent, const char* name)
    2. : MainWindowBase(parent, name),
    3. m_server(0)
    4. {
    5. m_clients.setAutoDelete(true);
    6.  
    7.  
    8. proc = new QProcess(this);
    9.  
    10. proc->addArgument("ipconfig");
    11. proc->addArgument("-all");
    12.  
    13.  
    14. QObject::connect(proc,SIGNAL(readyReadStdout()),this,SLOT(readFromStdout()));
    15. QObject::connect(m_start, SIGNAL(clicked()), this, SLOT(slotStartClicked()));
    16. QObject::connect(m_stop, SIGNAL(clicked()), this, SLOT(slotStopClicked()));
    17. proc->start();
    18. }
    19.  
    20. ////////////////////////////////////////////////////////////////////////////////
    21.  
    22. void ServerMainWindow::readFromStdout(){
    23. // QByteArray data = proc->readStdout();
    24. QString data = proc->readStdout();
    25. textEdit1->setText(data);
    26. sendToClients(data);
    27. }
    28. ////////////////////////////////////////////////////////////////////////////////
    29.  
    30. ServerMainWindow::~ServerMainWindow()
    31. {
    32. slotStopClicked();
    33. }
    34.  
    35. ////////////////////////////////////////////////////////////////////////////////
    36.  
    37. void ServerMainWindow::sendToClients(const QString& text)
    38. {
    39. if (text.isNull()) return;
    40.  
    41.  
    42. // iterate over all clients and send them the text
    43. QPtrDictIterator<Client> iter(m_clients);
    44. for (; iter.current() != 0; ++iter)
    45. {
    46. QSocket* sock = iter.current()->socket();
    47. QTextStream stream(sock);
    48. stream << text;
    49. }
    50. }
    To copy to clipboard, switch view to plain text mode 


    but still my client doesn't recive stdout from ipconfig
    function sendToClients(data); should send QString to void ServerMainWindow::sendToClients(const QString& text)

    is the function sendToClients(const QString& text) correct? or could be there some issue in the client application?

    client function that recieves data from server app is:
    Qt Code:
    1. void ClientMainWindow::slotRead()
    2. {
    3. QString text;
    4. while (m_socket->canReadLine())
    5. text += m_socket->readLine();
    6.  
    7. if (!text.isNull())
    8. appendText(text, Output);
    9. }
    10.  
    11.  
    12. where
    13.  
    14.  
    15.  
    16. void ClientMainWindow::appendText(const QString& text, Mode mode)
    17. {
    18. switch (mode)
    19. {
    20. case System:
    21. m_textEdit->setColor(Qt::blue);
    22. break;
    23.  
    24. case Error:
    25. m_textEdit->setColor(Qt::red);
    26. break;
    27.  
    28. default:
    29. m_textEdit->setColor(Qt::black);
    30. break;
    31. }
    32.  
    33. m_textEdit->append(text);
    34. }
    To copy to clipboard, switch view to plain text mode 

  13. #13
    Join Date
    May 2006
    Posts
    14
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QProcess again ..

    OK I found it already
    Thank you jacek 4 your time

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess again ..

    Quote Originally Posted by cmeliak
    i still can't see output from qWarning ... should be in a cmd window?
    If you use windows, you should add "CONFIG += console" to the .pro file.

    Quote Originally Posted by cmeliak
    bardzo diekuju za twoja pomoc
    Nie ma za co

Similar Threads

  1. QProcess +standard error + windows
    By antonio.r.tome in forum Qt Programming
    Replies: 0
    Last Post: 18th April 2006, 14:58
  2. QProcess start automaticaly needed application
    By raphaelf in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2006, 14:11
  3. Replies: 4
    Last Post: 13th February 2006, 11:35
  4. speed up qprocess readstdout
    By kooshball in forum Qt Programming
    Replies: 1
    Last Post: 31st January 2006, 09:42
  5. QProcess in a QThread
    By chombium in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2006, 15:52

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.