Hey trolls,
Consider the following code:
My onReadyRead() slot never gets called.
Why ?
Printable View
Hey trolls,
Consider the following code:
My onReadyRead() slot never gets called.
Why ?
http://lists.trolltech.com/qt-intere...ad01097-0.html
Quote:
stdin on Windows is a magic little beast that tries to work both like a
windows handle, and as the Posix FILE* handle (e.g., fileno(stdin) returns
something that can be passed to other BSD emulation functions). The problem
is that it is not a pipe. It isn't a socket. It's not a file. It is a
special buffered device that is flushed on endline or EOF, and sometimes it
can get flushed by other activity.
You cannot use QSocketNotifier to receive notifications on stdin, because
Windows doesn't send single object notifications on it. QSocketNotifier on
Windows is designed to work with network sockets, and stdin isn't a socket
per se.
Andreas
Here is the solution:
Code:
#include "qkReceptor.h" // Qt includes #include <qsocketnotifier> #include <qtextstream> #include <qtimer> //============================================================================= // Private //============================================================================= #include "qkReceptor_p.h" qkReceptorPrivate::qkReceptorPrivate(qkReceptor * p) : qkPrivate(p) { } void qkReceptorPrivate::init() { Q_Q(qkReceptor); q->start(); } //============================================================================= // Ctor / dtor //============================================================================= { Q_D(qkReceptor); d->init(); } qkReceptor::~qkReceptor() { } //============================================================================= // Protected QThread reimplementation //============================================================================= void qkReceptor::run() { // A timer to avoid high CPU charge QTimer timer; connect(&timer, SIGNAL(timeout()), this, SLOT(onCheckStdin()), Qt::DirectConnection); timer.start(100); } //============================================================================= // Private slots //============================================================================= void qkReceptor::onCheckStdin() { // Do we have a new line to be read ? if (line != "") { // Do something with our line qkPrint(line.C_STR); } }
If someone finds a proper way on windows, keep us posted.