readyRead() on stdin
-
Is it possible to use the signal
readyRead()
onstdin
?QFile _file; MyClient::MyClient(QObject *parent) : QObject(parent) { _file.open(stdin, QIODevice::ReadOnly); connect(&_file, &QFile::readyRead, this, &MyClient::readyRead); } void MyClient::readyRead() { qDebug() << "<<" << _file.readLine(); }
But when I type something on stdin and press enter, nothing happens.
I tried other ways but none worked, example:https://github.com/juangburgos/QConsoleListener
returns the warning "QSocketNotifier: Can only be used with threads started with QThread" and doesn't receive anything.
-
Is it possible to use the signal
readyRead()
onstdin
?QFile _file; MyClient::MyClient(QObject *parent) : QObject(parent) { _file.open(stdin, QIODevice::ReadOnly); connect(&_file, &QFile::readyRead, this, &MyClient::readyRead); } void MyClient::readyRead() { qDebug() << "<<" << _file.readLine(); }
But when I type something on stdin and press enter, nothing happens.
I tried other ways but none worked, example:https://github.com/juangburgos/QConsoleListener
returns the warning "QSocketNotifier: Can only be used with threads started with QThread" and doesn't receive anything.
@Mark81
Start by:-
Check the return result of your
_file.open()
, always, and certainly if you having problems. -
Look at passing some extra flags for
QIODevice::OpenMode mode
, tryQIODevice::Text
and/orQIODevice::Unbuffered
. -
Try using the
bool QFile::open(int fd, ...)
instead, withfd == 0
, does that make any difference? -
Do you need to close
stdin
/0
if they are already open for this Qtopen()
to work? -
What kind of application is your code, GUI or console-only?
Please always state what platform you are on for a question of this nature.
returns the warning "QSocketNotifier: Can only be used with threads started with QThread"
So are you using threading anywhere in your code/the code tested or not?
-
-
@Mark81
Start by:-
Check the return result of your
_file.open()
, always, and certainly if you having problems. -
Look at passing some extra flags for
QIODevice::OpenMode mode
, tryQIODevice::Text
and/orQIODevice::Unbuffered
. -
Try using the
bool QFile::open(int fd, ...)
instead, withfd == 0
, does that make any difference? -
Do you need to close
stdin
/0
if they are already open for this Qtopen()
to work? -
What kind of application is your code, GUI or console-only?
Please always state what platform you are on for a question of this nature.
returns the warning "QSocketNotifier: Can only be used with threads started with QThread"
So are you using threading anywhere in your code/the code tested or not?
@JonB said in readyRead() on stdin:
- Check the return result of your
_file.open()
, always, and certainly if you having problems.
Unfortunately it returns
true
.- Look at passing some extra flags for
QIODevice::OpenMode mode
, tryQIODevice::Text
&QIODevice::Unbuffered
.
I guess you're suggesting to use
|
instead of&
. Anyway, nothing has changed.Please always state what platform you are on for a question of this nature.
It's an embedded Linux system with Qt5.11:
# uname -a Linux MarkSys 4.19.49 #1 SMP PREEMPT Sun Jun 9 07:17:25 UTC 2019 armv7l armv7l armv7l GNU/Linux
So are you using threading anywhere in your code/the code tested or not?
I created a console test application. I added the source code (h, cpp files) of the example as a class and declared it into main:
I don't use threads in my code. The only one should be the one needed to useQSocketNotifier
but the warning comes before it has moved to the new thread:QThread _thread; _notifier = new QSocketNotifier(fileno(stdin), QSocketNotifier::Read); // <-- here the warning is printed _notifier->moveToThread(&_thread);
-
-
Hi,
If you want to use QSocketNotifier in a different thread, then you need to create it in the context of that thread. You can't use moveToThread for that.
-
Hi,
If you want to use QSocketNotifier in a different thread, then you need to create it in the context of that thread. You can't use moveToThread for that.
@SGaist said in readyRead() on stdin:
If you want to use QSocketNotifier in a different thread, then you need to create it in the context of that thread. You can't use moveToThread for that.
So, how the code is supposed to work?
The author says it works! -
Author ? Which one ?
-