编程语言
首页 > 编程语言> > java-如何混合使用C Qt对象和Qt Jambi对象

java-如何混合使用C Qt对象和Qt Jambi对象

作者:互联网

我正在尝试将一些用C编写的现有Qt代码与一些使用Qt Jambi用Java编写的代码结合起来,但是我不太确定该怎么做.我基本上是在尝试两件事:

>使用JNI将QObject从C传递到Java
>将Qt Jambi QObject从Java传递到C

看来我可以直接传递指针,然后将其包装在Java端的QNativePointer中,但是我不知道如何将QNativePointer变回由Qt Jambi包装的原始对象.

例如:我可以将QWidget *传递给Java,然后再用Java创建一个QNativePointer,但是我如何才能以此构造QWidget? QJambiObject和QObject似乎没有“ setNativePointer”方法,我不确定如何将其转换.

在C中:

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

在Java中:

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

        QWidget widget = ...

谢谢!

解决方法:

对于其他查看此内容的人,请查看以下内容:
http://labs.trolltech.com/blogs/2007/08/24/extremely-interesting-jambi-trick-x-instantiating-java-widgets-from-c/

特别是这部分:

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.

标签:qt4,qt-jambi,java,c-4
来源: https://codeday.me/bug/20191107/2002116.html