April 20, 2012

redkite redkite
Lab Rat
24 posts

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 [doc-snapshot.qt-project.org]” 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 [doc-snapshot.qt-project.org]” item from QML component did?

2 replies

April 23, 2012

mbrasser mbrasser
Ant Farmer
452 posts

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:

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

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

Regards,
Michael

April 23, 2012

redkite redkite
Lab Rat
24 posts

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 :

  1. 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.

 
  ‹‹ How to close started network connection      [SOLVED] QML TextInput text padding? ››

You must log in to post a reply. Not a member yet? Register here!