Results 1 to 20 of 20

Thread: Terminating and restarting an external program in linux and windows

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2007
    Posts
    244
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    42
    Thanked 8 Times in 8 Posts

    Default Re: Terminating and restarting an external program in linux and windows

    Quote Originally Posted by bender86 View Post
    Qt Code:
    1. void MainApp::recvQuit()
    2. {
    3. QDataStream in(tcpSocket,QIODevice::ReadOnly);
    4. in.setVersion(QDataStream::Qt_4_0);
    5. QString string;
    6. in >> string;
    7. if(string == QString("quit")) {
    8. // Quit application.
    9. QCoreApplication::instance()->quit();
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    Is right connecting recvQuit() to QTcpServer::newConnection() signal?

    In MainApp I haven't QTcpSocket object (only QTcpServer): I need to create one, right?

    Thanks
    Giuseppe CalÃ

  2. #2
    Join Date
    Nov 2007
    Posts
    89
    Qt products
    Qt4
    Platforms
    Windows
    Thanked 21 Times in 18 Posts

    Default Re: Terminating and restarting an external program in linux and windows

    Quote Originally Posted by jiveaxe View Post
    Is right connecting recvQuit() to QTcpServer::newConnection() signal?
    No, you should get new connection socket, and connect its readyRead signal to recvQuit.



    In MainApp I haven't QTcpSocket object (only QTcpServer): I need to create one, right?
    When QTcpServer::newConnection is emitted, you should do:
    Qt Code:
    1. void MainApp::newConnectionSlot()
    2. {
    3. // Loop on all incoming connections.
    4. while(tcpServer->hasPendingConnections()) {
    5. QTcpSocket *socket = tcpServer->nextPendingConnection();
    6. // Connect socket's signal, etc...
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Aug 2007
    Posts
    244
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    42
    Thanked 8 Times in 8 Posts

    Default Re: Terminating and restarting an external program in linux and windows

    Hi,
    finally the problem is solved thanks to bender86 last post. This is the final code (if it may be useful to somebody else):

    Qt Code:
    1. class MainApp: public QDialog
    2. {
    3. ...
    4. private slots:
    5. void startUpdater();
    6. void readIncomingData();
    7. void newConnectionSlot();
    8. ...
    9.  
    10. private:
    11. QProcess *myProcess;
    12. QTcpServer *tcpServer;
    13. QTcpSocket *tcpSocket;
    14. ...
    15. };
    16.  
    17. MainApp::MainApp(QWidget *parent)
    18. :QDialog(parent)
    19. {
    20. myProcess = 0;
    21.  
    22. ...
    23.  
    24. tcpServer = new QTcpServer(this);
    25. if (!tcpServer->listen()) {
    26. QMessageBox::critical(this, tr("Server"),
    27. tr("Unable to start the server: %1.")
    28. .arg(tcpServer->errorString()));
    29. close();
    30. return;
    31. }
    32.  
    33. connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnectionSlot()));
    34.  
    35. ...
    36.  
    37. connect(pushButton_1, SIGNAL(clicked()), this, SLOT(startUpdater()));
    38.  
    39. ...
    40. }
    41.  
    42. void MainApp::startUpdater()
    43. {
    44. QString program = "updater";
    45. args << port.toString();
    46.  
    47. ...
    48.  
    49. myProcess = new QProcess(this);
    50. myProcess->startDetached(program, args);
    51. }
    52.  
    53. void MainApp::newConnectionSlot()
    54. {
    55. while(tcpServer->hasPendingConnections()) {
    56. tcpSocket = tcpServer->nextPendingConnection();
    57. connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readIncomingData()));
    58. }
    59. }
    60.  
    61. void MainApp::readIncomingData()
    62. {
    63. ...
    64.  
    65. QDataStream in(tcpSocket);
    66. in.setVersion(QDataStream::Qt_4_0);
    67. QString string;
    68. in >> string;
    69. if(string == QString("quit")) {
    70. // Quit application.
    71. QCoreApplication::instance()->quit();
    72. }
    73. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class Updater: public QDialog
    2. {
    3. ...
    4. private slots:
    5. void sendQuitCommand();
    6. ...
    7.  
    8. private:
    9. QTcpSocket *tcpSocket;
    10. ...
    11. };
    12.  
    13. Updater::Updater(char *p, QWidget *parent)
    14. :QDialog(parent)
    15. {
    16. port = p;
    17.  
    18. ...
    19.  
    20. tcpSocket = new QTcpSocket(this);
    21. tcpSocket->connectToHost("localhost", port.toInt());
    22.  
    23. ...
    24. }
    25.  
    26. void Updater::sendQuitCommand()
    27. {
    28. QDataStream out(tcpSocket);
    29. out.setVersion(QDataStream::Qt_4_0);
    30. out << QString("quit");
    31. tcpSocket->disconnectFromHost();
    32. }
    To copy to clipboard, switch view to plain text mode 

    Regards
    Giuseppe CalÃ

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
  •  
Qt is a trademark of The Qt Company.