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. using a thread for running a function
Forum Updated to NodeBB v4.3 + New Features

using a thread for running a function

Scheduled Pinned Locked Moved Solved Mobile and Embedded
9 Posts 5 Posters 833 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.
  • B Offline
    B Offline
    benamiar
    wrote on last edited by
    #1

    hello guys again,

    i'm trying to use a thread to run a function with Argument, the function is on a big class that interact with several display i need to use the thread only for one or 2 function.

    so i don't wont to use my thread for all the class.

    the function is called when we push enterbutton so i think i should connect the thread to my function after the pushbutton
    so how can we do to use the thread just for a function not all the class ?

    i saw that some people are using moveToThread but they are connecting it to a class perhaps i didn't undertood well what they doing so need you help please

    bool copyPath(QString sourceDir, QString destinationDir, bool overWriteDirectory)
    {
    // using thread for this function
    // thread start
    ...........
    
    if(condition==true)
    // thread stoped ; retrun true 
    
    }
    
    

    thanks for help :)

    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • B benamiar

      hello guys again,

      i'm trying to use a thread to run a function with Argument, the function is on a big class that interact with several display i need to use the thread only for one or 2 function.

      so i don't wont to use my thread for all the class.

      the function is called when we push enterbutton so i think i should connect the thread to my function after the pushbutton
      so how can we do to use the thread just for a function not all the class ?

      i saw that some people are using moveToThread but they are connecting it to a class perhaps i didn't undertood well what they doing so need you help please

      bool copyPath(QString sourceDir, QString destinationDir, bool overWriteDirectory)
      {
      // using thread for this function
      // thread start
      ...........
      
      if(condition==true)
      // thread stoped ; retrun true 
      
      }
      
      

      thanks for help :)

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

      @benamiar Take a look at https://doc.qt.io/qt-5/qtconcurrentrun.html

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

      R 1 Reply Last reply
      3
      • B benamiar

        hello guys again,

        i'm trying to use a thread to run a function with Argument, the function is on a big class that interact with several display i need to use the thread only for one or 2 function.

        so i don't wont to use my thread for all the class.

        the function is called when we push enterbutton so i think i should connect the thread to my function after the pushbutton
        so how can we do to use the thread just for a function not all the class ?

        i saw that some people are using moveToThread but they are connecting it to a class perhaps i didn't undertood well what they doing so need you help please

        bool copyPath(QString sourceDir, QString destinationDir, bool overWriteDirectory)
        {
        // using thread for this function
        // thread start
        ...........
        
        if(condition==true)
        // thread stoped ; retrun true 
        
        }
        
        

        thanks for help :)

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @benamiar like @jsulm suggest QtConcurrent seems like the tool you need for your situation.

        Just make sure that you do not use member variables inside your function, that you want to run concurrently!


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        1
        • B Offline
          B Offline
          benamiar
          wrote on last edited by benamiar
          #4

          i did that and i got an erreur wish speak about a non static function memebr

            QFuture<bool> future = QtConcurrent::run(copyPath,pathSrc,pathDes,false);
          
          

          erreur : invalid use of non-static member function
          QFuture<bool> future = QtConcurrent::run(copyPath,pathSrc, pathDes, false);

          whats wrong with that line ?
          ^ ^

          J.HilkJ 1 Reply Last reply
          0
          • K Offline
            K Offline
            kuzulis
            Qt Champions 2020
            wrote on last edited by kuzulis
            #5

            @benamiar said in using a thread for running a function:

            copyPath

            Your copyPath() function should be static (I think).

            B 1 Reply Last reply
            0
            • B benamiar

              i did that and i got an erreur wish speak about a non static function memebr

                QFuture<bool> future = QtConcurrent::run(copyPath,pathSrc,pathDes,false);
              
              

              erreur : invalid use of non-static member function
              QFuture<bool> future = QtConcurrent::run(copyPath,pathSrc, pathDes, false);

              whats wrong with that line ?
              ^ ^

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @benamiar said in using a thread for running a function:

              QFuture<bool> future = QtConcurrent::run(copyPath,pathSrc,pathDes,false);

              IIRC run wants a function pointer?

              QFuture<bool> future = QtConcurrent::run(this, &myClass::copyPath,pathSrc,pathDes,false);
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              B 1 Reply Last reply
              1
              • K kuzulis

                @benamiar said in using a thread for running a function:

                copyPath

                Your copyPath() function should be static (I think).

                B Offline
                B Offline
                benamiar
                wrote on last edited by
                #7

                @kuzulis tnx for your reply but the problem is that i can't see any utility or gain in making my function static but in doing that the run is working :)

                1 Reply Last reply
                0
                • J.HilkJ J.Hilk

                  @benamiar said in using a thread for running a function:

                  QFuture<bool> future = QtConcurrent::run(copyPath,pathSrc,pathDes,false);

                  IIRC run wants a function pointer?

                  QFuture<bool> future = QtConcurrent::run(this, &myClass::copyPath,pathSrc,pathDes,false);
                  
                  B Offline
                  B Offline
                  benamiar
                  wrote on last edited by
                  #8

                  @J.Hilk thanks for your help i didn't see the result yet but seems working thank you all :D

                  1 Reply Last reply
                  2
                  • jsulmJ jsulm

                    @benamiar Take a look at https://doc.qt.io/qt-5/qtconcurrentrun.html

                    R Offline
                    R Offline
                    Rodolfo_TT
                    Banned
                    wrote on last edited by
                    #9
                    This post is deleted!
                    1 Reply Last reply
                    -1

                    • Login

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