[Development] Types defined in c++ plugin aren't available in QML code

Łukasz Korbel korbel85 at gmail.com
Mon Jan 16 18:35:36 CET 2012


Hello,
I have problem with using c++ defined types in QML code. Qt version i use
is Qt 5.0 built from git repository.

Problem:
I wrote simple project in Qt 4.7 according to guidelines from
http://doc.qt.nokia.com/qt5/qml-extending.html and
http://doc.qt.nokia.com/qt5/src-imports-folderlistmodel.html.
Next I made transition from Qt.4.7 to 5.0. (
http://wiki.qt-project.org/Transition_from_Qt_4.x_to_Qt5)
Building of project is going succesful but running qmlscene example.qml
returns error
that MyType(the one I've created) is not a type.

Details of project:
MyType is C++ class meeting following conditions:
1. Inherit from QObject
2. Use Q_OBJECT macro in body and QML_DECLARE_TYPE macro in header file
3. MyType is registered with qmlRegisterType in MyPlugin class

MyPlugin class (in MyPlugin.cpp) inherits from QDeclarativeExtensionPlugin,
defines void registerTypes(const char *uri) method and have
Q_EXPORT_PLUGIN2 macro in file.



Listing:

----------------------------------------------------------------------------------------------------------------------------------
MyType.h
----------------------------------------------------------------------------------------------------------------------------------
#ifndef MYTYPE_H
#define MYTYPE_H

#include <QObject>
#include <QtDeclarative>
#include <QString>

QT_BEGIN_HEADER
QT_MODULE(Declarative)

class MyType : public QObject
{
    Q_OBJECT
    Q_PROPERTY( QString data READ data WRITE setData NOTIFY dataChanged)

    public:
        MyType();
        ~MyType();

        QString data() const;
        void setData( const QString&);

        Q_INVOKABLE void performAlogrithm();

    Q_SIGNALS:
        void dataChanged();

    private:
        Q_DISABLE_COPY(MyType);
        QString data_;
};

QML_DECLARE_TYPE(MyType)
QT_END_HEADER

#endif
----------------------------------------------------------------------------------------------------------------------------------


----------------------------------------------------------------------------------------------------------------------------------
MyType.cpp
----------------------------------------------------------------------------------------------------------------------------------
#include "MyType.h"

MyType:: MyType()
{
    data_ = "init";
}

MyType:: ~MyType()
{
    data_ = "destroy";
}

QString MyType:: data() const
{
    return data_;
}

void MyType:: setData( const QString& data)
{
    data_ = data;
}

void MyType:: performAlogrithm()
{
    //do something
    emit dataChanged();
}
----------------------------------------------------------------------------------------------------------------------------------


----------------------------------------------------------------------------------------------------------------------------------
MyPlugin.cpp
----------------------------------------------------------------------------------------------------------------------------------
#ifndef MYPLUGIN
#define MYPLUGIN

#include <QDeclarativeExtensionPlugin>
#include <qdeclarative.h>
#include "MyType.h"

class MyPlugin : public QDeclarativeExtensionPlugin
{
    Q_OBJECT

    public:
        void registerTypes(const char *uri)
        {
            qmlRegisterType<MyType>(uri, 1, 0, "MyType");
        }
};

#include "MyPlugin.moc"
Q_EXPORT_PLUGIN2(myplugin, QT_PREPEND_NAMESPACE(MyPlugin));

#endif
----------------------------------------------------------------------------------------------------------------------------------


----------------------------------------------------------------------------------------------------------------------------------
project.pro
----------------------------------------------------------------------------------------------------------------------------------

TEMPLATE = lib
CONFIG += qt plugin release
QT += declarative
HEADERS += MyType.h
SOURCES += MyType.cpp MyPlugin.cpp
TARGET = $$qtLibraryTarget(myplugin)
----------------------------------------------------------------------------------------------------------------------------------


----------------------------------------------------------------------------------------------------------------------------------
qmldir
----------------------------------------------------------------------------------------------------------------------------------
plugin myplugin
----------------------------------------------------------------------------------------------------------------------------------


----------------------------------------------------------------------------------------------------------------------------------
example.qml
----------------------------------------------------------------------------------------------------------------------------------
import QtQuick 2.0

Item{
    width: 200; height: 50

    MyType{
        id: myObject
        data: "nothing smart"
        onDataChanged: console.log( myObject.data)
    }

    MouseArea{
        onClicked: myObject.performAlgorithm()
    }
}
----------------------------------------------------------------------------------------------------------------------------------

>qmlscene example.qml
Error: example.qml:7:2: MyType is not a type
        MyType{


Whole project is built using qmake from Qt5. Qmake complains about lack of
quick module but adding it doesnt solve the problem. I also tried relase
and debug version of plugin - same result. There's no problem for QML to
detect types in qml files, it affects only c++ plugins.

I will appreciate any suggestions how to solve this problem.
Thank you in advance,
Łukasz Korbel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20120116/c1bc6647/attachment.html>


More information about the Development mailing list