Subclassing QML Image item



  • Hello,

    I'm trying to create a QML Image item from a C++ class, for dynamically adding image into a QDeclarativeView.
    I found that I need to herit from QDeclarativeItem and to implement paint(...).

    Isn't it possible to directly use/herit the QML item "Image":http://doc-snapshot.qt-project.org/4.8/qml-image.html" instead? Like this, I would only need to set the property "source" by my self, and the painting, the scaling, the fitting, ... will be managed automatically like "Image":http://doc-snapshot.qt-project.org/4.8/qml-image.html" item from QML component did?



  • Hi,

    It isn't possible to use or inherit QDeclarativeImage directly from C++ (it is a private class). One option for creating and manipulating Image elements from C++ is the following:

    @
    QQmlEngine engine;
    QQmlComponent component(&engine);
    component.setData("import QtQuick 1.0;\nImage {}", QUrl());
    QObject *object = component.create();
    @

    You could then set the source either via QObject::setProperty(), or by manipulating the string passed to setData().

    Regards,
    Michael



  • Thanks for the reply,

    it was already what I did (I suppose you mean QDeclarativeComponent in Qt 4.8.1), except that I was loading a nearly empty QML file :
    @QDeclarativeComponent component(engine, QUrl::fromLocalFile("myimage.qml"));@

    But it's still not allowing me to get a C++ class like MyImage which would also be a QDeclarativeImage.

    I was needing a direct inherit from my C++ class to the declarative class for easily founding my C++ class from the parent item of an other QML item using findChild.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.