Hello,
Im making a console application where Im getting user input with std::getline() (in a while loop). Im also using QextSerialPort (inherits QIODevice) and readyRead() signal. Thus I need an event loop so I'm using QCoreApplication and calling a.exec().
The problem is getline() blocks event loop and, while it's blocked, I can't receive anything from serial port. I tried to use QCoreApplication:rocessEvents() in the while(1){getline()} loop, but found that's not a good solution. Then, I tried to call QCoreApplication:rocessEvents() in a QThread but it didn't work.
Here's the code Im using:
int main(int argc, char *argv[])
{
Base *base = new Base();
Commander *commander = new Commander(base);
QObject::connect(commander,
SIGNAL(end
()),
&a,
SLOT(quit
()));
QTimer::singleShot(0, commander,
SLOT(run
()));
return a.exec();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Base *base = new Base();
Commander *commander = new Commander(base);
QObject::connect(commander, SIGNAL(end()), &a, SLOT(quit()));
QTimer::singleShot(0, commander, SLOT(run()));
return a.exec();
}
To copy to clipboard, switch view to plain text mode
void Commander::askToConnect()
{
if(base->connect() == false){
cout << "Failed to open port." << endl;
}
}
void Commander::run{
string inputline;
askToConnect();
while(1){
cout << endl << "> ";
getline(cin, inputline);
// ...
}
}
void Commander::askToConnect()
{
if(base->connect() == false){
cout << "Failed to open port." << endl;
}
}
void Commander::run{
string inputline;
askToConnect();
while(1){
QCoreApplication::processEvents(); // ???
cout << endl << "> ";
getline(cin, inputline);
// ...
}
}
To copy to clipboard, switch view to plain text mode
Even if I have while(1); the event loop is blocked and I can't read data from serial port.
So, how can I get user input without blocking event loop?
Thanks!
Bookmarks