Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] execute a slot with parameters inside a thread

[SOLVED] execute a slot with parameters inside a thread

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 3.5k 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.
  • X Offline
    X Offline
    XGuy
    wrote on last edited by
    #1

    Hi,
    i have a class with plenty of functions and slots and i want to run these slots in another thread!
    but the problem is i don't know how to pass parameters to my slots!
    here is a sample code:

    @
    class Conn
    {
    private slots:
    int getConn();
    int setConn(QString strPath);
    .
    .
    .
    };

    void MyClass::func1()
    {
    Conn objConn;
    QThread thrConn;
    thrConn.setObjectName("thrConn");
    objConn.moveToThread(&thrConn);
    QObject::connect(&thrConn, SIGNAL(started()), &objConn, SLOT(getConn())));
    thrConn.start();
    }
    @

    in above code I'm executing slot getConn() in another thread! so far so good. but how about another slot named setConn(QString strPath). how am i supposed to supply that parameter to the slot!
    right now I'm using public data members! but it sounds silly! i have loads of slots in that class and how many public data members am i supposed to declare! and also the return value is another problem, as long as slots can't return a value using QObject::Connect approach!
    any ideas how to deal with these two problems!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mmoll
      wrote on last edited by
      #2

      bq. in above code I’m executing slot getConn() in another thread!

      I don't think you are. Read http://qt-project.org/doc/qt-5.1/qtcore/threads-qobject.html#signals-and-slots-across-threads . The slot is executed in the thread that func1 is called in. Also, as a consequence, I think that getConn is called on an inexistent object.

      1 Reply Last reply
      0
      • X Offline
        X Offline
        XGuy
        wrote on last edited by
        #3

        [quote author="mmoll" date="1381245038"]bq. in above code I’m executing slot getConn() in another thread!

        I don't think you are. Read http://qt-project.org/doc/qt-5.1/qtcore/threads-qobject.html#signals-and-slots-across-threads . The slot is executed in the thread that func1 is called in. Also, as a consequence, I think that getConn is called on an inexistent object.[/quote]

        so in my case it is like Direct Connection !!
        and i should run func1 in a new thread so that getConn is also executed in a new thread. is this right?
        if not, would you guide me on how to run that slot in a new thread?
        btw, i really appreciate it if you could help me with the parameters and return value problems.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          One of the problem you have is that objCon and thrCon are going to be destroyed right after your called thrConn.start() so there's probably nothing that will be done.

          Use a signal with the "return" value as a parameter and emit it once the work is done. As for the rest, you should setup your worker object before calling start on the thread, this is normal way to do this.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • X Offline
            X Offline
            XGuy
            wrote on last edited by
            #5

            [quote author="SGaist" date="1381263180"]Hi,

            One of the problem you have is that objCon and thrCon are going to be destroyed right after your called thrConn.start() so there's probably nothing that will be done.

            Use a signal with the "return" value as a parameter and emit it once the work is done. As for the rest, you should setup your worker object before calling start on the thread, this is normal way to do this.[/quote]

            i still have some code to execute after thrConn.start() so they don't get destroyed!
            @
            void MyClass::func1()
            {
            Conn objConn;
            QThread thrConn;
            thrConn.setObjectName("thrConn");
            objConn.moveToThread(&thrConn);
            QObject::connect(&thrConn, SIGNAL(started()), &objConn, SLOT(getConn())));
            thrConn.start();
            if(thrConn.wait())
            {
            if(!thrConn.isFinished() | thrConn.isRunning())
            {
            thrConn.quit();
            }
            }
            }

            int Conn::getConn()
            {
            .
            .
            .

            if(this->thread())
            {
            if(this->thread()->objectName() == "thrConn")
            {
            this->thread()->quit();
            }
            }
            }
            @
            as i start waiting on the thread, getConn slot get executed and at the end of it i quit thrConn, therefore i suppose getConn is running in thrConn.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You should have posted that code earlier. Since you are waiting on the thread to end its work, why not just call the function directly ? The call to wait will block your calling thread (probably the main thread)

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • X Offline
                X Offline
                XGuy
                wrote on last edited by
                #7

                [quote author="SGaist" date="1381310836"]You should have posted that code earlier. Since you are waiting on the thread to end its work, why not just call the function directly ? The call to wait will block your calling thread (probably the main thread)[/quote]

                you mean this way?
                @void MyClass::func1()
                {
                Conn objConn;
                QThread thrConn;
                thrConn.setObjectName("thrConn");
                objConn.moveToThread(&thrConn);
                thrConn.start();
                objConn.getConn();
                @

                yes you are right! i shouldn't wait on the thread because doing so, blocks the main thread and things don't happen synchronously :-)

                if this way of function calls across threads are okay, then my problem with function return value and parameters are also solved!
                i have read somewhere that we should use signal-slots approach when working with threads because it is thread safe in nature!

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  No not this way, your thread would still not make sense.

                  Anyway, you need to allocate both your objConn and thrConn on the heap so they won't get destroyed at the end of func1. Also connect the finished signal to the deleteLater slots of both objConn and thrConn.

                  There are several articles you can found about this pattern

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  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