OmixVisualization / qtjambi

QtJambi is a wrapper for using Qt in Java.
http://www.qtjambi.io
Other
365 stars 43 forks source link

How can I generate bindings for a custom class? #164

Closed paul35621 closed 1 year ago

paul35621 commented 1 year ago

I'm trying to implement a subclass of QAbstractItemModel in C++ instead of Java to get better performance. It would be nice if I can generate Java bindings for this class, but I'm struggling with this. I'm using Qt 6.5.1 and QtJambi 6.5.2 and I compiled the generator using Ant 1.10.14 and MSVC 19.29.30151.

When I run QtJambiGenerator.exe nodemodel.h I can read in mjb_rejected_classes.log that NodeModel is not in the type system. So I figured that I need to put NodeModel in the type system some how and I made the file typesystem.qml.

When I run QtJambiGenerator.exe -t typesystem.qml nodemodel.h I get a No such file or directory: <QAbstractItemModel> warning in reported_warnings.log, so it seems I need to add an include path.

Now I run QtJambiGenerator.exe -I "C:\Qt\6.5.1\msvc2019_64\include;C:\Qt\6.5.1\msvc2019_64\include\QtCore" -t typesystem.qml nodemodel.h, and it seems to work! The C++ and Java files that are generated look alright.

I started writing this issue, convinced that I wouldn't get it working, but now it works. I'm posting it anyway so there is some documentation.

nodemodel.h

#include <QAbstractItemModel>

class NodeModel : public QAbstractItemModel
{
    Q_OBJECT

    public:
        NodeModel (QObject *parent = nullptr);
        int columnCount (const QModelIndex &parent = QModelIndex()) const override;
        QVariant data (const QModelIndex &index, int role = Qt::DisplayRole) const override;
        QModelIndex index (int row, int column, const QModelIndex &parent = QModelIndex()) const override;
        QModelIndex parent (const QModelIndex &index) const override;
        int rowCount(const QModelIndex &parent = QModelIndex()) const override;
};

typesystem.qml

import QtJambiGenerator 1.0

TypeSystem {
    LoadTypeSystem {name: "QtCore"}
    ObjectType {
        name: "NodeModel"
    }
}