I created a Qt form that handles the close event. In my other class (Test class), I create an instance of this form and would like to handle the close event of the form from the "Test" class.
I tried using the following code in the "Test" class, but does not seem to work .
connect(instanceWidget, SIGNAL(closeEvent(QCloseEvent *)), this, SLOT(handleFormCloseEvent(QCloseEvent *)));
Note instanceWidget is the instance of the Qt form from test class.
The event handler methods, like closeEvent, are not signals, so you can't connect to them.
The event processing goes like this:
The event loop gets an event for given object from its queue.
It notifies the object by calling object->event(...).
This method is reimplemented in QWidget and calls relevant specific event handler. In this case, it calls closeEvent().
The signals/slots are not involved.
If you want to handle events of other objects (a QWidget is a QObject), you must install an event filter on them. Your event filter will react to the event you care about, while passing onwards other events.
My solution -
Handle OnClose event in the form/UI class and inject the following code -
emit OnClose();
event->accept;
Add Signal OnClose();
In the instantiated (test class) , create a signal and slot for OnClose and provide custom funcationality.
-- Closing form , calls UI's close event handler, which emits onclose signal and gets handled by test class. after which it returns to form class and closes the form.
Related
I am new to Qt and understands the concept of signal and slots. However I am not able to implement it.
My objective is:
Form1 has a button Config. So when I click Config it should open another form Form2( without closing Form1) and send a string strData to Form2.
In Form2 I set some value in the string strData. Then I click Ok button in Form2, Form2 should close and return back the string to Form1.
When the call returns back to Form1, it should continue from where it emitted the signal to invoke Form2.
Any help is highly appreciated.
You can't do this using signals/slots; the signal is emitted, and all of the connected slots are executed, and then the code continues from where the signal is emitted and eventually returns to the event loop. That's when your second form is actually shown and the user can respond to it, but by then, your code is long past where the signal was emitted.
What I believe you're looking for is the QDialog::exec method; use it in place of the signal. The basic pattern of the code is:
// This is the response to click on Config...
Form2Dialog form2;
form2.setSomeStringValue (some_value);
if (form2.exec() == QDialog::Accepted)
{
QString some_new_value = form2.newValue();
}
The Form2Dialog is a subclass of QDialog where you've added your own setSomeStringValue and newValue methods. (What you actually name these is up to you.)
The important thing is that the exec method blocks and doesn't return until the user selects OK or Cancel on the dialog, or closes it using the "close" button in the title bar (if there is one).
I'm experiencing a strange issue with WatchOS (but I suppose that this problem is similar with iOS and OSX).
I'm using a singleton to handle a WCSession delegate (The full code is by NatashaTheRobot, I paste here only a portion of her code, the full code is here ).
This class has a startSession function where the singleton is associated as delegate of the session:
func startSession() {
session?.delegate = self
session?.activateSession()
}
and all the delegate functions are defined inside the same class, like session:didReceiveMessage:replyHandler:
I'd like to be able to have the delegate called every time that the Watch app receives a message independently by the current InterfaceController.
I thought that a good place to achieve this goal might be the ExtensionDelegate class:
class ExtensionDelegate: NSObject, WKExtensionDelegate {
let session = WatchSessionManager.sharedManager // THE SINGLETON INSTANCE
func applicationDidFinishLaunching() {
session.startSession()
}
it seems that this code is not working and the delegate function are never called.
Then I decided to go for a less generic way and I started adding the reference to the singleton instance inside all the InterfaceController... but again it doesn't work and delegate methods are never been called.
Then, in my last attempt, I've implemented the session delegate protocol directly inside the InterfaceController code. In that case I receive the messages from the iOS app... it was working correctly (obviously only when the watch app is presenting that specific InterfaceController).
My question are: why implementing a generic singleton object doesn't work? Why I have to implement the delegate directly on the InterfaceController to make it work?
Try moving the startSession call from the ExtensionController's applicationDidFinishLaunching to its init method. The init gets called no matter which context (complication, app, glance, notification, etc) the extension is being loaded for.
I'm implementing a module using nesC for TinyOS. My modules uses interface Timer<> so I have to implement the event fired of the interface Timer, it is possible to return a value inside this implementation or an event must be always void?
The return type of the Timer.fired event is defined as void and this cannot be changed. Even if the type was non-void, the returned value would be delivered to the component that signals the event, which is some system component implementing the Timer interface.
In order to get some hints on how to solve your problem, please provide more details explaining for what purpose you want to return a value from the fired event, that is, who is expected to get and process the returned value.
I have a Command that executes a service call. In the result handler, I am doing some logic based off the result data. If the logic meets specific criteria, I am displaying a confirmation popup. If the user clicks the continue button in the confirmation popup, I have a method that gets called, which dispatches a Parsley event. That Parsley event is not being caught. However, if I dispatch the Parsley event from inside the result method, it is being caught. Any idea why the event is not being caught when dispatching it from outside the result method?
For example...
[MessageDispatcher]
[Bindable]
public var dispatcher:Function;
I execute some service call from inside the command:
public function execute(event:SomeEvent):AsyncToken
{
return service.callService(event.type, false);
}
I now have a result handler like this:
public function result(data:Object):void
{
if (add some logic here based off data)
AlertHelper.showContinueQuestion(onSelection, "Are you sure you want to continue?");
}
If the user clicks the Continue button on the confirmation popup, it calls the onSelection method:
private function onSelection():void
{
dispatcher(new SomeEvent(SomeEvent.UPLOAD));
}
That Parsley event, SomeEvent, is not being caught. However, if I dispatch that event after the if statement, it is being caught and works fine. Any idea why it is not being caught when dispatched from outside of the result handler? I tried in other commands too, and it does the same thing.
Found this on the Spicefactory site, works as designed. I ended up updating a flag in the Model, versus dispatching an event. I then have a BindSetter listening for changes to that flag in the model. When the flag is set to true, the Parsley event is dispatched.
Command Object Lifecycle
Apart from grouping the executing method and the result handlers the DynamicCommand also introduces a special kind of lifecycle management for command objects. The creation of the object does not happen until a matching message is dispatched. It then becomes a container managed object just for the duration of the command execution. It will immediately be removed from the Context after the result or error handler have been invoked. But during its lifetime, it is a fully managed object, can have its dependencies injected, or even take part in messaging during the command execution. But that would be a rather rare case, most common usage scenario is probably the command object just receiving all the dependencies it needs to execute the command.
I've just the documentation on the Qt event system and the QEvent class. I'm interested in the behavior of the QObject::event() method. The documentation states:
This virtual function receives events to an object and should return true if the event e was recognized and processed.
What is the expected behavior when false is returned from the event() method? What else is attempted in order to handle the event? Is the event automatically forwarded to the parent object?
Note: I know the source is available, and I do have a copy. I'm ideally looking for some piece of documentation addressing this behavior.
I believe the best practice is to explicitly forward the events to the base-class event method if you do not wish to filter that event type (e.g. return QObject::event(event);) since the event function delegates events to specific handlers (e.g. QWidget::keyPressEvent).
QCoreApplication::notify propogates events based on the return value. On true, it considers the event as consumed and stops. Otherwise, the event is passed to the object's parent. For more information, see Events and Filters and Another Look at Events.
Some Events can be propagated.Event will be propagated to it's parent and it's parent recursively until it is processed. Take a look at this:https://doc.qt.io/archives/qq/qq11-events.html