I have multiple Slots and I have lambda function.
Each time when Lambda function gets called but how to identify that from which slot that is being called .
you can get the slot details from the event object that comes to the lambda function.
see the request object and its details - SLOT_NAME.
Related
I have the following function that is called each time a user click a button :
void Foo::onCommand1Clicked(int index)
{
connect(this, &Foo::authorized, this, [=]() {
// avoid multiple connections
QObject::disconnect(this, &Foo::authorized, this, nullptr);
// do work based on "index" captured by value ( or other parameters in real code )
this->processCommand1(index);
// ..
}
});
}
Now, if the command is "authorized" ( the signal is asynchronously emitted but may also not be emitted at all), then the lambda containing the command logic is executed.
Moreover, while the command is pending for authorization, the button is disabled ( preventing the function to be called )
My question is about the lambda connected to the signal and especially its parameters captured by value : Are those parameters eventually released from memory or do they accumulate into the memory stack each time the connection is done (ie the button is clicked) ?
More generally, is there any kind of "memory leak" or "continuously growing stack" in this code ?
Thank you.
for lambda connections in Qt third parameter called context is used. In your case is this pointer. So, while this is alive, connection is active. When you create a lambda connection, lambda object is moved as a whole object to connection manager. When you disconnect from your signal, lambda object should be dead (as i suggest). Maybe it's deleted later. So I reccomend to insert disconnect as last instruction in lambda. However, this is still alive.
A also recommend you to use another mechanism to call async task, than connecting/disconnecting to a signal in the same object!
A thread, for instance. Or to call QTimer::singleShot. I don't see in what circumstances authorized signal is emitted
I have a QObject class processing requests. So I could create a SLOT process(QString). I would like to know when the request was processed - i.e. receive a future of some kind that I can wait for. It should be possible to relate reults to corresponding requests.
But since the SLOT can't return a value, I'm a bit stuck... Can this be achieved with the Qt SIGNAL/SLOT mechanism?
Your slot can emit a signal when it is done (with any data you need as arguments) that you can then connect to, to do other stuff at that point.
I've stumbled across a problem I can't solve on an elegant way right now.
The situation: I have a callback function which is called from outside my application. The callback function has to update some gui object.. Since I can't call (for example) repaint() from within another thread, I have to finde some way to add a function call to the main event loop so the task gets executed at some time.
One possible way would be to use this:
QMetaObject::invokeMethod(object, "functionName", Qt::QueuedConnection, Q_ARG(float, value));
However, this just gives me the response that no such Method "Object::functionName". (which is obviously a lie!)
I've also read about connecting a signal to a slot which will be called from the event loop by setting the connection type to Qt::QueuedConnection. However, using QOjbect.:connect() won't work since I don't knwo which object the signal needs to get. Nice would be something like
QObject::emit(object, SIGNAL(function(flaot)), arg);
QMetaObject::invokeMethod is usually what you should use in this kind of situation. Make sure that:
object is a QObject subclass with the Q_OBJECT macro at the top
functionName is either declared in the slots section or has the Q_INVOKABLE macro
I need event filter only for some time, is there a way of uninstalling it later on?
Please read about how the event system works in Qt here. This is crucial for the basic understanding, particularly this paragraph:
The QObject::installEventFilter() function enables this by setting up an event filter, causing a nominated filter object to receive the events for a target object in its QObject::eventFilter() function. An event filter gets to process events before the target object does, allowing it to inspect and discard the events as required. An existing event filter can be removed using the QObject::removeEventFilter() function.
Having that read, you can see that there is a counter-part for installEventFilter, not surprisingly, it is called removeEventFilter. Here is the Qt 5 documentation to it:
void QObject::removeEventFilter(QObject * obj)
Removes an event filter object obj from this object. The request is ignored if such an event filter has not been installed.
All event filters for this object are automatically removed when this object is destroyed.
It is always safe to remove an event filter, even during event filter activation (i.e. from the eventFilter() function).
Yes there is. It's a function called QObject::removeEventFilter.
From Qt Docu:
void QObject::removeEventFilter ( QObject * obj )
Removes an event filter object obj from this object. The request is ignored if such an event filter has not been installed.
All event filters for this object are automatically removed when this object is destroyed.
It is always safe to remove an event filter, even during event filter activation (i.e. from the eventFilter() function).
I would like to implement the observer pattern similar to Timer component. Instead of calling a callback by time expiration, the callbacks that are observers of a topic would be called from system events (like new file created, or a new e-mail received, etc.). I tried using nsIObserverService in the component XPCOM, but it seems that functions from component aren't able to call observers in JavaScript by using NotifyObservers. The NotifyObservers only works when is called from JavaScript.
Thanks in advance
Example::Example always runs on the main thread (because it's being created by your script). So it never creates a proxy to the observer service. But the call to Example::Call from Ex::Run happens on the background thread, and I think in this case the call to NotifyObservers returns NS_ERROR_UNEXPECTED (which you ignore).