【问题标题】:QObject inheritance Ambiguous BaseQObject 继承 Ambiguous Base
【发布时间】:2011-10-26 13:32:27
【问题描述】:

我有一个简单的类,当我的程序获得和失去焦点时停止和启动一个计时器,但它给出的错误是 QObject 是 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;
    }

【问题讨论】:

    标签: c++ qt signals-slots qobject ambiguous


    【解决方案1】:

    在您当前的示例中,您创建了一个拆分继承方案,其中您的对象以QObject 的双重实例结束...QApplication 有一个基QObject,另一个用于实际MyApp 类。这会产生歧义,因为访问继承的 QObject 方法或数据成员将不知道要访问哪个继承的基础对象。

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

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

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

        QObject
           |
           |
      QApplication
           |
           |
         MyApp
    

    【讨论】:

    • 好的,但是现在在 Q_OBJECT 宏上方的“{”上以及在 MyApp.cpp 中在此行上两次出现的 MyApp 上获得对“MyApp 的 vtable”的未定义引用:“MyApp:: MyApp(int argc, char **argv): QApplication(argc, argv)"
    • 您的main() 是什么样的? ...我还支持 Laurent 关于在您的项目上运行 qmake 的评论。
    • 我已将我的主要添加到我的问题中
    • 第一次调用MyApp的构造函数在main()还是其他地方触发的错误?
    • 它是由第一次调用 MyApp.cpp 以及宏上方的 MyApp.h 文件中的构造函数触发的?
    【解决方案2】:

    QApplication 已经是QObject,所以你应该简单地写:

    class MyApp : public QApplication
    {
        Q_OBJECT
    ...
    
    }
    

    【讨论】:

    • 好的,但是现在在 Q_OBJECT 宏上方的“{”上以及在 MyApp.cpp 中在此行上两次出现的 MyApp 上获得对“MyApp 的 vtable”的未定义引用:“MyApp:: MyApp(int argc, char **argv): QApplication(argc, argv)"
    • 你是先在你的项目上运行 qmake 吗?
    • 我使用 Qt Creator,它似乎是自动完成的
    猜你喜欢
    • 2012-01-24
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多