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 377395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:41:32+00:00 2026-05-12T14:41:32+00:00

[related to this question ] I wrote this piece of code to understand how

  • 0

[related to this question]

I wrote this piece of code to understand how qt signals and slots work. I need someone to explain the behaviour, and to tell me if I’m right about my own conclusions.

My program:

connectionhandler.h

#ifndef CONNECTIONHANDLER_H
#define CONNECTIONHANDLER_H

#include <QTcpServer>
class ConnectionHandler : public QObject
{
    Q_OBJECT
public:
    ConnectionHandler();
public slots:
    void newConn();
private:
    QTcpServer *server;
};

#endif // CONNECTIONHANDLER_H

connectionhandler.cpp

#include "connectionhandler.h"
#include <QTextStream>

ConnectionHandler::ConnectionHandler() {
    server = new QTcpServer;
    server->listen(QHostAddress::LocalHost, 8080);
    QObject::connect(server, SIGNAL(newConnection()),this, SLOT(newConn()));
}
void ConnectionHandler::newConn() {
    QTextStream out(stdout);
    out << "new kanneksan!\n";
    out.flush();
}

main.cpp

#include <QCoreApplication>
#include "connectionhandler.h"

int main(int argc, char* argv[]) {
    QCoreApplication app(argc,argv);
    ConnectionHandler handler;
    return app.exec();
}

Now, running this program sends it into an infinite loop looking for new connections.

Observation: if I don’t call app.exec(), the program returns immediately (as it should).
Question: why?

Question: if I had connected the slot as a queued connection, when would the slot invocation be performed?
Question: if app.exec() is an infinite loop of sorts, how does the newConnection() signal ever get emitted?

Big Question: Is their any "second thread" involved here? (I expect a no, and a stunningly elegant explanation 🙂 )

Thanks,
jrh

PS: who else has this nested parenthesis syndrome? like "(.. :))" or "(.. (..))"?

  • 1 1 Answer
  • 1 View
  • 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-12T14:41:32+00:00Added an answer on May 12, 2026 at 2:41 pm

    If you don’t call app.exec() then the program hits the end of your main() and ends. (Why? There’s no more code to execute!)

    app.exec() is an infinite loop of the following style:

    do
    {
      get event from system
      handle event
    }
    while (true);
    

    If you use a queued connection, then the event is added to your event queue, and it will be performed at some point in the future during the app.exec() loop.

    There is no second thread in your program. Events are delivered asynchronously by the OS, which is why it appears that there’s something else going on. There is, but not in your program.

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

Sidebar

Related Questions

My Question is related to this as I solved the problem, I wrote my
This question is related to another question I wrote: Trouble using DOTNET from PHP.
Related to this question , is the idea of a default servlet that serves
Note: this question is related to this one , but two years is a
This question is related to this , hopefully better phrased. I would like to
This question is related to this one: Best way to store product colors in
This is related to this question I asked earlier about syntax highlighting user-defined blocks
Somewhat related to this question , but in the absence of any answer about
My question is related to this question: Baseline snaplines in custom Winforms controls However,
I read all topics related to this question in stackoverflow and whole internet and

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.