Qt
6.x
The Qt SDK
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
5
void
functionScope
()
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
19
static
void
fromJavaOne
(JNIEnv *env, jobject thiz, jint
x
)
20
{
21
Q_UNUSED
(env);
22
Q_UNUSED
(thiz);
23
qDebug
() <<
x
<<
"< 100"
;
24
}
25
26
static
void
fromJavaTwo
(JNIEnv *env, jobject thiz, jint
x
)
27
{
28
Q_UNUSED
(env);
29
Q_UNUSED
(thiz);
30
qDebug
() <<
x
<<
">= 100"
;
31
}
32
33
void
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
)}};
39
QJniEnvironment
env;
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
49
class
FooJavaClass
50
{
51
public
static
void
foo
(
int
x
)
52
{
53
if
(
x
< 100)
54
callNativeOne(
x
);
55
else
56
callNativeTwo(
x
);
57
}
58
59
private
static
native
void
callNativeOne(
int
x
);
60
private
static
native
void
callNativeTwo(
int
x
);
61
62
}
methods
static JNINativeMethod methods[]
Definition
androidjnimain.cpp:800
FooJavaClass
[C++ native methods]
Definition
src_qjniobject.cpp:50
QJniEnvironment
\inmodule QtCore
QJniObject
\inmodule QtCore
QString
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition
qstring.h:127
qDebug
#define qDebug
[1]
Definition
qlogging.h:160
x
GLint GLint GLint GLint GLint x
[0]
Definition
qopengles2ext.h:605
Q_UNUSED
#define Q_UNUSED(x)
Definition
qtpreprocessorsupport.h:20
functionScope
void functionScope()
[QJniObject scope]
Definition
src_qjniobject.cpp:5
fromJavaOne
static void fromJavaOne(JNIEnv *env, jobject thiz, jint x)
[QJniObject scope]
Definition
src_qjniobject.cpp:19
fromJavaTwo
static void fromJavaTwo(JNIEnv *env, jobject thiz, jint x)
Definition
src_qjniobject.cpp:26
foo
void foo()
Definition
src_qjniobject.cpp:33
qtbase
src
corelib
doc
snippets
jni
src_qjniobject.cpp
Generated by
1.9.7