Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to run Android app in different thread?



  • Hi,

    I am trying to start my Qt/QML Android app in a different thread than the one main function runs in. The reason is that I want it to run in the same thread as a library that I use.
    For testing I simply renamed my main function to main2 and reimplement main like this:

    int main( int argc, char* argv[])
    {
        QtConcurrent::run(main2, argc, argv);
        while (!global_quit) QThread::msleep(1000);
        return 0;
    }
    

    With this the app starts normally but it will not react to any input.

    I have read in other forum posts that it is allowed to create and run QCoreApplication in a different thread. So what am I doing wrong here?

    Thanks a lot.


  • Lifetime Qt Champion

    @Andreas-E. Why not simply use the library in your GUI thread?
    I mean: why do you use the library in another thread?
    Don't forget: it is not allowed to access GUI classes from different threads!
    Usually you do not move UI to another thread but background tasks.



  • @jsulm The library is running in a service, so it may already exist in its own thread before the activity starts. I am not trying to use GUI classes from different threads, everything is supposed to run in the same thread, just not in the thread Qt creates to run the main function in.


  • Moderators

    @Andreas-E. said in How to run Android app in different thread?:

    The library is running in a service, so it may already exist in its own thread before the activity starts.

    which means it also runs in a different process, so inaccessible for you.
    I can't think of anything you want to achieve...



  • @raven-worx It is not a QtService, it is implemented in Java. Therefore the service and activity share the same process and main thread by default. However, Qt starts a different thread to run the main function in, this is where my problem starts. Being able to create QApplication in a different thread might solve the problem, but it does not seem to work correctly on Android.



  • No Need to create main thread again, create worker thread
    Simply move that class to thread;

    use moveToThread(&class reference);
    &&
    Initialize same class instance in constructor. then your task runs Parallel.


Log in to reply