调用单独的android线程时,emit不工作

时间:2014-05-29 13:45:55

标签: java c++ multithreading qt qml

抱歉我的英文。

有一个代码在Android上运行在一个单独的线程(Java代码)上,如下所示:

public class ClientThread extends Thread 
{
    public void run()
    {
        MainActivity.nativeMethod(); // not working
    }
}

public class MainActivity extends QtActivity
{
    private static final String TAG = "MainActivity";
    private static MainActivity _instance = null;
    private static ClientThread _clientThread = null;

    public MainActivity()
    {
        _instance = this;
    }

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        nativeMethod(); // working

        _clientThread = new ClientThread();
        _clientThread.start();

    }

    public static native void nativeMethod();
}

并且有C ++桥梁:

class TestObject : public QObject
{
    Q_OBJECT

public:
    explicit TestObject(QObject *parent = 0);
    static TestObject *getInstance();

Q_SIGNALS:
    void nativeMethod();

public Q_SLOTS:

};

static void nativeMethod(JNIEnv *, jclass)
{
        qDebug() << "works threaded or non-threaded calls But not below on threaded call.";
    emit TestObject::getInstance()->nativeMethod();
}

也是QML连接:

import TestObjectPackage 1.0

Connections
{
    target: TestObjectPackage.testObjectInstance
    onNativeMethod:
    {
        console.log("QML received signal!");
    }
}

jniNativeMethod每次调用但在单独的线程上不发送信号给QML。你能帮帮我吗?

1 个答案:

答案 0 :(得分:1)

Qt可以通过不同的线程发送信号。它确实记录哪个对象“属于”哪个线程和信号相应地传递。

但是,您需要确保接收线程实际处理传入信号。这是通过在接收线程中安装Qt主循环或手动查询Qt队列。

有关所有详细信息,请参阅http://qt-project.org/wiki/ThreadsEventsQObjects