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