What is the actual use of connectNotify and bool QApplication::notify ?



  • Can any one explain me with example what is the use of void QObject::connectNotify ( const char * signal ) and
    bool QApplication::notify ( QObject * receiver, QEvent * e );


  • Moderators

    Have you tried the documentation already? I find the description of both methods to be pretty good (if short).



  • ya i have read the documentation but i didn't get clear idea. Can you give me complete example.



  • QObject:connectNotify is invoked when you perform a connect on an object, in other words is a method invoked when you connect a signal to your object, so that your object can perform some lazy initialization. For instance, imagine you want to connect a signal emitted from an editor to your object, but you have to handle data changes and propagate them back to a relation database. You can open a database connection as soon as the application starts, or you can connect to the database only when the signal connection happens, that means that if the user never "opens" the editor you will never waste resources to open a connection that you will never use. This is a stupid example, nothing more complete come into my mind.

    The QApplication::notify method simply dispatches an event (e.g., mouse pressed) to the receiver object.

    The documentation is pretty clear in my opinion.


  • Moderators

    Do you have specific questions about those methods that the documentation should answer?



  • can we notify about button clicked event to parent window before mouse press event is called using this?



  • No, that is not possible. The button click is actually generated based on mouse events (a mouse press + a mouse release).



  • ok then can we notify only for press button before mouse press event is called?



  • You can install an event filter on the button. It will be called before the button receives the event, and you can block the event from reaching the button if you want. Look into QObject::installEventFilter and QObject::eventFilter for more details.



  • Replying "The documentation is pretty clear in my opinion" to someone who is already demonstrating that the documentation is NOT clear, is a useless, if not subtly offending comment to the original poster.

    To me, this is clear as mud.

    I would really appreciate if someone is able to provide an hones answer that actually has the intention to help, not to point out the obvious statement that the question implies ignorance.

    I too do not understand how this method works and yes, I have read what the documentation says and it is NOT clear to me: How do I call it? what is it notifying? what does this returns? how can I use it at all? could anybody show a simple (please, simple) example showing how it is called and used?

    Please don't bother to reply at all if your reply is going to be like fluca1978's. I do not need to be told that the documentation is perfect. It "obviously" is. Thanks a lot in advance !



  • Fair point, it may not be as clear as it could be.

    I would still appreciate if we could keep the tone a bit more friendly here. Frustration about something difficult is one thing, and I understand that, telling off people who try to be helpful, even if they aren't, is another. I don't like it.



  • Thanks Alexandra,
    Point taken.
    Still, I would really appreciate some help on the topic.
    Thanks again



  • First of all, QObject::connectNotify and QCoreApplication::notify are unrelated.

    QObject::connectNotify is a message that is called if a slot is connected to one of this Objects' signals. Normally, you should not care about this, as the whole idea of signals and slots is that the object sending the signal does not care what happens to it. However, if calculating the value you want to send out is expensive, for instance, then it can be handy to know that someone is listening. Again, you normally would not use this method.

    QCoreApplication::notify is part of the eventhanding system. Notice that events in Qt are a different, independent communication mechanism from signals and slots. As documented in the QCoreApplication documentation, notify sends a QEvent to a specific receiver. You can reimplement this message to, for instance, insert custom handling of specific events before they are send to the indented receiver, or stop them from arriving at all. It is a core part of the Qt event handling system, and if you're not careful, you may screw it up completely. Normally, you would not use this one either. I have never used this method; the event filter strategy always was enough for my uses.



  • [quote author="tuvix" date="1327678861"]
    Please don't bother to reply at all if your reply is going to be like fluca1978's. I do not need to be told that the documentation is perfect. It "obviously" is. Thanks a lot in advance !
    [/quote]

    fluca already gave an example on a possible use case. I don't see why you focus on the "The documentation is pretty clear in my opinion" here, as this was clearly not the main message in his post. Asking for "I would really appreciate some help on the topic" could be regarded as equally unspecific as the supposed-to-be offending answer. What else than a quite general and unspecific answer do you expect?

    So, do you have some specific questions regarding the two methods? Some use case in your application that doesn't work or where you want to know if that methods could be of use?



  • Thank you Andre, very interesting observations. It seems that it is easier to see why we should not use it than when or how should we use it.
    I have difficulty to understand how do I "see" or "capture" or "read" this message that is called if a slot is connected to one of this Objects' signals.
    void QObject::connectNotify ( const char * signal )
    so this function does not return anything, but still, how do you use it? If I could read a boolean or anything, I would understand. The documentation just says "this virtual function is called when..." but it doesn't say what does it actually do.

    Say, I want to be notified whenever something has been connected to signal. So connectNotify sends a message. But how do I get/read/know this message? Can you show a simple example of its usage?

    thanks a lot for your help and patience



  • [quote author="tuvix" date="1327683013"]The documentation just says "this virtual function is called when..." but it doesn't say what does it actually do.
    [/quote]

    Nothing :-)
    Really nothing, the method body in QObject sources is empty. It is just provided for the users of Qt (i.e. you and me etc.) as a hook where you can plug in for your own purpose. It is really, really seldom used. Even in the Qt sources there are only a handful of occurrences. I personally never came across a need of it within 10 years of using Qt.

    [quote author="tuvix" date="1327683013"]
    Say, I want to be notified whenever something has been connected to signal. So connectNotify sends a message. But how do I get/read/know this message? Can you show a simple example of its usage?
    [/quote]

    connectNotify doesn't send a message (in the sense of signal/slot connections that transfer messages in the arguments). It is just an ordinary virtual method that is called on your object. The parameter is a the signature of a signal of the class where this method is called. That is, your class object now knows from that parameter which of its signals has been connected to a slot somewhere else. You will neither know which slot nor wich class or object it has been connected.



  • Thank you very much, Volker.

    I can see that this method is probably not what I am looking for, and specially because it is made clear that it is a really very seldom used one.

    You have made me really curious about how would I implement such "hook" where I could plug in, by calling this on my object.... I will also probably have to keep digging into it a bit further. Perhaps, again, if and when I could see an example of the concept, but I perfectly understand now that being something rather esoteric, examples are not abundant.

    I will just probably avoid this method completely for now.

    Once again, thank you very much for your prompt, kind and interesting explanations !



  • There are methods in the API that are intended to be called by you as a programmer, and there are methods that are intended to reimplemented by you as a programmer, and that are then called by the framework. Most methods fall in the first category, and are often easiest to use. The second category is most often intended for more advanced usage. You can recognize them by the keyword "virtual".

    The methods we're discussing here both fall in the second category: advanced functionality that you use via reimplementation, not by calling any methods yourself.



  • Thanks Andre.



  • Maybe a short example will make it more clear:

    @
    QLineEdit *lineEdit = new QLineEdit(this);
    QTextEdit *textEdit = new QTextEdit(this);

    connect(lineEdit, SIGNAL(textChanged(QString)), textEdit(SLOT(setPlainText(QString)));
    // the connect statement is processed and eventually
    // the following line is called:

    lineEdit->connectNotify(SIGNAL(textChanged(QString)));
    @

    Nothing happens here, of course, as that method is not reimplemented in QLineEdit. If you would have used your own class instead of a QLineEdit and if you had reimplemented connectNotify() in that class, you could do something meaningful in your class. I really don't see much use for it though. Some delayed initialization for an otherwise unused heavy weighted resources could be one, as outlined bei fluca.



  • Ok, this is very useful example.
    This does help to make it clear. Thank you for the valuable help. I think I see the idea now.



  • I had the same question when I read this.

    I understand connectNotify() and disconnectNotify () as below.

    MyClass.h

    'void connectNotify(const char *signal);'

    MyClass.cpp

    'void MyClass::connectNotify(const char *signal)
    {
    //find the signal connected as in documentation
    if (signal == QMetaMethod::fromSignal(&MyObject::valueChanged)) {
    // signal is valueChanged
    }
    }'

    Is this correct?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.