I have interesting example. We are have qml form, and button. After click need close window and delete window. Simple. But application stable crash. Example:
main.cpp
#include <QApplication>
#include "windowmanager.h"
int main(int argc, char *argv[])
{
* *WindowManager manager;
* *manager.showWindow();
* *return a.exec();
}
#include <QApplication>
#include "windowmanager.h"
int main(int argc, char *argv[])
{
* *QApplication a(argc, argv);
* *WindowManager manager;
* *manager.showWindow();
* *return a.exec();
}
To copy to clipboard, switch view to plain text mode
windowmanager.h
#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
#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
To copy to clipboard, switch view to plain text mode
windowmanager.cpp
#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;
}
#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;
}
To copy to clipboard, switch view to plain text mode
main.qml
import QtQuick 1.1
Item {
* *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();
* * * * * *}
* * * *}
* *}
}
import QtQuick 1.1
Item {
* *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();
* * * * * *}
* * * *}
* *}
}
To copy to clipboard, switch view to plain text mode
If i use, deleteLater() then applicaition crash in 20% users. If i user hotkey for delete window, all work is good, but why app crash in click button?
Bookmarks