gpt4 book ai didi

c++ - QObject 继承 Ambiguous Base

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:02:54 27 4
gpt4 key购买 nike

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

最佳答案

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

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

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

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

    QObject
|
|
QApplication
|
|
MyApp

关于c++ - QObject 继承 Ambiguous Base,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7903342/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com