QMLApplicationViewer multiple instances only first shows window

432 Views Asked by At

I have a problem with QTQuick1.1 (QT4.8). I have a main class where i instance QMLApplicationViewer and it shows the window. I also call a class that should show another one with another QML File but it is not show. However the Debug Messages are displayed. Here is my code:

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

And in Main

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

Thanks in advance.

1

There are 1 best solutions below

0
On

This line is the trouble :

QmlApplicationViewer view;

Just think for a second what you are doing here?

Q. Instantiating an object which you want throughout the lifetime of your program(hopefully), inside a function ?

Q. Whats the lifetime of a local variable !?

Q. What happens to this variable when the function exits !?

Oh!, but this works fine in the main, which is also a function ?

Q. But then what is the lifetime of the main function ?


Solution : Make this object in such a way that its is available throughout the life of your program. How about a pointer and allocating memory dynamically ? How about making this pointer a data member of your class ? And main doesn't 'exit' before your program ends, does it !?