[Qt-interest] Q_DECLARE_METATYPE and abstract classes
Schimkowitsch Robert
Robert.Schimkowitsch at andritz.com
Mon Aug 8 12:36:16 CEST 2011
> How do I declare a metatype of an abstract base class? I have seen
> nothing in the Q_DECLARE_METATYPE-docs that would forbid it.
>
> I need the metatype to create a Q_PROPERTY of (a pointer to) that
> abstract type. The compiler errors are clear enough: It wants to
> create a new object of that type, which doesn't work with an abstract
> class.
>
> How can I work around this? Of course, I could make a dummy
> implementation in the abstract base class, but would the polymorphism
> mechanisms even work correctly when passing my objects to and from
> QVariants?
>
>
>
> The longer story: What do I actually want to achieve?
>
> I have found a very elegant solution to the problem: How to create
> (graphics) widgets that can handle events differently in different
> modes or contexts, without needing to know about the modes or
> contexts.
>
> The idea is that the graphics widget always draws the same way, but
> will respond differently on various events, e.g. mouse clicks.
>
> There are two use cases I am trying to address:
> 1) The same graphics widget is used on a touch device and on a PC with
> mouse. I need to respond to the events differently to achieve the same
> results.
> 2) The same graphics widget can be in different "modes", and act
> differently in those modes. An example would be a painting surface
> that can be in "pan", "zoom in" or "zoom out" mode. A click would
> cause a different course of action depending on mode.
>
> To make this all easily extensible, I have created an event handler
> base class, and a concrete event handler class for each specific mode
> or context. The event handler knows its "buddy" graphics widget via
> pointer, so it can act on the widget if need be.
>
> To switch the mode, I simply set a different concrete event handler on
> the graphics widget. The widget forwards all calls to "event(QEvent*)"
> to the event handler, if one is present.
>
> And now the final trick: I intend to do the "mode switches" via State
> machine.
> But for that, I need to be able to assign the concrete event handler
> pointer via Q_PROPERTY (or is there another way)?
>
>
> This is my abstract event class, which must be implemented by all
> concrete event handlers:
>
> template<class buddyT>
> class CGlib_AbstractGraphicsEventHandler
> {
> public:
> CGlib_AbstractGraphicsEventHandler()
> : m_Buddy(0)
> {
> }
> virtual ~CGlib_AbstractGraphicsEventHandler()
> {
> }
>
> virtual const buddyT* buddy()
> {
> return m_Buddy;
> }
>
> virtual void setBuddy(buddyT* newBuddy)
> {
> m_Buddy = newBuddy;
> }
>
> virtual bool performEvent(QEvent* event) = 0;
> private:
> buddyT* m_Buddy;
>
>
> Ideas?
>
>
> Kind regards
>
> Robert Schimkowitsch
#####################################################################################
This message and any attachments are solely for the use of the intended recipients. They may contain privileged and/or confidential information or other information protected from disclosure. If you are not an intended recipient, you are hereby notified that you received this email in error and that any review, dissemination, distribution or copying of this email and any attachment is strictly prohibited. If you have received this email in error, please contact the sender and delete the message and any attachment from your system.
Thank You.
#####################################################################################
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110808/e37ef779/attachment.html
More information about the Qt-interest-old
mailing list