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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:22:23+00:00 2026-05-30T22:22:23+00:00

I have a QT class instance, called C, (C inherits QOBJECT) that sends a

  • 0

I have a QT class instance, called C, (C inherits QOBJECT) that sends a signal S.
In my program, other QT classes instances X are created and destroyed when the program runs. These other classes connect and disconnect S, i.e. they run:

connect(C,SIGNAL(S()), this, SLOT(my_func()));  // <this> is an instance of X

or

disconnect(C,SIGNAL(S()), this, SLOT(my_func()));

In class C, the calculation of whether S should be emitted (and the data associated to it – not shown here) is rather complicated, so I would like the instance of class C (which emits the signal) to be notified when one(or more) object are connected (listening) to S or when all are disconnected.

I have read about the connectNotify and disconnectNotify functions, but their usage is discouraged. Besides the documentation does not state very clearly if there is a one to one relationship between the number of (dis)connectNotify calls and the number of “listener” to the signal (or can one single connectNotify be called for more than one listener?).

Can I just count positively (count++) the number of connectNotify and negatively (count–) the number of disconnectNotify and just react to non-zero value?

Any better way to do this?

  • 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-30T22:22:25+00:00Added an answer on May 30, 2026 at 10:22 pm

    First, I think you’ve got it right that connectNotify and disconnectNotify can be used for this purpose – each connect event will be counted properly, even if it is a duplicate from the same object.

    You can also double check this with QObject::receivers

    int QObject::receivers ( const char * signal ) const [protected]
    Returns the number of receivers connected to the signal. Since both
    slots and signals can be used as receivers for signals, and the same
    connections can be made many times, the number of receivers is the
    same as the number of connections made from this signal. When calling
    this function, you can use the SIGNAL() macro to pass a specific
    signal: if (receivers(SIGNAL(valueChanged(QByteArray))) > 0) {
    QByteArray data;
    get_the_value(&data); // expensive operation
    emit valueChanged(data); } As the code snippet above illustrates, you can use this function to avoid emitting a signal that
    nobody listens to. Warning: This function violates the object-oriented
    principle of modularity. However, it might be useful when you need to
    perform expensive initialization only if something is connected to a
    signal.

    My suggestion would be to write a simple test program. Override connectNotify and disconnectNotify to increment/decrement a counter, but also use receivers to verify that the counter is correct. Try connecting multiple times, disconnecting multiple times, disconnecting even if there is no connection, etc.

    Something to be careful of: connect and disconnect are thread-safe; I’m not sure if the matching Notify functions are safe also.

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

Sidebar

Related Questions

I have a class called Sprite, and ballSprite is an instance of that class.
I have an instance of a class called AccessData, which inherits from DbContext. So
If I have a class FunctionsClass that inherits QObject and has a QTcpServer and
Assume I have a Model class called Bird and a instance of Bird called
In my MIDLet I have an instance of the java class ImageFetcher called anImg.
I have an app holding an array of instances of a class called SwimmingPool.
I have a class Page that creates an instance of DB , which is
I have a class in which looking up an instance is expensive, so instances
I have written a custom SessionStoreProvider class that inherits from the SessionStateStoreProviderBase. I have
.Net 3.5, using C# I have an instance of a class (A) that has

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.