Important: Please read the Qt Code of Conduct -

Question about QDeclarativeComponent::setData()

  • Hello, I don't understand the documentation on QDeclarativeComponent::setData(). What does this mean: "If url is provided, it is used to set the component name and to provide a base path for items resolved by this component."?

    Am I able to create a component in c++ side based on a QML string and then create an instance of it in QML like this, MyComponent {}?

  • if you want to use Qt objects like QML you have to try QGraphicsView and paint your object. actually QML is script based version of QGraphics. so you can have scenes and animations like QML but with doing some code.

  • Thanks mohsen for trying to help :) I did not understand your answer though. I know that I can create a QObject derived object or QDeclarativeItem derived object in c++ and then expose it to be used in QML side with qmlRegisterType. I was wondering if I can create the object like this in c++:

    QDeclarativeComponent *comp = new QDeclarativeComponent(engine);
    comp->setData("import QtQuick 1.1 \n Rectangle{ \n color: "red" \n border.color: "blue" \n border.width : 8 } ");

    ... and then instantiate this component in QML?

    I am able to instantiate it in c++ side and the rectangle will appear on screen. I was also able to put it in correct place by setting a proper parent.

Log in to reply