[Qt-interest] Inheritance limitations

Scott Sibley sisibley at gmail.com
Sun Oct 18 05:07:39 CEST 2009


I'm curious about something with this code.

    class CustomWidget : public QWidget <http://doc.trolltech.com/qwidget.html>,
                         public ClipboardInterface
    {
        Q_OBJECT

    public:
        CustomWidget(QWidget <http://doc.trolltech.com/qwidget.html>
*parent = 0);

        void cut();
        void copy() const;
        void paste();

    private:
        ClipboardWrapper *wrapper;
    };

 The CustomWidget class "has a" ClipboardWrapper object. Here's the
ClipboardWrapper class definition:

    class ClipboardWrapper : public QObject
<http://doc.trolltech.com/qobject.html>,
                             public ClipboardInterface
    {
        Q_OBJECT

    public:
        ClipboardWrapper(QObject
<http://doc.trolltech.com/qobject.html> *parent)
            : QObject <http://doc.trolltech.com/qobject.html>(parent)
        {
            Q_ASSERT(parent);
            wrappedObject =
                qobject_cast<ClipboardInterface *>(parent);
            Q_ASSERT(wrappedObject);
        }

    public slots:
        void cut() { wrappedObject->cut(); }
        void copy() const { wrappedObject->copy(); }
        void paste() { wrappedObject->paste(); }

    private:
        ClipboardInterface *wrappedObject;
    };

ClipboardWrapper has a ClipboardInterface* named wrappedObject, and it
also subclasses ClipboardInterface. Is this just an artifact in the
example, or is there a real reason for this?
Something tells me there's a reason for it, because CustomWidget also
subclasses ClipboardInterface. Neither occurrence makes clear sense to
me.


On Tue, Oct 13, 2009 at 12:48 AM, Mihail Naydenov <mlists at ymail.com> wrote:

> Hi, you should read this (about qobjects and interface clases).
> http://doc.trolltech.com/qq/qq15-academic.html
>
> MihailNaydenov
>
>
> *From:* Scott Sibley <sisibley at gmail.com>
> *To:* Jason H <scorp1us at yahoo.com>
> *Cc:* qt-interest at trolltech.com
> *Sent:* Tue, October 13, 2009 2:02:04 AM
> *Subject:* Re: [Qt-interest] Inheritance limitations
>
>
> Probably true. I just thought of a solution to my issue though. You see, I
> needed a QObject slot, but a different method for each different class
> deriving A. So the following might do it.
>
> class A: public QObject { // The reason A needs to be a QObject is I have
> some properties for QScript to access.
>     Q_OBJECT
>
>     public slots:
>     void aMethod();
> };
>
> class B: public A {
>     public:
>     void aMethod(); // Class C would provide its own aMethod as well.
> }
>
> Of course I haven't tested this yet. I don't see why it wouldn't work.
>
> On Mon, Oct 12, 2009 at 5:01 PM, Jason H <scorp1us at yahoo.com> wrote:
>
>> If A inherits QObject, why inherit it again?
>>
>> class B: public A {
>>     Q_OBJECT
>> };
>>
>> Is all you should need. Right?
>>
>>
>> ------------------------------
>> *From:* Scott Sibley <sisibley at gmail.com>
>> *To:* qt-interest at trolltech.com
>> *Sent:* Mon, October 12, 2009 5:31:46 PM
>> *Subject:* [Qt-interest] Inheritance limitations
>>
>> I get the following warning with these two classes. I think I'm learning
>> to cope with it (I'm rewriting a pygtk app in C++ and QT, where I just
>> dropped signals wherever I liked), but was curious why there's the
>> limitation.
>>
>> [starlon at localhost tmp]$ moc-qt4 -I/usr/include/QtCore -o moc_test.cctest.h
>> test.h:11: Warning: Class B inherits from two QObject subclasses QObject
>> and A. This is not supported!
>>
>> // test.h
>> class A: public virtual QObject {
>>     Q_OBJECT
>> };
>>
>> class B: public virtual QObject, public A {
>>     Q_OBJECT
>> };
>>
>>
>>
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091017/38a3b3d4/attachment.html 


More information about the Qt-interest-old mailing list