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?
Forum Updated to NodeBB v4.3 + New Features

How to run Android app in different thread?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
6 Posts 4 Posters 1.3k Views 2 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.
  • A Offline
    A Offline
    Andreas E.
    wrote on 25 Jul 2018, 09:31 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.

    J 1 Reply Last reply 25 Jul 2018, 10:17
    0
    • A Andreas E.
      25 Jul 2018, 09:31

      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.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 25 Jul 2018, 10:17 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 25 Jul 2018, 10:27
      2
      • J jsulm
        25 Jul 2018, 10:17

        @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 25 Jul 2018, 10:27 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.

        R 1 Reply Last reply 25 Jul 2018, 11:06
        0
        • A Andreas E.
          25 Jul 2018, 10:27

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

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 25 Jul 2018, 11:06 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 25 Jul 2018, 11:23
          2
          • R raven-worx
            25 Jul 2018, 11:06

            @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 25 Jul 2018, 11:23 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
            • N Offline
              N Offline
              Narasimha Reddy MV
              wrote on 26 Jul 2018, 05:36 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

              1/6

              25 Jul 2018, 09:31

              • Login

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