Qt-interest Archive, June 2007
Re: Reacting on stdin-Input in a console application
Message 1 in thread
Andreas Aardal Hanssen wrote:
> QSocketNotifier on Windows is designed to work with network sockets,
> and stdin isn't a socket per se.
A more appropriate solution would be to use QFile and the readyRead
signal (as Malte indicated in the original post). Unfortunately, QFile
doesn't seem to emit readyRead on any platform, AFAICT.
Andreas Aardal Hanssen wrote:
> You cannot use QSocketNotifier to receive notifications on stdin,
> because Windows doesn't send single object notifications on it.
Malte Witt wrote:
> Does anyone have a solution for this problem? Is there a way to use
> SIGNAL/SLOT mechanism to asychronously react on stdin-input?
You can get single object notifications on stdin on Windows, but you'll
have to modify your local copy of qeventdispatcher_win.cpp. The modified
code would look something like this:
(in QEventDispatcherWin32::processEvents):
...
pHandles[nCount] = (HANDLE)_get_osfhandle(fileno(stdin));
waitRet = MsgWaitForMultipleObjectsEx(nCount+1, pHandles, 0,
QS_ALLINPUT, MWMO_ALERTABLE);
if ((haveMessage = (waitRet == WAIT_OBJECT_0 + nCount+1))) {
// a new message has arrived, process it
continue;
}
}
if (haveMessage) {
...
} else if (waitRet >= WAIT_OBJECT_0 &&
waitRet < WAIT_OBJECT_0 + nCount + 1) {
if (waitRet == WAIT_OBJECT_0 + nCount)
{
if (_kbhit())
{
/* emit signal here */
}
else break;
}
}
...
and similarly for the second call to MsgWaitForMultipleObjectsEx further
down in in QEventDispatcherWin32::processEvents.
--Michiel.
Michiel de Hoon
Center for Computational Biology and Bioinformatics
Columbia University
1150 St Nicholas Avenue
New York, NY 10032
--
[ signature omitted ]