Results 1 to 2 of 2

Thread: Problems communicating with a process (Gnuchess) via QProcess

  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Problems communicating with a process (Gnuchess) via QProcess

    Hi,
    I can't get to communicate with Gnuchess. When I start it using the -x flag nothing happens, and there is no data at all to read from. If I start it without any commandline flags I get some info about gnuchess and I (white) am prompted to make my move but it won't react to any of the commands I write to it using QProcess::write():

    GNU Chess 5.07
    Adjusting HashSize to 1024 slots
    Transposition table: Entries=1K Size=40K
    Pawn hash table: Entries=0K Size=28K
    White (1) :
    Below is a minimal test app I've made to show the problem. You will most likely have to edit line 35 and change the path to gnuchess if you want it to work. Of course Gnuchess needs to be installed ;-). Any ideas?

    Thanks in advance

    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class MainWindow : public QMainWindow
    5. {
    6. Q_OBJECT
    7. public:
    8. MainWindow(QWidget* parent = 0) : QMainWindow(parent) {
    9. m_log = new QTextBrowser;
    10.  
    11. m_lineEdit = new QLineEdit;
    12. connect(m_lineEdit, SIGNAL(returnPressed()), this, SLOT(send()));
    13.  
    14. QPushButton* sendButton = new QPushButton(tr("Send"));
    15. connect(sendButton, SIGNAL(clicked()), this, SLOT(send()));
    16.  
    17. QPushButton* receiveButton = new QPushButton(tr("Read"));
    18. connect(receiveButton, SIGNAL(clicked()), this, SLOT(readDataOnButtonPress()));
    19.  
    20. blo->addWidget(m_lineEdit);
    21. blo->addWidget(sendButton);
    22. blo->addWidget(receiveButton);
    23.  
    24. QWidget* cw = new QWidget;
    25. cw->setLayout(hlo);
    26. hlo->addWidget(m_log);
    27. hlo->addLayout(blo);
    28.  
    29. m_engine = new QProcess(this);
    30. m_engine->setProcessChannelMode(QProcess::MergedChannels);
    31. m_engine->setReadChannel(QProcess::StandardOutput);
    32. connect(m_engine, SIGNAL(readyRead()), this, SLOT(readData()));
    33. m_engine->start("/usr/games/bin/gnuchess", QStringList() << "-x");
    34.  
    35. setCentralWidget(cw);
    36. }
    37.  
    38. ~MainWindow() {
    39. m_engine->terminate();
    40. m_engine->kill();
    41. }
    42.  
    43. private:
    44. QTextBrowser* m_log;
    45. QProcess* m_engine;
    46. QLineEdit* m_lineEdit;
    47.  
    48. private slots:
    49. void readData() {
    50.  
    51. ba = m_engine->readAllStandardOutput().trimmed();
    52. if (!ba.trimmed().isEmpty()) {
    53. m_log->append(ba);
    54. }
    55.  
    56. ba = m_engine->readAllStandardError().trimmed();
    57. if (!ba.trimmed().isEmpty()) {
    58. m_log->append(ba);
    59. }
    60. }
    61.  
    62. void readDataOnButtonPress()
    63. {
    64. QString info;
    65.  
    66. info.append("----------------\n");
    67. info.append(QString("bytes available: %1\n").arg(m_engine->bytesAvailable()));
    68. info.append("----------------\n");
    69.  
    70. m_log->append(info);
    71.  
    72. readData();
    73. }
    74.  
    75. void send() {
    76. QString text = m_lineEdit->text().trimmed();
    77. m_lineEdit->clear();
    78.  
    79. if (text.isEmpty())
    80. return;
    81.  
    82. m_engine->write(text.toAscii());
    83. m_log->append(text);
    84. }
    85. };
    86.  
    87. int main(int argc, char** argv)
    88. {
    89. QApplication app(argc, argv);
    90. MainWindow mw;
    91. mw.setWindowTitle(QObject::tr("QProcess test"));
    92. mw.show();
    93. return app.exec();
    94. }
    95.  
    96. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files
    Last edited by momesana; 4th March 2010 at 21:28.

  2. #2
    Join Date
    Jun 2007
    Posts
    5
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Problems communicating with a process (Gnuchess) via QProcess

    try to send not only a command but also a newline.

Similar Threads

  1. QProcess, how to give focus to the new process on Mac
    By simlab in forum Qt Programming
    Replies: 3
    Last Post: 25th January 2010, 00:27
  2. Problems communicating with external editor
    By titaniumdecoy in forum Qt Programming
    Replies: 1
    Last Post: 18th October 2009, 21:12
  3. Connecting with QProcess to an already running process
    By high_flyer in forum Qt Programming
    Replies: 7
    Last Post: 26th November 2007, 11:31
  4. Replies: 4
    Last Post: 4th June 2007, 09:33
  5. Replies: 2
    Last Post: 30th March 2007, 09:10

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.