Convert jstring to QString
Asked Answered
U

2

7

I'm calling a Java function that returns a string:

QAndroidJniObject obj = QAndroidJniObject::callStaticObjectMethod<jstring>("HelloJava", "getString");
jstring jstr = obj.object<jstring>();
QString str = jstr; // This doesn't work, obviously, compiler-error.

And it returns a jstring, which is not very useful for me. How do I convert that to a QString, so I can use it in my code?

Ullund answered 7/12, 2014 at 17:33 Comment(0)
F
8

You need to use this method.

QString QAndroidJniObject::toString() const

Returns a QString with a string representation of the java object. Calling this function on a Java String object is a convenient way of getting the actual string data.

So, I would write this if I were you:

QAndroidJniObject string = QAndroidJniObject::callStaticObjectMethod<jstring>("HelloJava", "getString");

QString qstring = string.toString();
Fluecure answered 7/12, 2014 at 17:41 Comment(0)
D
2

for converting jstring to QString you can use following lines:

static void onContactSelected(JNIEnv * env, jobject /*obj*/, jstring number)
{
    QString qstr(env->GetStringUTFChars(number, 0));
    /* .... some codes .... */
}

or in simple:

JNIEnv* env;
QString qstr(env->GetStringUTFChars(number, 0));

Source

Doublecross answered 8/3, 2016 at 13:7 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.