[Qt-interest] Comment about inheritance order & error messages

Evan Driscoll evaned at gmail.com
Sat May 30 01:21:00 CEST 2009


I was trying to make a custom widget that I could use in the Qt
Designer. I originally had this:

  class QDESIGNER_WIDGET_EXPORT PdfPager
      : public Ui_PdfPager
     , public QWidget
  {
      Q_OBJECT

  public:
      PdfPager(QWidget *parent = 0);
  };

Which produced the following compiler diagnostics (the first was
before the latter ones):
  pager.h:16: Warning: Class PdfPager inherits from two QObject
subclasses Ui_PdfPager and QWidget. This is not supported!
  release/moc_pager.cpp:38: error: ‘staticMetaObject’ is not a member
of ‘Ui_PdfPager’
  release/moc_pager.cpp: In member function ‘virtual void*
PdfPager::qt_metacast(const char*)’:
  release/moc_pager.cpp:54: error: ‘qt_metacast’ is not a member of
‘Ui_PdfPager’
  release/moc_pager.cpp: In member function ‘virtual int
PdfPager::qt_metacall(QMetaObject::Call, int, void**)’:
  release/moc_pager.cpp:59: error: ‘qt_metacall’ is not a member of ‘Ui_PdfPager


So I spend some time trying to figure out why Ui_PdfPager was a
subclass of QObject, and why it worked in another widget I had. I
eventually tried commenting out the Q_OBJECT macro, which let it
compile.

However, this lead to a problem when I tried to use it in Designer, as
it said "A class name mismatch occurred when creating a widget using
the custom widget factory registered for widgets of class PdfPager. It
returned a widget of class QWidget."

After spending like 30 minutes trying to figure out wtf was going
wrong, I decided on a whim to change the inheritance order, and lo and
behold, it works.

This *is* actually in the MOC manual:

    Multiple Inheritance Requires QObject to Be First

    If you are using multiple inheritance, moc assumes that the first inherited
    class is a subclass of QObject. Also, be sure that only the first
inherited class
    is a QObject.

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

    Virtual inheritance with QObject is not supported.


However, it's not mentioned on the QObject page nor, I suspect, on any
page linked from that page... you have to follow the "see also" link
to the meta-object system then another link to the meta-object
compiler page.


Considering that it would probably be really easy to change the
"inherits from two QObject subclasses" error to add "Alternately, the
QObject subclass is not listed first." to the end of it, that the
current error message is just flat out wrong and misleading, and that
you rarely have to be concerned with the order in which superclasesses
are listed, I would suggest that someone make this change so that
other people don't get stuck in the same trap I did.

(The error is constructed at about src/tools/moc/moc.cpp:1200.)

Evan Driscoll




More information about the Qt-interest-old mailing list