dtech
dtech

Reputation: 49329

How to create custom basic type in QML?

I don't seem to find any resources on adding basic types to QML. I don't mean QObject or higher derived elements, but the basic types - bool, int, real and so on.

Didn't have any luck digging for it in the sources as well...

So any info is appreciated.

Upvotes: 3

Views: 4903

Answers (3)

vpicaver
vpicaver

Reputation: 1801

As of Qt 5.5 you can create basic types by using Q_GAGDET. For example you can create a custom MyError class:

class MyError
{
    Q_GADGET
    Q_ENUMS(ErrorType)

    Q_PROPERTY(bool suppressed READ suppressed WRITE setSupressed)
    Q_PROPERTY(QString message READ message WRITE setMessage)
    Q_PROPERTY(ErrorType type READ type WRITE setType)

public:
    enum ErrorType {
        Warning, //Survex should still run
        Fatal, //Survex will not run
        Unknown
    };

    cwSurveyChunkError();
    cwSurveyChunkError(const cwSurveyChunkError &);
    cwSurveyChunkError &operator=(const cwSurveyChunkError &);
    ~cwSurveyChunkError();

    ErrorType type() const;
    void setType(ErrorType type);

    QString message() const;
    void setMessage(QString message);

    bool suppressed() const;
    void setSupressed(bool suppressed);

private:
    QSharedDataPointer<MyErrorData> data;
};

Q_DECLARE_METATYPE(MyError) //Allows you to use this class in QVariant

You need to register the type with qml, using qmlRegisterUncreatableType. This allows you to use MyError.ErrorType enums in QML. Since MyError is a Q_GADGET you can pass it to QML directly and use MyError properties, as if it was a QObject. Q_GADGET object's are lighter weight versions of QObject except they can't be created in QML, and signals and slots don't work in them.

QML example:

Item {

   Component.onCompleted: {
        var error = model.errorAt(4); //Returns MyError 
        console.log("Error:" + error.type + " " + error.message + " " + error.suppressed)
   }
}

Upvotes: 0

stackunderflow
stackunderflow

Reputation: 10682

Check out this article and this article about how to create custom types in Qt/QML. As far as creating your own primitive types (int, bool, double, etc)... I don't think this is a possibility without digging into the source code of the Qt language itself and I don't think that's a good idea.

What purpose would there be to making your own primitive type? Why can't you use the ones already defined by the language itself?

If what you are trying to do is renaming a primitive type, there is always an option to do a typedef. "The purpose of typedef is to assign alternative names to existing types, most often those whose standard declaration is cumbersome, potentially confusing, or likely to vary from one implementation to another."

Upvotes: 0

MartinJ
MartinJ

Reputation: 1701

I assume you want to register more complex types than int, bool, real, since they are already present.

You cannot do this in Qt 4.x. It is possible in Qt 5 (QML 2), but there is no public API. If you still want to do it, you'll need to dig into some code to see how its done, as there's no documentation.

You need to implement a QQmlValueTypeProvider (qqmlglobal_p.h) and create QQmlValueTypeBase<> subclasses (qqmlvaluetype_p.h) for each of your types. Then register your value type provider with QQml_addValueTypeProvider() in your plugin's registerTypes().

One example you could look at are QGeoCoordinate, QGeoRectangle, etc. from QtLocation module: http://qt.gitorious.org/qt/qtlocation/trees/master/src/imports/location

Good luck.

Upvotes: 8

Related Questions