Creating a QMainWindow from Java using JNI

Posted by ebasconp on Stack Overflow See other posts from Stack Overflow or by ebasconp
Published on 2010-05-17T05:01:40Z Indexed on 2010/05/17 5:10 UTC
Read the original article Hit count: 313

Filed under:
|
|

Hi everybody:

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

© Stack Overflow or respective owner

Related posts about java

Related posts about qt