Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Emit is not working when calling on a separate android thread

Emit is not working when calling on a separate android thread

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nickolas
    wrote on last edited by
    #1

    Hey. Sorry for my english.

    There is a code that running on android on a separate Thread (Java Code) like this:

    @
    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();
    

    }@

    And there is C++ bridge for that:

    @class TestObject : public QObject
    {
    Q_OBJECT

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

    Q_SIGNALS:
    void nativeMethod();

    public Q_SLOTS:

    };

    static void jniNativeMethod(JNIEnv *, jclass)
    {
    qDebug() << "works threaded or non-threaded calls"
    emit TestObject::getInstance()->nativeMethod();
    }@

    Also a QML connection:

    @import TestObjectPackage 1.0

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

    jniNativeMethod calls every time but emit not sending signal to QML on sepearate thread. Could you help me?

    1 Reply Last reply
    0
    • jeremy_kJ Online
      jeremy_kJ Online
      jeremy_k
      wrote on last edited by
      #2

      I wonder if the problem is due to the use of a static signal.

      Cross-thread signal delivery uses an event loop in the receiving thread. Intra-thread delivery is a direct call by default. The static use might confuse the detection of the correct connection type.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved