C++ (Qt)#ifndef MAINWINDOW_H#define MAINWINDOW_H #include <QMainWindow>#include <QThread>#include "database.h" class MainWindow : public QMainWindow{ Q_OBJECT public: MainWindow(QWidget *parent = 0); ~MainWindow(); void startFirstThread(); void stratSecondThread();}; #endif // MAINWINDOW_H
C++ (Qt)#include "mainwindow.h"//-------------------------------------------------MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){ this->startFirstThread(); this->stratSecondThread();}//-------------------------------------------------MainWindow::~MainWindow(){}//-------------------------------------------------void MainWindow::startFirstThread(){ QThread* thread = new QThread; DataBase* db = new DataBase; QObject::connect(thread, SIGNAL(started()), db, SLOT(ProcessFirst())); QObject::connect(db, SIGNAL(finished()), thread, SLOT(quit())); QObject::connect(db, SIGNAL(finished()), db, SLOT(deleteLater())); QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); db->moveToThread(thread); thread->start();}//-------------------------------------------------void MainWindow::stratSecondThread(){ QThread* thread = new QThread; DataBase* db = new DataBase; QObject::connect(thread, SIGNAL(started()), db, SLOT(ProcessSecond())); QObject::connect(db, SIGNAL(finished()), thread, SLOT(quit())); QObject::connect(db, SIGNAL(finished()), db, SLOT(deleteLater())); QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); db->moveToThread(thread); thread->start();}//-------------------------------------------------
C++ (Qt)#ifndef DATABASE_H#define DATABASE_H #include <QObject>#include <QtSql> class DataBase : public QObject{ Q_OBJECTpublic: explicit DataBase(QObject *parent = 0); private: QString nameConnect; QString lastError; QSqlQuery* qDb; bool init(QString tableName); signals: void finished(); public slots: void ProcessFirst(); void ProcessSecond();}; #endif // DATABASE_H
C++ (Qt)#include "database.h"//----------------------------------------------------------DataBase::DataBase(QObject *parent) : QObject(parent){ qsrand(QTime(0,0,0).msecsTo(QTime::currentTime()));}//----------------------------------------------------------//Инициализацияbool DataBase::init(QString tableName){ this->nameConnect = QString("%1%2").arg(tableName).arg(qrand()); QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", this->nameConnect); db.setDatabaseName(tableName); db.setHostName("127.0.0.1"); db.setUserName("rudi"); db.setPassword("rudi"); //Open if(!db.open()) { this->lastError = QString("Cannot open dbBlackList database: %1").arg(db.lastError().text()); qDebug() << this->lastError; return false; } this->qDb = new QSqlQuery(QSqlDatabase::database(this->nameConnect)); if(!this->qDb) return false; return true;}//----------------------------------------------------------void DataBase::ProcessFirst(){ this->init("First"); emit finished();}//----------------------------------------------------------void DataBase::ProcessSecond(){ this->init("Second"); emit finished();}//----------------------------------------------------------