Results 1 to 6 of 6

Thread: Execute window command line-by-line using QProcess

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Execute window command line-by-line using QProcess

    QIODevice relies on the Qt event loop to process writes to the subprocess, and that is not functioning here. You need to waitForBytesWritten()/waitForBytesRead(), or you need to write using asynchronous methods. Here is a very crude example:
    Qt Code:
    1. #include <QtCore>
    2.  
    3. class PopLister: public QObject
    4. {
    5. Q_OBJECT
    6. public:
    7. enum State { Starting, SentUser, SentPass, SentList, ReadList, Stopping };
    8.  
    9. explicit PopLister(QObject *p = 0):
    10. QObject(p),
    11. process(new QProcess(this)),
    12. state(Starting)
    13. {
    14. process->setProcessChannelMode(QProcess::MergedChannels);
    15. connect(process, SIGNAL(readyRead()), SLOT(handleOutput()));
    16. }
    17.  
    18. void start()
    19. {
    20. args << "s_client" << "-connect" << "server:995";
    21. process->start("openssl", args);
    22. state = Starting;
    23. }
    24.  
    25. private slots:
    26. void handleOutput() {
    27. while (process->canReadLine()) {
    28. QByteArray line = process->readLine();
    29.  
    30. if (line.startsWith("+OK")) {
    31. switch (state) {
    32. case Starting:
    33. process->write("USER username\r\n");
    34. state = SentUser;
    35. break;
    36. case SentUser:
    37. process->write("PASS password\r\n");
    38. state = SentPass;
    39. break;
    40. case SentPass:
    41. listData.clear();
    42. process->write("LIST\r\n");
    43. state = SentList;
    44. break;
    45. case SentList:
    46. state = ReadList;
    47. break;
    48. case ReadList: // suppress compiler warnings
    49. case Stopping:
    50. break;
    51. }
    52. }
    53. else if(line.startsWith("-ERR")) {
    54. qDebug() << line;
    55. process->write("QUIT\r\n");
    56. state = Stopping;
    57. }
    58. else if (state == ReadList) {
    59. if (line == ".\r\n") {
    60. qDebug() << listData;
    61. process->write("QUIT\r\n");
    62. state = Stopping;
    63. }
    64. else {
    65. listData += line;
    66. }
    67. }
    68. else if (state == Stopping) {
    69. qDebug() << line;
    70. }
    71. }
    72. }
    73. private:
    74. QProcess *process;
    75. State state;
    76. QByteArray listData;
    77. };
    78.  
    79. int main(int argc, char **argv)
    80. {
    81. QCoreApplication app(argc, argv);
    82. PopLister lister;
    83. lister.start();
    84. return app.exec();
    85. }
    86. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  2. The following user says thank you to ChrisW67 for this useful post:

    nhocjerry (29th July 2013)

Similar Threads

  1. Replies: 1
    Last Post: 30th March 2012, 16:35
  2. Replies: 3
    Last Post: 9th March 2011, 13:52
  3. Replies: 7
    Last Post: 15th November 2010, 10:00
  4. QProcess and the command line
    By auba in forum Qt Programming
    Replies: 17
    Last Post: 27th May 2009, 11:39
  5. Is there a command-line window in Qt4?
    By miaoliang in forum Qt Programming
    Replies: 2
    Last Post: 8th November 2006, 09:56

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.