Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
src_qjniobject.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
6{
7 QString helloString("Hello");
8 jstring myJString = 0;
9 {
10 QJniObject string = QJniObject::fromString(helloString);
11 myJString = string.object<jstring>();
12 }
13
14 // Ops! myJString is no longer valid.
15}
17
19static void fromJavaOne(JNIEnv *env, jobject thiz, jint x)
20{
21 Q_UNUSED(env);
22 Q_UNUSED(thiz);
23 qDebug() << x << "< 100";
24}
25
26static void fromJavaTwo(JNIEnv *env, jobject thiz, jint x)
27{
28 Q_UNUSED(env);
29 Q_UNUSED(thiz);
30 qDebug() << x << ">= 100";
31}
32
33void foo()
34{
35 // register the native methods first, ideally it better be done with the app start
36 const JNINativeMethod methods[] =
37 {{"callNativeOne", "(I)V", reinterpret_cast<void *>(fromJavaOne)},
38 {"callNativeTwo", "(I)V", reinterpret_cast<void *>(fromJavaTwo)}};
40 env.registerNativeMethods("my/java/project/FooJavaClass", methods, 2);
41
42 // Call the java method which will calls back to the C++ functions
43 QJniObject::callStaticMethod<void>("my/java/project/FooJavaClass", "foo", "(I)V", 10); // Output: 10 < 100
44 QJniObject::callStaticMethod<void>("my/java/project/FooJavaClass", "foo", "(I)V", 100); // Output: 100 >= 100
45}
47
50{
51 public static void foo(int x)
52 {
53 if (x < 100)
54 callNativeOne(x);
55 else
56 callNativeTwo(x);
57 }
58
59private static native void callNativeOne(int x);
60private static native void callNativeTwo(int x);
61
62}
static JNINativeMethod methods[]
[C++ native methods]
static void foo(int x)
\inmodule QtCore
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
#define qDebug
[1]
Definition qlogging.h:164
GLint GLint GLint GLint GLint x
[0]
#define Q_UNUSED(x)
void functionScope()
[QJniObject scope]
static void fromJavaOne(JNIEnv *env, jobject thiz, jint x)
[QJniObject scope]
static void fromJavaTwo(JNIEnv *env, jobject thiz, jint x)
void foo()