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



  • 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();
    }
    @



  • 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]



  • How do I do this in Qt 4.8?



  • How do I do this in Qt 4.8?


  • Lifetime Qt Champion

    Hi,

    Exactly the same


  • Lifetime Qt Champion

    Hi,

    Exactly the same



  • Thanks for the answer Sam!



  • Thanks for the answer Sam!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.