Thread function is not getting called, confused on moveToThread()
-
I have a model for my QListView. I am moving the model to a new thread and my understanding is by doing so, all the operations I call on this object will be called in that thread, is that right?
@MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
xmlFileModel = new StandardItemModelEx(this);xmlFileModel->moveToThread( &thread); QObject::connect(&thread, SIGNAL(started()), xmlFileModel, SLOT(onStarted())); QObject::connect(&thread, SIGNAL(finished()), xmlFileModel, SLOT(onFinished())); thread.start(); ui->setupUi(this);
}@
In above StandardItemModelEx is my class derived from QStandardItemModel. Now I do thread.start() I expect onStarted() to be call but it never does.
I also have a search function in this class which can take sometime so that's why I put it in this class. I think since I moved this model object in a seperate thread (moveToThread()), any function I call through that pointer will be called in that thread? However when I call this function, it appears to be running in the same GUI thread. Here is the function and I call it in On_TextChange() of one of the edit box.
@int StandardItemModelEx::searchXmlFiles(QString rootDir)
{
clear();
int count = 0;QString xmlFilePath; QDirIterator iter( rootDir, QStringList() << "*.xml", QDir::Files | QDir::NoDotAndDotDot, QDirIterator::Subdirectories); QDir dir( rootDir ); while(iter.hasNext() ) { qDebug() << iter.next(); xmlFilePath = iter.filePath(); QStandardItem * item = new QStandardItem( QIcon(":/icons/Progeny.ico") , dir.relativeFilePath( xmlFilePath) ); appendRow( item ); count++; } return count;
}@
-
Hi,
[quote author="xalam" date="1424989955"]I am moving the model to a new thread and my understanding is by doing so, all the operations I call on this object will be called in that thread, is that right?[/quote]No. moveToThread() only changes the "thread affinity":http://doc.qt.io/qt-5/qobject.html#thread-affinity of the object, so queued signals and events are handled in the new thread. If you make a direct function call, then the function will still run in the thread that called the function.
Unfortunately, you cannot move a model to another thread. The views expect them to remain in the GUI thread. You should have seen an error message in your console output when you tried this.
What you can do is let the model spawn another thread internally to handle searches.
-
Thanks but atleast onStarted() should have been called when I start the thread. I noticed an error when I run:
@QObject::connect: No such slot QStandardItemModel::onStarted() in C:\code\Progeny2Spc\mainwindow.cpp:22
QObject::connect: No such slot QStandardItemModel::onFinished() in C:\code\Progeny2Spc\mainwindow.cpp:23@This is interesting because xmlFileModel is actually StandardItemModelEx (derived from QStandardItemModel). Is there a particular of deriving from model class that it is not recognizing it?
My class definition is followlng:
@#include <QStandardItemModel>
class StandardItemModelEx : public QStandardItemModel
{
public:
StandardItemModelEx(QObject * parent = 0);
~StandardItemModelEx();int searchXmlFiles(QString rootDir);
public slots:
void onStarted();
void onFinished();
};@ -
If you implement your own slots, then you need to add the Q_OBJECT macro (http://doc.qt.io/qt-5/qobject.html#Q_OBJECT ) and run qmake. Otherwise, your slot doesn't get registered with Qt.