【问题标题】:QMLApplicationViewer multiple instances only first shows windowQMLApplicationViewer 多实例只先显示窗口
【发布时间】:2013-09-14 11:33:07
【问题描述】:

我对 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();
}

提前致谢。

【问题讨论】:

    标签: c++ qml qt-quick qt4.8


    【解决方案1】:

    这行很麻烦:

    QmlApplicationViewer view;

    想一想你在这里做什么?

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

    问。局部变量的生命周期是多少!?

    问。当函数退出时这个变量会发生什么!?

    哦!但是这在 main 中可以正常工作,这也是一个函数?

    问。但是主函数的生命周期是多少?


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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      相关资源
      最近更新 更多