Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6236659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:53:15+00:00 2026-05-24T10:53:15+00:00

Im making a console application where Im getting user input with std::getline() (in a

  • 0

Im making a console application where Im getting user input
with std::getline() (in a while loop). Im also using QextSerialPort (inherits QIODevice). 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::processEvents() in the while(1){getline()} loop, but found that’s not a good solution. Then, I tried to call

QCoreApplication::processEvents() in a QThread but it didn’t
work.

Here’s the code Im using:

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();
}

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);

       // ...
    }
}

How can I have a getline() (or similar method) without blocking event loop?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:53:16+00:00Added an answer on May 24, 2026 at 10:53 am

    Yes, i got similar experiences with the ProcessEvent() method called from the main thread. The problem is that it’s just put on top of the current call stack and that can have other nasty side effects like endless calling depth or re-entrance problems.

    I would just add a few threads to your application to create a clean solution. Since getLine() belongs to the (console) user interface, I would keep it in the main thread and move the networking code into an extra thread but other solutions are also possible (spawning threads for all kinds of interactions and just maintaining them from the main thread).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using the Rails console a fair bit lately and its making
Hi I am making an ATM console application in C++. I am planning to
I am using LinkedIn-j api for making a Linkedin application.The problem that I am
I am making a simple console application for a home project. Basically, it monitors
Is there a standard way of making a C# console application unit-testable by programming
I am making a console application where I drop files onto the .exe from
I'm making a Java console application that needs to send an HTTP request to
I am making a linux application using C++ and it will print info out
I am making an iphone application which has user information on a web server.
When making changes using SubmitChanges() , LINQ sometimes dies with a ChangeConflictException exception with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.