mardi 2 octobre 2018

Qt Quick application locks up when 25 application windows are created

I'm experiencing a problem where my Qt quick application will lock up after opening 25 ApplicationWindows. I have no idea why so I wrote this simple test app to reproduce the issue. It begins by creating a window with a button and clicking this button creates another window. The window it creates has a button to spawn another window with a button, this will happen infinitely. However, the application locks up after I hit exactly 24 sub menus ( 25 total including the first window created initially). No crash, no error, just stops responding to mouse clicks. Memory and other resources appear to be ok. So why are we locking up? Does Qt have a limit on the number of application windows that can be open? Iv never heard of one. Am I breaking a limit in Windows where a program can only spawn so many windows? I highly doubt that's the case. I have been researching this for a while now with no good reason why it locks up. Any advice would be greatly appreciated.

Main.cpp

#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>

#include "MenuManager.h"

int main(int argc, char *argv[])
{
   QCoreApplication::setAttribute( Qt::AA_UseOpenGLES );
   QApplication app( argc, argv );
   QQmlApplicationEngine engine;

   MenuManager* menuManager = new MenuManager( &engine );
   engine.rootContext()->setContextProperty( "menuManager", menuManager );
   engine.load( QUrl(QStringLiteral( "qrc:/main.qml" ) ) );

   if ( engine.rootObjects().isEmpty() )
      return -1;

   return app.exec();

   delete menuManager;
}

MenuManager.h

#pragma once

#include <QQmlApplicationEngine>
#include <QObject>

class MenuManager :
   public QObject
{
   public:
      explicit MenuManager( QQmlApplicationEngine* engine );

   public slots:
      void openSubMenu();

   private:
      Q_OBJECT

      QQmlApplicationEngine* engine;
};

MenuManager.cpp

#include <QQuickItem>
#include <QWindow>

#include "MenuManager.h"

MenuManager::MenuManager( QQmlApplicationEngine* engine ) :
   engine{ engine }
{
}

void MenuManager::openSubMenu()
{
   QWindow* createdComponent = nullptr;
   QQmlComponent component( engine, QUrl( QString( "qrc:///Menu.qml" ) ), 
                            QQmlComponent::PreferSynchronous );

   if ( component.status() == QQmlComponent::Ready )
      createdComponent = qobject_cast< QWindow* >( component.create() );
}

main.qml

import QtQuick.Window 2.2
import QtQuick.Controls 2.0

ApplicationWindow
{
   id: mainWindow

   title: qsTr( "Test" )

   visible: true
   x: 100
   y: 100
   width: 200
   height: 200

   Button
   {
      text: "TopLevelMenu"

      onClicked: menuManager.openSubMenu();
   }
}

Menu.qml

import QtQuick.Controls 2.0
import QtQuick.Window 2.2

ApplicationWindow
{
   id: menu

   visible: true
   x: 100
   y: 100
   width: 200
   height: 200

   Button
   {
      onClicked: menuManager.openSubMenu()

      text: "SubMenu"
   }
}

Aucun commentaire:

Enregistrer un commentaire