#ifndef POPULATEDEVICETHREAD_H#define POPULATEDEVICETHREAD_H#include <QThread>class DeviceModel;class PopulateDeviceThread : public QThread{ Q_OBJECTpublic: PopulateDeviceThread(QObject *parent = 0); ~PopulateDeviceThread(); DeviceModel *model(); void selectData(const QString &filter = "");protected: void run();signals: void done(bool);private: DeviceModel *devModel;};#endif // POPULATEDEVICETHREAD_H
#include <QSqlDatabase>#include "headers/populatedevicethread.h"#include "headers/devicemodel.h"PopulateDeviceThread::PopulateDeviceThread(QObject *parent) : QThread(parent){ devModel = new DeviceModel(); devModel->moveToThread(this);}PopulateDeviceThread::~PopulateDeviceThread(){}void PopulateDeviceThread::run(){ QSqlDatabase db = QSqlDatabase::database(); if(!db.isOpen()){ qDebug()<<"DB not open!!!"; emit done(false); }else{ devModel->select(); emit done(true); }}DeviceModel *PopulateDeviceThread::model(){ return devModel;}void PopulateDeviceThread::selectData(const QString &filter){ devModel->setFilter(filter); this->start();}