开发者

Creating a QMainWindow from Java using JNI

I'm trying to create a Qt main windows from Java 开发者_开发知识库using JNI directly and I got a threading error.

My code looks like this:

Test class:

public class Test
{
    public static void main(String... args)
    {
        System.out.println(System.getProperty("java.library.path"));
        TestWindow f = new TestWindow();
        f.show();
    }
}

TestWindow class:

public class TestWindow
{
static { System.loadLibrary("mylib"); }

public native void show();
}

C++ impl:

void JNICALL Java_testpackage_TestWindow_show
  (JNIEnv *, jobject)
{
    int c = 0; char** a = NULL;
    QApplication* app = new QApplication(c, a);
    QMainWindow* mw = new QMainWindow();
    mw->setWindowTitle("Hello");
    mw->setGeometry(150, 150, 400, 300);
    mw->show();
    QApplication::exec();
}

and I get my window painted but frozen (it does not receive any event) and the following error message when instantiating the QMainWindow object:

QCoreApplication::sendPostedEvents: Cannot send posted events for objects in another thread

I know all the UI operations must done in the UI thread but in my example I created the QApplication in the only thread I have running, so, everything should work properly.

I did some tests executing the code of my "show" method from a QMetaObject::invokeMethod stuff using Qt::QueuedConnection but nothing works properly.

I know I could use Jambi... but I know that it could be done natively too and that is what I want to do :)

Any ideas on this? Thanks in advance!

Ernesto


Just a suggestion*: may be you should instantiate a native window in a separate thread?

public class Test
{
    public static void main(String... args)
    {
        System.out.println(System.getProperty("java.library.path"));
        new Thread(new Runnable() {
            public void run() {
                TestWindow f = new TestWindow();
                f.show();
            }
        }).start();
    }
}

*I've never programmed Java apps with native GUI. May be you should consider Swing or SWF instead?

Another suggestion is to try JNA instead of JNI: https://github.com/twall/jna/


The solution was very naïve:

I did not know that I needed to start the jvm with the parameter:

-XstartOnFirstThread

I did it and everything worked properly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜