Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved] Switching between QML and UI windows
Forum Updated to NodeBB v4.3 + New Features

[Solved] Switching between QML and UI windows

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 4.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Endless
    wrote on last edited by
    #1

    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?
    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • E Offline
        E Offline
        Endless
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved