Expose abstract type as Q_PROPERTY to QML

krdx

I am using Qt 4.8 with BB10.

I defined a base interface for classes to implement:

class AbstractImageProcessor : public QObject
{
public:
    AbstractImageProcessor(QObject * parent) : QObject(parent) {}
    virtual QImage process(const QByteArray &data) = 0;
    virtual ~AbstractImageProcessor(){ }
};

One such implementation that I want usable from QML looks like this:

class WebImageProcessor : public AbstractImageProcessor
{
    Q_OBJECT
    Q_PROPERTY(int maxHeight READ getMaxHeight WRITE setMaxHeight NOTIFY maxHeightChanged)
    Q_PROPERTY(int maxWidth READ getMaxWidth WRITE setMaxWidth NOTIFY maxWidthChanged)
    Q_PROPERTY(bool fit READ isFit NOTIFY fitChanged)
    public WebImageProcessor(QObject * parent = 0) : AbstractImageProcessor(parent) {}
    virtual ~WebImageProcessor() {}
    /* rest of code omitted */ 
};

I want to expose this AbstractImageProcessor as a property on another QML type:

class WebImageView : public bb::cascades::ImageView {
    Q_OBJECT
    Q_PROPERTY(AbstractImageProcessor* processor READ getProcessor WRITE setProcessor NOTIFY processorChanged)
    WebImageView(bb::cascades::Container * parent) : bb::cascades::ImageView(parent)  {}
    virtual WebImageView() {}
    /* rest of code omitted */
};

So I register my custom types with QML

//application.cpp
qmlRegisterUncreatableType<AbstractImageProcessor>("foo.controls", 1, 0, "AbstractImageProcessor", ""); ;
qmlRegisterType<WebImageProcessor>("foo.controls", 1, 0, "WebImageProcessor");
qmlRegisterType<WebImageView>("foo.controls", 1, 0, "WebImageView");

How I want to use it in QML

//main.qml
import foo.controls 1.0
/* omitted containers */

WebImageView { 
  processor: WebImageProcessor {
     maxHeight: 500
     maxWidth: 300
  } 
  /* rest of properties omitted */
}

But once I launch my application it fails to parse the qml document.

bb::cascades::QmlDocument: error when loading QML from: QUrl( "asset:///main.qml" )
--- errors: (asset:///main.qml:138:57: Cannot assign object to property) bb::cascades::QmlDocument:createRootObject document is not loaded or has errors, can't create root

In fact if I hover over the WebImageProcessor class in the editor, it says:

The super type of the component WebImageProcessor is unknown, some of its properties are not validated.

Now the thing is that the for example the built in cascades ListView exposes an abstract type as a Q_PROPERTY:

http://developer.blackberry.com/native/reference/cascades/bb_cascades_listview.html#property-datamodel

Event inspecting the header files of bb::cascades::ListView and bb::cascades::DataModel gives me no other clues because it's done essentially the same way.

Could it be that I have to register the types in a different way? If so how?

If I use WebImageProcessor in the Q_PROPERTY instead of the AbstractImageProcessor then it works as expected, but I want to expose the abstract type, and given that cascades does it then it's definitely possible somehow

Thanks

Frank Osterfeld

Your AbstractImageProcessor declaration is lacking a Q_OBJECT macro, which is necessary to export the class to QtQuick.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Expose abstract type as Q_PROPERTY to QML

From Dev

How to expose a pointer to a Q_GADGET to QML through a Q_PROPERTY

From Dev

Bind a QAbstractListModel derived listmodel member of an object in QML as Q_PROPERTY

From Dev

How to expose property of QObject pointer to QML

From Dev

Q_PROPERTY of type float on custom widget does not appear in the Property Editor

From Dev

expose C++ class to QML

From Dev

Expose QList<QObject *> hierarchy to QML

From Dev

How to set property type of qml signal?

From Dev

How to set property type of qml signal?

From Dev

loading data from database to expose in qml

From Dev

How to expose C++ structs for computations to Qml

From Dev

Expose and add QWebView widget object to QML

From Dev

Expose and add QWebView widget object to QML

From Dev

Not able to use imported type as property type in QML when imported with qualifier

From Dev

How to override a Q_Property?

From Dev

Defining generic property in abstract class as a type of the implementing class

From Dev

Overriding abstract property using more specified return type (covariance)

From Dev

Inheritance of abstract class use another Type than defined for property

From Dev

ServiceStack: Property in request DTO becomes null if type is abstract

From Dev

Overriding abstract property using more specified return type (covariance)

From Dev

Abstract interface cannot be used as type in array - missing property

From Dev

Setting object type property in QML from C++

From Dev

Store any object as as string, but expose as typed object or Map a non-string property of any type to a string column

From Dev

Abstract Data Type and Interface

From Dev

Abstract constructor type in TypeScript

From Dev

ML abstract data type

From Dev

Parametric locally abstract type

From Dev

Cannot instantiate the type (abstract)

From Dev

С++ abstract return type

Related Related

  1. 1

    Expose abstract type as Q_PROPERTY to QML

  2. 2

    How to expose a pointer to a Q_GADGET to QML through a Q_PROPERTY

  3. 3

    Bind a QAbstractListModel derived listmodel member of an object in QML as Q_PROPERTY

  4. 4

    How to expose property of QObject pointer to QML

  5. 5

    Q_PROPERTY of type float on custom widget does not appear in the Property Editor

  6. 6

    expose C++ class to QML

  7. 7

    Expose QList<QObject *> hierarchy to QML

  8. 8

    How to set property type of qml signal?

  9. 9

    How to set property type of qml signal?

  10. 10

    loading data from database to expose in qml

  11. 11

    How to expose C++ structs for computations to Qml

  12. 12

    Expose and add QWebView widget object to QML

  13. 13

    Expose and add QWebView widget object to QML

  14. 14

    Not able to use imported type as property type in QML when imported with qualifier

  15. 15

    How to override a Q_Property?

  16. 16

    Defining generic property in abstract class as a type of the implementing class

  17. 17

    Overriding abstract property using more specified return type (covariance)

  18. 18

    Inheritance of abstract class use another Type than defined for property

  19. 19

    ServiceStack: Property in request DTO becomes null if type is abstract

  20. 20

    Overriding abstract property using more specified return type (covariance)

  21. 21

    Abstract interface cannot be used as type in array - missing property

  22. 22

    Setting object type property in QML from C++

  23. 23

    Store any object as as string, but expose as typed object or Map a non-string property of any type to a string column

  24. 24

    Abstract Data Type and Interface

  25. 25

    Abstract constructor type in TypeScript

  26. 26

    ML abstract data type

  27. 27

    Parametric locally abstract type

  28. 28

    Cannot instantiate the type (abstract)

  29. 29

    С++ abstract return type

HotTag

Archive