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] Executing method in different thread without creating new object?
Forum Updated to NodeBB v4.3 + New Features

[solved] Executing method in different thread without creating new object?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 5.6k Views 1 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.
  • S Offline
    S Offline
    shimano
    wrote on last edited by
    #1

    Hi guys,

    I wonder, is it possible to move only a method/slot to new thread but without creat new object?

    Let's suppose that I have an object MainWindow with method (slot) findFile(). I would like to execute findFile() after press a QPushButton (that's easy part). As I observed in some examples and documentation, the easiest way to put something into thread is to use moveToThread(), which, as I understand, should look like:

    @
    class MyClass {
    public slots:
    void findFile();
    };

    void MyClass::findFile() {
    // .. do a lot timewasting work
    }

    void MainWindow::threadingWork() {
    MyClass *myObject = new MyClass;
    QThread *searchingThread = new QThread(this);
    connect(searchingThread, SIGNAL(started()), this, SLOT(findFile()));
    myObject->moveToThread(searchingThread);
    searchingThread->start();
    }
    @

    But, I would like just execute a findFile(), I don't need to create myObject, so I would like to code like:

    @
    class MainWindow {
    // code needed to create GUI
    public slots:
    void findFile();
    };

    void MainWindow::findFile() {
    // .. do a lot timewasting work
    }

    void MainWindow::threadingWork() {
    QThread *searchingThread = new QThread(this);
    connect(searchingThread, SIGNAL(started()), this, SLOT(findFile()));
    moveToThread(searchingThread);
    searchingThread->start();
    }
    @

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tchoninho
      wrote on last edited by
      #2

      You can use QConcurrent::run().
      QConcurrent framework allows to run a global method in parallel mode.

      That example was copied from Assistant.

      [code]
      void someFunction(int arg1, double arg2);
      QFuture<void> future = QtConcurrent::run(boost::bind(someFunction, 1, 2.0));
      ...
      [/code]

      Computer Scientist
      Belo Horizonte - Brasil
      C++ Development
      Qt Development

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kumararajas
        wrote on last edited by
        #3

        How do I do this in Qt 4.8?

        --Kumar

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kumararajas
          wrote on last edited by
          #4

          How do I do this in Qt 4.8?

          --Kumar

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

            Hi,

            Exactly the same

            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
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              Exactly the same

              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
              • K Offline
                K Offline
                kumararajas
                wrote on last edited by
                #7

                Thanks for the answer Sam!

                --Kumar

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  kumararajas
                  wrote on last edited by
                  #8

                  Thanks for the answer Sam!

                  --Kumar

                  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