.hclass ContentRefresher : public QThread{ Q_OBJECTpublic: ContentRefresher(QTableView *contentView); ~ContentRefresher(); void refreshContent();signals: void contentRefreshed();private: void run(); volatile bool m_abort; QTableView *m_contentView; QMutex m_mutex;};.cppContentRefresher::ContentRefresher(QTableView *contentView){ m_abort = false; m_contentView = contentView;}ContentRefresher::~ContentRefresher(){ if(!isRunning()) return; m_mutex.lock(); m_abort = true; m_mutex.unlock(); wait();}void ContentRefresher::refreshContent(){ start();}void ContentRefresher::run(){ ContentModel *model = qobject_cast<ContentModel *>(m_contentView->model()); model->refresh(); m_contentView->setColumnHidden(0,true); m_contentView->setColumnHidden(2,true); m_contentView->resizeColumnsToContents(); m_contentView->resizeRowsToContents(); m_contentView->horizontalHeader()->setStretchLastSection(true); emit contentRefreshed();}
CentralWidget::CentralWidget(QWidget *parent) : contentRefresher(0),........{ ..... contentModel = new ContentModel(this); m_ui.contentView->setModel(contentModel); m_ui.contentView->setSelectionBehavior(QAbstractItemView::SelectRows); m_ui.contentView->setSelectionMode(QAbstractItemView::SingleSelection); m_ui.contentView->verticalHeader()->hide(); m_ui.contentView->setItemDelegate(new ContentDelegate(m_ui.contentView)); m_ui.contentView->installEventFilter(this); ............. QTimer::singleShot(0, this, SLOT(refreshContentModel()));}CentralWidget::~CentralWidget(){ ....... delete contentRefresher;}void CentralWidget::refreshContentModel(){ contentRefresher = new ContentRefresher(m_ui.contentView); connect(contentRefresher, SIGNAL(contentRefreshed()), this, SLOT(contentRefreshed())); m_ui.progressWidget->setVisible(true); contentRefresher->refreshContent();}void CentralWidget::contentRefreshed(){ m_ui.progressWidget->setVisible(false);}
ContentRefresher::ContentRefresher(ContentModel *contentModel){ m_abort = false; m_contentModel = contentModel;}ContentRefresher::~ContentRefresher(){ if(!isRunning()) return; m_mutex.lock(); m_abort = true; m_mutex.unlock(); wait();}void ContentRefresher::refreshContent(){ start();}void ContentRefresher::run(){ while(!m_abort) { m_contentModel->refresh(); emit contentRefreshed(); } m_mutex.lock(); m_abort = false; m_mutex.unlock();}void ContentRefresher::stop() { m_mutex.lock(); m_abort = true; m_mutex.unlock();}[codе]вот его использование:[code]void CentralWidget::refreshContentModel(){ contentRefresher = new ContentRefresher(contentModel); connect(contentRefresher, SIGNAL(contentRefreshed()), this, SLOT(contentRefreshed()), Qt::QueuedConnection); m_ui.progressWidget->setVisible(true); m_ui.contentView->setVisible(false); contentRefresher->refreshContent();}void CentralWidget::contentRefreshed(){ contentRefresher->stop(); m_ui.contentView->setModel(contentModel); m_ui.contentView->setColumnHidden(0,true); m_ui.contentView->setColumnHidden(2,true); m_ui.contentView->resizeColumnsToContents(); m_ui.contentView->resizeRowsToContents(); m_ui.contentView->horizontalHeader()->setStretchLastSection(true); m_ui.contentView->setVisible(true); m_ui.progressWidget->setVisible(false);}
class ContentRefresher : public QThread{ Q_OBJECTpublic: ContentRefresher(QObject *parent = 0); ~ContentRefresher(); void processQuery(const QString &qStr, const QString &hStr);signals: void sendContent(ContentModel *contentModel);public slots: void stopProcess();protected: void run();private: QString m_queryString; QString m_headerString; bool m_abort; ContentModel *m_contentModel; QMutex mutex;};ContentRefresher::ContentRefresher(QObject *parent) : QThread(parent){ m_abort = false;}ContentRefresher::~ContentRefresher(){ mutex.lock(); m_abort = true; mutex.unlock(); wait();}void ContentRefresher::processQuery(const QString &qStr, const QString &hStr){ m_queryString = qStr; m_headerString = hStr; m_abort = false; start();}void ContentRefresher::run(){ ContentModel *contentModel = new ContentModel(); contentModel->setQueryString(m_queryString, m_headerString); contentModel->refresh(); emit sendContent(contentModel); if (m_abort) return;}void ContentRefresher::stopProcess(){ mutex.lock(); m_abort = true; mutex.unlock();}
....contentRefresher = new ContentRefresher();connect(contentRefresher, SIGNAL(finished()), this, SLOT(resetUi()));connect(contentRefresher, SIGNAL(sendContent(ContentModel *)), this, SLOT(setContent(ContentModel *)));....void CentralWidget::refreshContentModel(const QString &qStr, const QString &hStr){ m_ui.progressWidget->setVisible(true); contentRefresher->processQuery(qStr, hStr);}void CentralWidget::setContent(ContentModel *contentModel){ m_ui.contentView->setModel(contentModel); m_ui.contentView->setColumnHidden(0,true); m_ui.contentView->setColumnHidden(2,true); m_ui.contentView->resizeColumnsToContents(); m_ui.contentView->resizeRowsToContents(); m_ui.contentView->horizontalHeader()->setStretchLastSection(true); m_ui.contentView->setVisible(true); loadFirstRecipe();}void CentralWidget::resetUi(){ m_ui.progressWidget->setVisible(false);}
void run(){ CreateWidgets(); forever(){ // что то делаете } exec();}
class ContentRefresher : public QThread{ Q_OBJECTpublic: ContentRefresher(QObject *parent = 0); ~ContentRefresher(); void processQuery(const QString &qStr, const QString &hStr);signals: void sendRecord(const ContentRecord &record);public slots: void stopProcess();protected: void run();private: bool m_abort; QMutex mutex; QString m_queryString; QString m_headerString;};ContentRefresher::ContentRefresher(QObject *parent) : QThread(parent){ m_abort = false;}ContentRefresher::~ContentRefresher(){ mutex.lock(); m_abort = true; mutex.unlock(); wait();}void ContentRefresher::processQuery(const QString &qStr, const QString &hStr){ m_queryString = qStr; m_headerString = hStr; m_abort = false; start();}void ContentRefresher::run(){ QSqlQuery query(m_queryString); while(query.next()) { ContentRecord record(query.record()); emit sendRecord(record); if (m_abort) return; msleep(10); }}void ContentRefresher::stopProcess(){ mutex.lock(); m_abort = true; mutex.unlock();}
...contentRefresher = new ContentRefresher(); connect(contentRefresher, SIGNAL(finished()), this, SLOT(resetUi())); connect(contentRefresher, SIGNAL(sendRecord(ContentRecord)), this, SLOT(addRecord(ContentRecord)));...void CentralWidget::refreshContentModel(const QString &qStr, const QString &hStr){ // m_ui.progressWidget->setVisible(true); contentModel->clear(); contentRefresher->processQuery(qStr, hStr);}void CentralWidget::addRecord(const ContentRecord &record){ contentModel->addContentRecord(record.record()); qApp->processEvents();}void CentralWidget::resetUi(){ //m_ui.progressWidget->setVisible(false); m_ui.contentView->resizeRowsToContents(); loadFirstRecipe();}