Dynamically instantiating a QML object from C++ is well documented, but what I can't find is how to instantiate it with pre-specified values for it's properties.
For example, I am creating a slightly modified SplitView from C++ like this:
QQmlEngine* engine = QtQml::qmlEngine( this );
QQmlComponent splitComp( engine, QUrl( "qrc:/qml/Sy_splitView.qml" ) );
QObject* splitter = splitComp.create();
splitter->setProperty( "orientation", QVariant::fromValue( orientation ) );
The problem I have is that specifying the orientation of the SplitView after it is instantiated causes it's internal layout to break. So, is there a way of creating the SplitView with the orientation already specified?
Alternatively I can create both a horizontal and vertical version of SplitView in separate files and instantiate the appropriate one at runtime - but this is less elegant.
Update
I found QQmlComponent::beginCreate(QQmlContext* publicContext):
QQmlEngine* engine = QtQml::qmlEngine( this );
QQmlComponent splitComp( engine, QUrl( "qrc:/qml/Sy_splitView.qml" ) );
QObject* splitter = splitComp.beginCreate( engine->contextForObject( this ) );
splitter->setProperty( "orientation", QVariant::fromValue( orientation ) );
splitter->setParent( parent() );
splitter->setProperty( "parent", QVariant::fromValue( parent() ) );
splitComp.completeCreate();
But it had no effect surprisingly.
QObjectparent first, and then QML visual parent second (if I could set theQObjectparent via QML, I wouldn't have bothered with C++ at all for this).SplitViewcontains two custom OSG viewports, each of which in turn can be split ad infinitum (like Qt Creator's text editor panes), forming a tree. So when I delete a particular splitter, it deletes all of the children appropriately. I'll take a look at having ownership on QML side, it might simplify things - thanks!