#include <QApplication>#include "windowmanager.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); WindowManager manager; manager.showWindow(); return a.exec();}
#ifndef WINDOWMANAGER_H#define WINDOWMANAGER_H#include <QObject>#include <QDeclarativeView>class WindowManager : public QObject{ Q_OBJECT public: explicit WindowManager(QObject *parent = 0); void showWindow(); Q_INVOKABLE void closeWindow(); QDeclarativeView *window(); private: QDeclarativeView *m_window;};#endif // WINDOWMANAGER_H
#include "windowmanager.h"#include <QDeclarativeContext>WindowManager::WindowManager(QObject *parent) : QObject(parent), m_window(0){}void WindowManager::showWindow(){ window()->show();}void WindowManager::closeWindow(){ m_window->close(); if (m_window != 0) { delete m_window; m_window = 0; }}QDeclarativeView *WindowManager::window(){ if (m_window == 0) { m_window = new QDeclarativeView(0); m_window->rootContext()->setContextProperty("manager",this); m_window->setSource(QUrl("qrc:/main.qml")); } return m_window;}
import QtQuick 1.1Item { width: 500 height: 500 Rectangle { width: 200 height: 200 anchors.centerIn: parent color: "red" Text { anchors.centerIn: parent color: "white" text: "Click for crash" } MouseArea { anchors.fill: parent onClicked: { manager.closeWindow(); } } }}
C++ (Qt)delete m_window;
C++ (Qt)m_window->deleteLater();
C++ (Qt)m_window->rootContext()->setContextProperty("app",qApp);
JavascriptonClicked: { app.quit(); }
C++ (Qt)void WindowManager::closeWindow(){ m_window->hide(); m_window->deleteLater();}
C++ (Qt)QTimer::singleShot(1000, m_window, SLOT(deleteLater()));
C++ (Qt)m_window->disconnect();
QTimer::singleShot(1000, m_window, SLOT(deleteLater()));