Question

I'm trying to combine some existing Qt code written in C++ with some code written in Java using Qt Jambi, but I'm not quite sure how to do it. I'm basically trying to acieve two things:

  1. Pass a QObject from C++ to Java using JNI
  2. Pass a Qt Jambi QObject from Java to C++

It looks like I can pass the pointer directly and then wrap it in QNativePointer on the Java side, but I can't figure out how to turn a QNativePointer back into the original object, wrapped by Qt Jambi.

Eg: I can pass a QWidget* as a long to Java and then create a QNativePointer in Java, but how can I then construct a QWidget out of this? QJambiObject and QObject dont seem to have a "setNativePointer" method and I'm not sure how to convert it.

In C++:

QWidget* widget = ...
jclass cls = env->FindClass("Test");
jmethodID mid = env->GetStaticMethodID(cls, "test", "(I)V");
env->CallStaticVoidMethod(cls, mid, int(widget));

In Java:

public class Test {
    public static void test (int ptr) {
        QNativePointer pointer = new QNativePointer(QNativePointer.Type.Int);
        pointer.setIntValue(ptr);

        QWidget widget = ...

Thanks!

Was it helpful?

Solution

For other people looking at this, check this out: http://labs.trolltech.com/blogs/2007/08/24/extremely-interesting-jambi-trick-x-instantiating-java-widgets-from-c/

Especially this part:

The qtjambi_from_QWidget() call will either create a new Java widget if the parent widget was created in C++, or it will return the existing Java object if the parent was created in Java. If it has to create a new java object, the type of this will be the closest Java supertype known to Qt Jambi. If you have mapped your own C++ widgets and want to use them correctly in calls such as these, you have to make sure the initialization code of your generated library is called prior to the conversion takes place. Also note that in qtjambi_core.h you will find several other convenient conversion functions that can be used to convert back and forth between C++ and JNI, as well as other convenient, JNI-based code.

OTHER TIPS

I wouldn't expect that this is possible in the way, you are trying to achieve this. If I understand your approach correctly, you are somehow trying to cast the in-memory representation of a C++ QWidget object to a Java QWidget object. This could only work if the in-memory representation of Java and C++ objects would be the same, which I doubt seriously.

Even if that would be the case, it wouldn't probably work either, because I am pretty sure, that the QtJambi version of the QWidget class isn't a one to one clone of the C++ QWidget class.

For this to work, you would have to somehow read the values of the C++ QWidget, create a QtJambi QWidget object and then set these values to the new QtJambi widget. I am not aware of a conversion method, that would do this job, nor am I sure if that is possible at all.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top