问题
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.
回答1:
I think you should be able to use a custom QQmlIncubator
and the QQmlComponent::create(QQmlIncubator & incubator, QQmlContext * context = 0, QQmlContext * forContext = 0)
factory method.
In particular, quoting from the QQmlIncubator documentation:
void QQmlIncubator::setInitialState(QObject * object) [virtual protected]
Called after the object is first created, but before property bindings are evaluated and, if applicable, QQmlParserStatus::componentComplete() is called. This is equivalent to the point between QQmlComponent::beginCreate() and QQmlComponent::endCreate(), and can be used to assign initial values to the object's properties.
The default implementation does nothing.
回答2:
I have had similar situation for my own QML component. Just wanted to init some props before running some bindings. In pure QML I did it that way:
var some = component.createObject(this, {'modelClass': my_model});
From C++ I tried that way:
// create ui object
auto uiObject = qobject_cast<QQuickItem*>(component.beginCreate(ctx));
// place on ui
uiObject->setParentItem(cont);
// set model properties
classInstance->setView(QVariant::fromValue(uiObject));
classInstance->setPosition(QPointF(x, y));
// set ui object properties
uiObject->setProperty("modelClass", QVariant::fromValue(classInstance.get()));
// finish
component.completeCreate();
but I had binding errors because modelClass remains null! After digging for a while I found the cause. And it looks reasonable for me. I've changed my QML class!
Item {
id: root
// property var modelClass: null
property var modelClass // just remove : null!
}
Properties with initial values right after invoking beginCreate are not visible from C++, until I call completeCreate(). But if I remove initial value property becomes visible and I can initialize it in C++ code
来源:https://stackoverflow.com/questions/19691950/create-qml-object-from-c-with-specified-properties