QMLApplicationViewer 多个实例仅首先显示窗口

QMLApplicationViewer multiple instances only first shows window

本文关键字:显示 窗口 实例 QMLApplicationViewer      更新时间:2023-10-16

我对QTQuick1.1(QT4.8)有问题。我有一个主类,我在其中实例QMLApplicationViewer,它显示了窗口。我还调用一个应该显示另一个带有另一个 QML 文件的类,但它没有显示。但是,将显示调试消息。这是我的代码:

AnotherWindow::AnotherWindow(QString notImportant)
{
}
void AnotherWindow::create(){
QmlApplicationViewer view;
qDebug()<<"dbug: CWCReate";
view.addImportPath(QLatin1String("modules"));
view.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
view.setMainQmlFile(QString("instanceOfAnotherWindow.qml"));
view.showExpanded();
qDebug()<<"dbug: show";
}

而在主

QmlApplicationViewer view;
view.addImportPath(QLatin1String("modules"));
view.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
view.setMainQmlFile(QLatin1String("/main.qml"));
view.setFixedSize(360,600);
AnotherWindow *c = new AnotherWindow("notImportantHere");
c->create();
view.showExpanded();
return app->exec();
}

提前谢谢。

这一行是麻烦:

QmlApplicationViewer view;

想想你在这里做什么?

Q. 在函数中实例化一个你想要的在程序的整个生命周期内(希望如此)的对象?

问:局部变量的生存期是多少!?

问:当函数退出时,此变量会发生什么情况!

哦!,但这在主要工作正常,这也是一个功能?

问:但是主函数的寿命是多少?


解决方案 : 使此对象在程序的整个生命周期中都可用。指针和动态分配内存怎么样?如何使此指针成为类的数据成员?而且 main 不会在您的程序结束之前"退出",是吗!?