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. Thread function is not getting called, confused on moveToThread()

Thread function is not getting called, confused on moveToThread()

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.2k 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.
  • X Offline
    X Offline
    xalam
    wrote on last edited by
    #1

    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;
    

    }@

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      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.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

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

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

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          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.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          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