4

I have a simple class that stops and starts a timer when my program gains and loses focus but it gives the error tha QObject is Ambiguous base of MyApp on every signal-slot connection. Here is the relevant code:

class MyApp : public QApplication, public QObject
{
    Q_OBJECT
    ...
}

Here is my (messy) Main.cpp:

    #include <QtGui/QApplication>
    #include "qmlapplicationviewer.h"
    #include <QObject>
    #include <QGraphicsObject>
    #include <QTimer>
    #include <QVariant>
    #include "timecontrol.h"
    #include "scorecontrol.h"
    #include "Retry.h"
    #include <QEvent>
    #include "myapp.h"

    int main(int argc, char *argv[])
    {
        MyApp app(argc, argv);

        QmlApplicationViewer viewer;
        viewer.setOrientation(QmlApplicationViewer::ScreenOrientationLockLandscape);
        viewer.setMainQmlFile(QLatin1String("qml/Raker/main.qml"));
        viewer.showExpanded();

        QObject *rootObject = viewer.rootObject();

        QTimer *timmer = new QTimer;
        timmer->setInterval(1000);

        TimeControl *timcon = new TimeControl;

        scorecontrol *scorer = new scorecontrol;

        Retry *probeer = new Retry;

        QObject::connect(timmer, SIGNAL(timeout()), timcon, SLOT(updateTime()));
        QObject::connect(timcon, SIGNAL(setTime(QVariant)), rootObject, SLOT(setTime(QVariant)));
        QObject::connect(rootObject, SIGNAL(blockClicked(int, int)), scorer, SLOT(checkRight(int, int)));
        QObject::connect(scorer, SIGNAL(setScore(QVariant)), rootObject, SLOT(setScore(QVariant)));
        QObject::connect(scorer, SIGNAL(setState(QVariant)), rootObject, SLOT(setState(QVariant)));

        QObject::connect(rootObject, SIGNAL(start()), probeer, SLOT(Reetry()));
        QObject::connect(probeer, SIGNAL(start()), timmer, SLOT(start()));
        QObject::connect(probeer, SIGNAL(stop()), timmer, SLOT(stop()));
        QObject::connect(probeer, SIGNAL(start(int)), scorer, SLOT(randomNum(int)));
        QObject::connect(probeer, SIGNAL(sReset()), timcon, SLOT(reset()));
        QObject::connect(probeer, SIGNAL(tReset()), scorer, SLOT(reset()));
        QObject::connect(timcon, SIGNAL(timeOut()), scorer, SLOT(reset()));

        QObject::connect(timcon, SIGNAL(setState(QVariant)), rootObject, SLOT(setState(QVariant)));
        QObject::connect(timcon, SIGNAL(changeFinal()), scorer, SLOT(changeFinal()));
        QObject::connect(scorer, SIGNAL(setFinal(QVariant)), rootObject, SLOT(setFinal(QVariant)));

        QObject::connect(&app, SIGNAL(focusL()), probeer, SLOT(focusL()));
        QObject::connect(&app, SIGNAL(focusG()), probeer, SLOT(focusG()));

        return app.exec();
    }

MyApp.cpp:

    #include "myapp.h"
    #include <QDebug>
    #include <QObject>

    MyApp::MyApp(int argc, char **argv): QApplication(argc, argv)
    {
        installEventFilter(this);
    }

    bool MyApp::eventFilter(QObject *object, QEvent *event)
    {
        if (event->type() == QEvent::ApplicationDeactivate)
        {
            qDebug() << "Focus lost";
            focusL();
        }
        if (event->type() == QEvent::ApplicationActivate)
        {
            qDebug() << "Focus gained";
            focusG();
        }

        return false;
    }
Gerharddc
  • 3,921
  • 8
  • 45
  • 83

2 Answers2

10

With your current example, you've created a split inheritance scheme, where your object ends up with dual instances of a QObject ... there is one base QObject for the QApplication, and another for the actual MyApp class. That's going to create ambiguity since accessing an inherited QObject method or data-member will not know exactly which inherited base object to access.

Right now, your inheritance diagram looks like this (note the two instances of QObject your MyApp object inherits):

 | QObject |         | QObject |
       \                 /
        \         | QApplication |
         \             /
          \           /
         |    MyApp    |

You should keep your inheritance diagram linear, rather than have a split inheritance scheme, and that means having a derived class that only contains one instance of the base-class. So you want something like this:

    QObject
       |
       |
  QApplication
       |
       |
     MyApp
Jason
  • 31,834
  • 7
  • 59
  • 78
  • Ok, but now get a undefined reference to 'vtable for MyApp' on the "{" above the Q_OBJECT macro, and in MyApp.cpp on both occurences of MyApp on this line : "MyApp::MyApp(int argc, char **argv): QApplication(argc, argv)" – Gerharddc Oct 26 '11 at 13:59
  • 1
    What does your `main()` look like? ... I'd also second the comment from Laurent about running `qmake` as well on your project first. – Jason Oct 26 '11 at 14:11
  • I have added my main to my qeustion – Gerharddc Oct 26 '11 at 14:16
  • Is the error triggered by the first call to the constructor of `MyApp` in `main()` or somewhere else? – Jason Oct 26 '11 at 14:19
  • Its trigged by the first call to my constructor in the MyApp.cpp as well as in my MyApp.h file above the macro? – Gerharddc Oct 26 '11 at 14:22
  • Without the call to focusL and G and without the macro it works fine though? – Gerharddc Oct 26 '11 at 14:28
  • I'm sorry, I'm at a bit of a loss on this problem right now... typically that error is caused by not running `qmake` ... – Jason Oct 26 '11 at 14:37
4

QApplication is already a QObject, so you should simply write:

class MyApp : public QApplication
{
    Q_OBJECT
...

}
laurent
  • 88,262
  • 77
  • 290
  • 428
  • Ok, but now get a undefined reference to 'vtable for MyApp' on the "{" above the Q_OBJECT macro, and in MyApp.cpp on both occurences of MyApp on this line : "MyApp::MyApp(int argc, char **argv): QApplication(argc, argv)" – Gerharddc Oct 26 '11 at 13:59
  • 2
    Did you run qmake on your project first? – laurent Oct 26 '11 at 14:10
  • I use Qt Creator and it appears to do it automatticly – Gerharddc Oct 26 '11 at 14:12