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.