QObject继承Ambiguous Base

Ger*_*ddc 4 c++ qt signals-slots ambiguous qobject

我有一个简单的类,当我的程序获得并失去焦点时停止并启动一个计时器但是它给出了错误,因为QObject是每个信号槽连接上的MyApp的模糊基础.这是相关代码:

class MyApp : public QApplication, public QObject
{
    Q_OBJECT
    ...
}
Run Code Online (Sandbox Code Playgroud)

这是我的(凌乱)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();
    }
Run Code Online (Sandbox Code Playgroud)

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;
    }
Run Code Online (Sandbox Code Playgroud)

Jas*_*son 10

根据您目前的例子中,你已经创建了一个分割继承方案,你的对象,具有的双重情况下结束了QObject...有一个碱基QObjectQApplication,而另一个用于实际的MyApp类.这会产生歧义,因为访问继承的QObject方法或数据成员将无法准确知道要访问哪个继承的基础对象.

现在,您的继承图如下所示(请注意您的MyApp对象继承的两个QObject实例):

 | QObject |         | QObject |
       \                 /
        \         | QApplication |
         \             /
          \           /
         |    MyApp    |
Run Code Online (Sandbox Code Playgroud)

您应该保持继承图是线性的,而不是使用拆分继承方案,这意味着拥有一个只包含一个基类实例的派生类.所以你想要这样的东西:

    QObject
       |
       |
  QApplication
       |
       |
     MyApp
Run Code Online (Sandbox Code Playgroud)