You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过QtAndroid::androidActivity().callObjectMethod获取int值?

Solution for Retrieving int/jint from Android Activity in Qt 5.12

Let’s fix that compilation error right away—you just picked the wrong QAndroidJniObject method for primitive data types!

The core issue here is mixing up methods designed for object types vs primitive types:

  • callObjectMethod<T> is exclusively for Java methods that return objects (like jstring, custom class instances, etc.). Since int/jint are primitive data types in Java, this method throws a compile error because it expects to wrap an object, not a raw primitive value.
  • For primitive return types such as jint, you need to use the callMethod<T> method instead.

Correct Code Implementation

Here’s the proper way to call your Java method that returns an int:

// Call the Java method that returns a jint
jint my_jint = QtAndroid::androidActivity().callMethod<jint>("someMethod");
// Convert to Qt's native int type (safe, since jint maps directly to int on most platforms)
int my_int = static_cast<int>(my_jint);

Handling Methods With Parameters

If your someMethod accepts arguments, you’ll need to pass the method signature and parameters just like you would with object methods. For example, if your Java method looks like this:

public int someMethod(String input) {
    // Your logic here
    return 42;
}

Your Qt code would look like this:

jint result = QtAndroid::androidActivity().callMethod<jint>(
    "someMethod",
    "(Ljava/lang/String;)I",  // Method signature: takes a String, returns int
    QAndroidJniObject::fromString("Hello from Qt").object<jstring>()
);

Quick Notes

  • This API behavior is identical across Qt 5.12 Commercial and Open Source editions—no special differences here.
  • jint is just a JNI typedef for Java’s int, so casting it to Qt’s native int is straightforward and safe for all standard Android target architectures.

内容的提问来源于stack exchange,提问作者AdeleGoldberg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 16:37:44