[Solved] Switching between QML and UI windows



  • My application opens up a QML window (because it's so easy to do any visual effects in QML) and sets a timer. The animation runs fine, and the timer goes off at the end of the animation time. What I'd like to do at that point is close the QML window and open a UI window. The UI window opens but disappears immediately. Here are the three sections of code that are important. The main function, the timer code, and the mainwindow code.

    main.cpp:
    @
    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    QDeclarativeView view;
    AnimationTimer mTimer;
    view.setSource(QUrl("qrc:/MyFiles/animation.qml"));
    view.show();
    return app.exec();
    }
    @

    antimer.cpp:
    @
    AnimationTimer::AnimationTimer()
    {
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(EndAnimationSlot()));
    timer -> start(4850);
    }

    void AnimationTimer::EndAnimationSlot()
    {
    qDebug() << "Timer tripped";
    delete timer;
    MainWindow mainw;
    mainw.show();
    }
    @

    mainwindow.cpp:
    @
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }
    @

    When I put the mainwindow call in the main function, it works fine, so I know the UI window works. Here are my two questions:

    1. Why doesn't the mainwindow stay on the screen for more than an instant?
    2. How can I get rid of the QML window?


  • The problem is, that the MainWindow in antimer.cpp goes out of scope immediately. Instead of creating your mainWindow on the stack, create it on the heap using the new keyword.

    Edit:
    As for how to get rid of the QML window. One option is to add a signal to your animation timer class, and connect that signal with the deleteLater() slot of the QDeclarativeView that shows your QML. You can emit the signal after creating your main window. That should take care of deleting your QML window for you.



  • Yes, I made those two changes, and everything works perfectly. Thanks so much!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.