QObject继承模糊基类

4

我有一个简单的类,在程序获得和失去焦点时停止和启动定时器,但是每次信号槽连接时都会出现QObject is Ambiguous base of MyApp的错误。

以下是相关代码:

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

这是我的(混乱的)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;
    }
2个回答

10

根据你目前的例子,你创建了一种分裂继承方案,导致你的对象最终拥有两个QObject实例......一个是QApplication的基本QObject,另一个是实际的MyApp类的基本QObject。这会造成不确定性,因为访问继承的QObject方法或数据成员将无法确定要访问哪个继承的基本对象。

目前,你的继承图看起来像这样(请注意,你的MyApp对象继承了两个QObject实例):

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

您应该保持继承图线性,而不是采用分裂继承方案,这意味着派生类只包含一个基类实例。因此,您需要像这样:

    QObject
       |
       |
  QApplication
       |
       |
     MyApp

好的,但现在在 Q_OBJECT 宏上面的“{”处以及 MyApp.cpp 文件中,在此行的两个 MyApp 上都出现了“vtable for MyApp”的未定义引用:“MyApp::MyApp(int argc,char **argv):QApplication(argc,argv)”。 - Gerharddc
1
你的 main() 函数是什么样子的?... 我也赞同 Laurent 的评论,先在你的项目上运行 qmake - Jason
我已经将我的主函数添加到了我的问题中。 - Gerharddc
错误是由main()函数中对MyApp构造函数的第一次调用触发的,还是由其他地方引起的? - Jason
它由MyApp.cpp中对我的构造函数的第一次调用以及在我的MyApp.h文件中宏上方触发。 - Gerharddc
显示剩余2条评论

4

QApplication已经是一个QObject,所以你只需要简单地写:

class MyApp : public QApplication
{
    Q_OBJECT
...

}

好的,但现在在 Q_OBJECT 宏上面的“{”处以及 MyApp.cpp 文件中,在此行中的 MyApp 的两个实例上都出现了对“vtable for MyApp”的未定义引用:“MyApp::MyApp(int argc,char **argv):QApplication(argc,argv)”。 - Gerharddc
2
你先在项目上运行了qmake吗? - laurent
我使用Qt Creator,它似乎自动完成了这个操作。 - Gerharddc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接