[Qt-qml] How to display QDeclarativeComponent created from C++?

Charley Bay charleyb123 at gmail.com
Mon Oct 11 21:10:50 CEST 2010


I've been using QDeclarativeView to display QML
files launched from C++.  Works great.

However, the docs seem to suggest that although
QDeclarativeView can be used in production applications,
the developer may want to avoid creating too many of
them (to avoid performance problems):

<http://doc.trolltech.com/4.7/qml-integration.html>

So, I then followed the docs to instantiate QDeclarativeEngine
and QDeclarativeComponent, and then creating my
QDeclarativeItem instances.  Also works great.

However, the instantiated QDeclarativeItem instances
do not display until I "QDeclarativeItem::setParentItem()",
passing in a QDeclarativeView as the parent.  This works,
but is this the proper use model?

For example, I previously created *many* QDeclarativeView
instances to give the appearance of many
"desktop-widget-type-things" within the same application.
If I "setParentItem()" on my QDeclarativeItem instances, and
want them to display independently (like their own dialogs,
or their own "desktop-widget-type-things"), then must the
parent *always* be a QDeclarativeView?

I know the QDeclarativeView is a "convenience" class, but
it's pretty resource heavy, and I'd prefer to not have many of
those (because I've already established my own infrastructure
to maintain C++ instances for a single QDeclarativeEngine,
multiple QDeclarativeContexts, my QDeclarativeComponents,
and then my instantiated QDeclarativeItem instances.

Is there a way to display *without* QDeclarativeView?  If
an application has many dialogs, is the assumption that
each will be in its own QDeclarativeView (or what is the
alternative)?

Finally, it's not clear to me that I'm allowed to "delete"
a QDeclarativeView when I'm "done" with it (e.g., when
the dialog is dismissed).  (After a delete, and after completion
of the dismiss callback, I later get a crash from within
the Qt libraries attempting to access the property on an
object that I think is related to the now-deleted QDeclarativeView).

QUESTION #1:  For a non-legacy (e.g., non-QWidget) application,
Is a QDeclarativeView necessary as a "root" widget to display a
QDeclarativeItem?

QUESTION #2:  What is the expected lifecycle when I'm "done"
with a QDeclarativeView (e.g,. it represents a dialog, now the
dialog is dismissed)?  (e.g., Should I "my_old_view->deleteLater()"
or "delete my_old_view"?)

--charley



More information about the Qt-qml mailing list