如何混合 C++ Qt 对象和 Qt Jambi 对象
我正在尝试使用 Qt Jambi 将一些用 C++ 编写的现有 Qt 代码与一些用 Java 编写的代码结合起来,但我不太确定该怎么做。我基本上试图实现两件事:
- 使用 JNI 将 QObject 从 C++ 传递到 Java
- 将 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:
- Pass a QObject from C++ to Java using JNI
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为这是可能的,你正在努力实现这一目标。如果我正确理解你的方法,你会以某种方式尝试将 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.
对于其他正在查看此内容的人,请查看以下内容:
http://labs.trolltech.com/blogs/2007/08/24/extremely-interesting-jambi-trick-x-instantiating-java-widgets-from-c/
特别是这部分:
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: