The Silent Eeer
Original address: http://www.cnblogs.com/hebaichuanyeah/p/6092152.html
Once written in Qt, there is a need to add a custom event to all of the program's interfaces. The event is triggered by a thread.
It is really troublesome to add a single event binding to each interface object.
So I wrote an event's parent interface, binding the thread to an event filter function in an event, and then deciding whether to trigger an event in the event filter, and then invoking an event response function, which is a pure virtual function that is actually implemented in a subclass. And the interface class is inherited from this parent class.
Later, I turned to "design mode" and found that this set of things in fact a "template method" mode.
Intent: Define an operation algorithm skeleton, and put some steps into the subclass implementation.
The implementation of the above event mechanism. The BaseEvent class cannot be instantiated and can only be inherited.
Class Baseevent{public: baseevent () { Binds the thread that triggered the event to the Eventfiler event filter. }protected: virtual void envent () =0; Event filter void Eventfiler () { if (current interface gets focus) { //the event function is actually implemented in subclasses. envent (); Qdebug () << Print current object information. } } }
Another chestnut, in the parent class, has an action function that opens the document, but the document is opened with different algorithms for different formats.
void Baseclass::opendocument (string path) { if (path does not exist) { return; } Document * doc = open (path); Read (doc);}
In the above code wisdom Oh you, the actual open function, and the read function are all pure virtual functions, for different formats of the document, in different subclasses to implement the specific open and read algorithm.
Previous examples of customizing event base classes in Qt.
[design mode]<10>. C + + and template method patterns