2011-10-26 29 views
3

我有一个简单的类,当我的程序获得并失去焦点时停止并启动一个计时器,但它给出的错误是QObject是MyApp在每个信号插槽连接上的模糊基础。 下面是相关代码:QObject继承不明确的基地

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; 
    } 

回答

9

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

现在,你的继承图看起来是这样的(注意的QObject的两个实例,你MyApp对象继承):

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

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

QObject 
     | 
     | 
    QApplication 
     | 
     | 
    MyApp 
+0

好吧,但是现在在Q_OBJECT宏上方的“{”上以及MyApp.cpp中这两行上MyApp的两次出现中获得对'MyApp'的vtable的未定义引用:“MyApp :: MyApp(int argc,char ** argv):QApplication(argc,argv)” – Gerharddc

+1

你的'main()'看起来像什么? ...我也会首先在你的项目上再次发表Laurent关于运行'qmake'的评论。 – Jason

+0

我已将我的主添加到我的qeustion中 – Gerharddc

4

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

class MyApp : public QApplication 
{ 
    Q_OBJECT 
... 

} 
+0

好了,但现在就在这行上MyApp的两个OCCURENCES一个未定义的引用“虚函数表的MyApp的”对“{”上面的Q_OBJECT宏,在MyApp.cpp中:“MyApp :: MyApp(int argc,char ** argv):QApplication(argc,argv)” – Gerharddc

+2

您是否首先在您的项目上运行qmake? –

+0

我使用Qt Creator,它似乎自动执行 – Gerharddc