README.md 5,5 КБ
Newer Older
Ilya Pankratov's avatar
Ilya Pankratov включено в состав коммита
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
[**EN**](README.md) | [RU](README.ru.md)

# QtBindings

A gradle plugin that generates Qt bindings for Kotlin code compiled into a
C static or shared library. It helps to share business logic using Kotlin
Multiplatform and create UI using Qt Quick (QML).

**Supported KMP targets**: linuxArm64, linuxX64.

## Add QtBindings to the project

Add plugin to the build script and set up linux target(s) to compile
static or shared library:

```kotlin
plugins {
    id("org.jetbrains.kotlin.multiplatform") version "2.1.10"
    id("com.google.devtools.ksp") version "2.1.10-1.0.29"
    id("ru.auroraos.kmp.qtbindings") version "0.1.0"
}

kotlin {
    val linuxTargets = listOf(linuxX64(), linuxArm64())

    linuxTargets.forEach { it ->
        it.binaries {
            staticLib("shared")
        }
    }
}

qtBindings {
    libName = "shared"
}
```

**Note**: The `libName` **must match** the name of the compiled static or
dynamic library for the linux target.

## Usage

Annotation `QtExport` can be applied to the top-level classes or function
to create Qt bindings for them.

* Export top-level Kotlin function

```kotlin
import ru.auroraos.kmp.qtbindings.QtExport

@QtExport
fun helloWorld() = println("Hello, World!")
```

<details><summary>Generated Qt function</summary>
<p>

The QtBindings plugin will generate this Qt function:

```cpp
void helloWorld();
```

</p>
</details>

* Export top-level Kotlin class

```kotlin
import ru.auroraos.kmp.qtbindings.QtExport

@QtExport
class Calculator() {
    fun sum(x: Int, y: Int) : Int = x + y
    fun multiply(x: Int, y: Int) : Int = x * y
}
```

<details><summary>Generated Qt class</summary>
<p>

The QtBindings plugin will generate this Qt class:

```cpp
class Calculator
{
public:
    Calculator();
    Calculator(Calculator &&other);
    Calculator(libshared_kref_ru_auroraos_example_Calculator ptr);
    ~Calculator();

    Calculator &operator=(Calculator &&other);

    int sum(int x, int y) const;
    int multiply(int x, int y) const;

    libshared_kref_ru_auroraos_example_Calculator unsafeKotlinPointer() const;
private:
    libshared_kref_ru_auroraos_example_Calculator d_ptr;
};
```

</p>
</details>

* Export suspend function

```kotlin
import kotlinx.coroutines.delay
import ru.auroraos.kmp.qtbindings.QtExport

@QtExport
suspend fun calculateAsync(x: Int, y: Int) : Int {
    delay(1000)
    return x + y
}
```

<details><summary>Generated Qt function that returns QFuture</summary>
<p>

The QtBindings plugin will generate this Qt function:

```cpp
QFuture<int> calculateAsync(int x, int y);
```

</p>
</details>

**Note**: Generated Qt bindings (`cpp` and `hpp` files) are located in
`build/generated/ksp/[linuxTargetName]/[linuxMainSourceSetName]/resources`

**Do not forget** to add the compiled C library for the Linux target to the Qt project
to make the Qt bindings work.

Ways to use Qt bindings in a Qt project:

* Compile static or shared library from the generated Qt bindings and add it to the project
* Add the generated Qt binding source code to the project's build system (qmake, cmake, meson)

## Mappings

| Kotlin                                         | Qt (C++)                                                                 |
|------------------------------------------------|--------------------------------------------------------------------------|
| Primitives: `Unit`, `Bool`, `Int`, `Long`, etc | `void`, `bool`, `int`, `long long`, etc                                  |
| `String`                                       | `QString`                                                                |
| `List`, `MutableList`                          | `QList`                                                                  |
| `suspend` functions (coroutines)               | Functions that returns `QFuture`                                         |
| Public top-level Kotlin function               | Public top-level Qt (C++) function                                       |
| Public top-level Kotlin class                  | Public top-level Qt (C++) class                                          |
| `data` class                                   | Qt (C++) class with copy constructor, copy operator, getters and setters |

## Known limitations

* Qt collections (`QList`) are copies of Kotlin ones. You need create a setter
or special kotlin function that updates Kotlin list.

```kotlin
import ru.auroraos.kmp.qtbindings.QtExport

@QtExport
data class Example(val example: MutableList<String>)
```

For example, generated code for the Kotlin `Example` class:

```cpp
class Example
{
public:
    Example(const QList<QString> &example);
    Example(const Example &other);
    Example(Example &&other);
    Example(libshared_kref_ru_auroraos_example_Example ptr);
    ~Example();

    Example &operator=(const Example &other);
    Example &operator=(Example &&other);

    QList<QString> getExample() const; // Returns copy of the Kotlin list
    void setExample(const QList<QString> &set); // Change Kotlin list

    libshared_kref_ru_auroraos_example_Example unsafeKotlinPointer() const;
private:
    libshared_kref_ru_auroraos_example_Example d_ptr;
};
```

`getExample` function will return **COPY** of Kotlin `MutableList`.
You need to use `setExample` function to change its value in the
`Example` class in the Kotlin side. After you have changed the value
of the list using `setExample`, you will get the updated value in the
`getExample` function.

* Nested classes are not supported yet
* Objects are not supported yet
* Enums are not supported yet

## How to build and publish locally

Prerequisites:

* JDK 11

```kotlin
./gradlew publishToMavenLocal
```