Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to run Android app in different thread?
QtWS25 Last Chance

How to run Android app in different thread?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
6 Posts 4 Posters 1.2k Views
  • 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.
  • A Offline
    A Offline
    Andreas E.
    wrote on last edited by
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • A Andreas E.

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      2
      • jsulmJ jsulm

        @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.

        A Offline
        A Offline
        Andreas E.
        wrote on last edited by
        #3

        @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.

        raven-worxR 1 Reply Last reply
        0
        • A Andreas E.

          @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.

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @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...

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          A 1 Reply Last reply
          2
          • raven-worxR raven-worx

            @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...

            A Offline
            A Offline
            Andreas E.
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            0
            • Narasimha Reddy MVN Offline
              Narasimha Reddy MVN Offline
              Narasimha Reddy MV
              wrote on last edited by Narasimha Reddy MV
              #6

              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.

              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