void NewspaperWindow::autoThread(){ QStringList folders; folders << "0" << "1" << "2" << "3" << "4"; for (int i = 0; i < folders.size(); ++i){ text = folders.at(i).toLocal8Bit().constData(); ThreadProcess *thread = new ThreadProcess(text, this); thread->start(QThread::HighPriority); }}
#include <QFile>#include <QFileInfo>#include <QModelIndex>#include <QTextStream>#include <QTextDocument>#include <QStringList>#include <QDir>#include "threadprocess.h"ThreadProcess::ThreadProcess(QString flats, QObject *parent) : QThread(parent), th_flats(flats){ }ThreadProcess::~ThreadProcess(){}void ThreadProcess::run(){ th_model = new QSqlTableModel(); th_model->setTable("Str_rep"); th_model->select(); while ( th_model->canFetchMore() ) th_model->fetchMore(); th_model_phon = new QSqlTableModel(); th_model_phon->setTable("Phon"); th_model_phon->select(); while ( th_model_phon->canFetchMore() ) th_model_phon->fetchMore(); QStringList files; QString dirname = "C:\\Local Web\\" + th_flats + "\\www.dalpress.ru\\gazeta\\list"; QDir directory = QDir( dirname ); if ( fileName.isEmpty() ) fileName = "*"; files = directory.entryList( QStringList( fileName ), QDir::Files | QDir::NoSymLinks ); unitName = "uniter" + th_flats + ".txt"; ThreadfindFiles( directory, files ); processText(); threadSave();}void ThreadProcess::ThreadfindFiles( const QDir &directory, const QStringList &files){ QStringList foundFiles; QTextStream out(&proc_str); for ( int i = 0; i < files.count(); ++i ) { qApp->processEvents(); QFile file( directory.absoluteFilePath( files[ i ] ) ); if ( file.open( QIODevice::ReadOnly ) ) { QTextStream in(&file); while ( !in.atEnd() ) { QByteArray data = file.readAll(); QTextCodec *codec = Qt::codecForHtml( data ); str = codec->toUnicode( data ); doc = new QTextDocument(); if ( Qt::mightBeRichText( str ) ) { doc->setHtml( str ); str = doc->toPlainText(); } else { str = QString::fromLocal8Bit( data ); } out << str; foundFiles << files[ i ]; } } }}void ThreadProcess::processText(){ QString street; QString street_rep; for ( int i = 0; i < th_model->rowCount(); ++i ) { QModelIndex indexStr = th_model->index( i, 0 ); QModelIndex indexRep = th_model->index( i, 1 ); street = th_model->data( indexStr, Qt::DisplayRole ).toString(); street_rep = th_model->data( indexRep, Qt::DisplayRole ).toString(); proc_str.replace( street + " ", "\t" + street_rep + " " ); } QString phon; for ( int i = 0; i < th_model_phon->rowCount(); ++i ) { QModelIndex indexphon = th_model_phon->index( i, 0 ); phon = th_model_phon->data( indexphon ).toString(); proc_str.replace( phon, "" ); } QString strout; QTextStream in(&proc_str ); QTextStream out(&strout); while (!in.atEnd()) { QString line = in.readLine(); int j = 6 - line.count(QRegExp("\\t")); for (int i = 0; i < j; ++i) { line.append("\t"); } if (line.contains(QRegExp("\\d{5,}"))){ if(!th_flats.isNull()){ out << th_flats << "\t" << line << "\n"; line = th_flats + "\t" + line; } else { out << line << "\n"; } QStringList list = line.split(QRegExp("\\t")); [color=red]///// showList(list); // здесь вызывается функция главного окна - а именно заполнение TableWidgeta данными [/color] } } doc->setPlainText(strout);}void ThreadProcess::threadSave(){ QFile file(unitName); if ( !file.open( QFile::WriteOnly) ) { return ; } QTextStream save_out( &file ); QApplication::setOverrideCursor( Qt::WaitCursor ); save_out << doc->toPlainText(); QApplication::restoreOverrideCursor();}
QStringList list = line.split(QRegExp("\\t")); [color=red]///// showList(list); // здесь вызывается функция главного окна - а именно заполнение TableWidgeta данными [/color] }
bool NewspaperWindow::showList( const QStringList &list){ int j = 0; listTable->insertRow(j); for ( int i = 0; i < listTable->columnCount(); ++i ) { QTableWidgetItem *listNameItem = new QTableWidgetItem( list[ i ] ); listTable->setItemDelegate(new TableDelegate()); listTable->setItem( j, i, listNameItem ); } return true;}
QList<int> global_shared_list;// thread 1global_shared_list << 123;// thread 2for ( QListIterator<int> it( global_shared_list ); it.hasNext(); ) do_something_with_int( it.next() );
DataStorage():data(""){};
#ifndef LISTSTORAGE_H#define LISTSTORAGE_H#include <QQueue>#include <QMutex>class ListStorage { QQueue<QStringList> listqueue;//это тоже не понял- сделал как в примере QMutex mutex;public: ListStorage():listqueue(){}; void setData(QStringList &str){ mutex.lock(); listqueue.enqueue(str); mutex.unlock(); } void getData(QStringList &th_list){ mutex.lock(); while (!listqueue.isEmpty()) th_list = listqueue.dequeue(); mutex.unlock(); };};#endif // LISTSTORAGE_H
void NewspaperWindow::autoThread(){ QStringList folders; folders << "0" << "1" << "2" << "3" << "4"; for (int i = 0; i < folders.size(); ++i){ text = folders.at(i).toLocal8Bit().constData(); ThreadProcess *thread = new ThreadProcess(text, this); thread->setListStorage(&ds); connect(thread, SIGNAL(dataReady()), this, SLOT(ins_ShowList())); thread->start(QThread::HighPriority); }}void NewspaperWindow::ins_ShowList(){ ds.getData(th_list); showList(th_list);}
............... list = line.split(QRegExp("\\t")); _ds->setData(list); emit dataReady(); this->msleep(1000);//!!!!!!!!!!!!!!!!!!!!!!!!!!
SecondTh::SecondTh(QObject *parent) : QThread(parent){ counter = 0; timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(gen_data())); timer->start(1000); }
<skip> void getData(QStringList &th_list){ mutex.lock(); if (!listqueue.isEmpty()) th_list = listqueue.dequeue(); mutex.unlock(); };};
<skip>void NewspaperWindow::ins_ShowList(){ QStringList th_list ds.getData(th_list); showList(th_list);}
_ds.getData(th_list); QStringList nw_list; nw_list = th_list.split(QRegExp("\\t")); showList(nw_list);
<skip> void getData(QStringList &th_list){ mutex.lock(); if (!listqueue.isEmpty()) //Здесь у тебя стояло while, т.е. ты выбирал из очереди ВСЁ //но возвращал только последний элемент, остальные терял th_list = listqueue.dequeue(); mutex.unlock(); };};
<skip>void NewspaperWindow::ins_ShowList(){//Здес нужен именно локальный объект, чтоб не замарачиваться чисткой QStringList th_list ds.getData(th_list); showList(th_list);}