6

I looked through the related questions and couldn't find anything that addresses exactly what I was talking about, so let me describe.

I have a class, let's say foo that needs to have its own slots and signals, but also needs to inherit from QXmlDefaultHandler (sounds rather odd, but I encountered this situation when trying to use QHttp to read a website directly into a QBuffer).

class foo: public QXmlDefaultHandler, public QObject
{
    public:
        foo();
        ~foo();

       Q_OBJECT
   public slots:
       void bar();
}

This code, if accompanied by a cpp to connect bar to a signal somewhere else, will not compile. You'll get errors about some members of QObject not being a member of QXmlDefaultHandler. Additionally, you can't move Q_OBJECT or else you get vtable errors for not implementing some things (go on! try it!).

please see my answer for the (very simple) fix. I will entertain voting you as the accepted answer if I think you explain it better than I do.

edit: for you c++ and Qt vets, please post an answer if you can explain it better. i spent quite a bit of time looking this info up, so please help someone else if you can do better than me.

4

2 に答える 2

12

mocのドキュメントには、多重継承の場合、QObjectを提供するクラスが最初に表示される必要があると記載されています

多重継承を使用している場合、mocは、最初に継承されたクラスがQObjectのサブクラスであると想定します。また、最初に継承されたクラスのみがQObjectであることを確認してください。

 // correct
 class SomeClass : public QObject, public OtherClass
 {
     ...
 };

QObjectによる仮想継承はサポートされていません。

于 2009-09-28T20:50:20.187 に答える
2
class foo: public QObject, public QXmlDefaultHandler
{
    public:
        foo();
        ~foo();
   Q_OBJECT
   public slots:
       void bar();
}

As simple as it sounds, if you don't put QObject first in the inheritance list, this task is impossible. It's a limitation in Qt's meta object system. If you don't do this, the compiler will try to apply some members of QObject as part of QXmlDefaultHandler.

于 2009-09-28T20:42:26.293 に答える