I have interface some> strong> signals in the Qt5 program.
The implementation of this interface is instituted on the start and the signal is connected to different parts of the program (multiple locations).
Now I want to delete and create a new instance for example, probably by another implementation, and somehow keep the signal connection In order to get all the places it is not necessary to pay attention to the signs that the implementation has changed.
Is there any way to do this in any way or do I have to change the structure of the program in order to control all the signal connections at one place)?
Example:
// PS: be considered as the best pseudocode, because many QT boilerplate // and error handling code have been omitted, and Many bugs are left in :-) struct MyInterface {Virtual Zero doSt uff () 0 =; Signal: Some zero () zero; } Struct MyImpX: Public MyInterface {void doStuff () {qDebug () & lt; & Lt; "MyImpX"; If ((random ()% 100) <5) {leave some signals (); Microfin: the public myfife {zero} (qDebug () inst = inst;} zero doStuff () {if (0! = Inst) {inst-> doStuff ();}} }} Microcosmor {Public Slots: Zero Handel () {qDebug () << "Handling Signals";}} Zero Main () {Q Application Application; MyInterface * inst = New MyImpX (); MyWorker con (inst); My Consumer Eye, J, K, L; // All additions in this example are in one place, and // In reality, they are called from many places, which is difficult to control // connect (Inst, sign (someSignal ()), & amp; i, slot (handleIt ()); connect (Inst, signal (someSignal ), & Amp; j, slot (handleIt ()); connect (Inst, sign (someSignal ()), & amp; K, slot (handleIt ()); Connect (Inst, sign (someSignal ()), & amp; L, slot (handleIt ()); // [... at this point some time passes where the signal is working] // Now these examples change, so the old connections are lost Con.setNewInstance (New MyImpY ()); Delete; Inst = 0; // [... at this point some time passes where the signal is not working] app.exec (); }
You can try to apply it on some basis, but I think That's the best at the hackney.
Instead, you may have a proxy object that can not be changed, and that can change its connection when changing the actual object. For this, you should probably use a signal-signal connection, however you can also write slots that emit signals. There is a pseudocode in the question, so there are some pseudocodes to display in theory as well.
Of course you can remove the oldObj
parameter, and for example store the currently connected object as a private variable,
And then your main
will start something like this: