如何混合 C++ Qt 对象和 Qt Jambi 对象

发布于 2024-08-15 12:05:50 字数 912 浏览 12 评论 0原文

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

  1. 使用 JNI 将 QObject 从 C++ 传递到 Java
  2. 将 Qt Jambi QObject 从 Java 传递到 C++

看起来我可以直接传递指针,然后将其包装在 Java 端的 QNativePointer 中,但我无法弄清楚如何将 QNativePointer 转回由 Qt Jambi 包装的原始对象。

例如:我可以将 QWidget* 作为 long 传递给 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 = ...

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请远离我 2024-08-22 12:05:50

我不认为这是可能的,你正在努力实现这一目标。如果我正确理解你的方法,你会以某种方式尝试将 C++ QWidget 对象的内存表示转换为 Java QWidget 对象。只有当 Java 和 C++ 对象的内存表示相同时,这才有效,对此我深表怀疑。

即使是这种情况,它也可能不起作用,因为我非常确定 QWidget 类的 QtJambi 版本不是 C++ QWidget 类的一对一克隆。

为此,您必须以某种方式读取 C++ QWidget 的值,创建 QtJambi QWidget 对象,然后将这些值设置为新的 QtJambi 小部件。我不知道可以完成这项工作的转换方法,我也不确定这是否可能。

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.

奶气 2024-08-22 12:05:50

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

特别是这部分:

qtjambi_from_QWidget() 调用将
要么创建一个新的 Java 小部件,如果
父窗口小部件是用 C++ 创建的,或者
它将返回现有的Java
对象(如果父对象是在以下位置创建的)
爪哇。如果必须创建一个新的java
对象, this 的类型将是
Qt 已知的最接近的 Java 超类型
占碑。如果您已经映射了自己的 C++
小部件并希望正确使用它们
在诸如此类的电话中,您必须
确保初始化代码
您生成的库被称为prior
到转换发生。还
请注意,在 qtjambi_core.h 中,您将
找几个其他方便的
可以使用的转换函数
在 C++ 之间来回转换
和JNI,以及其他方便的,
基于 JNI 的代码。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文