DataBaseEngine *model; // DataBaseEngine наследуется от QSqlRelationalTableModelmodel = new DataBaseEngine();model->clear(); model->setTable("listwindow"); model->select(); for (int i=0; i <= model->rowCount()-1; i++){ qDebug()<<model->index(i,3).data().toInt(); if (model->index(i,3).data().toInt() == 1 && index(i,4).data().toInt() == 1) model->setData(model->index(i,3),"активно"); else if (model->index(i,3).data().toInt() == 0) model->setData(model->index(i,3),"нет"); else if (model->index(i,3).data().toInt() == 1){ model->setData(model->index(i,3),"РґР°"); qDebug()<<"!!!!";} } model->removeColumn(0); model->removeColumn(3); model->setData(model->index(model->rowCount()-1,3),4); // установка заголовков таблицы model->setHeaderData(0, Qt::Horizontal,QObject::tr("программа")); model->setHeaderData(1, Qt::Horizontal,QObject::tr("заголовок РѕРєРЅР°")); model->setHeaderData(2, Qt::Horizontal,QObject::tr("фокус РІРІРѕРґР°")); return model;
#ifndef WINDOWSMODEL_H#define WINDOWSMODEL_H#include "databaseengine.h"class WindowsModel : public DataBaseEngine{ Q_OBJECTpublic: explicit WindowsModel(QObject *parent = 0); DataBaseEngine *windowModel(); DataBaseEngine* selectWindows(QString date); DataBaseEngine *model;signals: public slots: };#endif // WINDOWSMODEL_H
#include "windowsmodel.h"#include <QDebug>#include <QTableView>WindowsModel::WindowsModel(QObject *parent) : DataBaseEngine(parent){ model = new DataBaseEngine();}DataBaseEngine* WindowsModel::windowModel(){ model->clear(); model->setTable("listwindow"); model->select(); for (int i=0; i <= model->rowCount()-1; i++){ qDebug()<<model->index(i,3).data().toInt(); if (model->index(i,3).data().toInt() == 1 && index(i,4).data().toInt() == 1) model->setData(model->index(i,3),"активно"); else if (model->index(i,3).data().toInt() == 0) model->setData(model->index(i,3),"нет"); else if (model->index(i,3).data().toInt() == 1){ model->setData(model->index(i,3),"да"); qDebug()<<"!!!!";} } model->removeColumn(0); model->removeColumn(3); model->setData(model->index(model->rowCount()-1,3),4); // установка заголовков таблицы model->setHeaderData(0, Qt::Horizontal,QObject::tr("программа")); model->setHeaderData(1, Qt::Horizontal,QObject::tr("заголовок окна")); model->setHeaderData(2, Qt::Horizontal,QObject::tr("фокус ввода")); return model;}DataBaseEngine* WindowsModel::selectWindows(QString date){ model->setFilter(date);}
#ifndef DATABASEENGINE_H#define DATABASEENGINE_H#include <QSqlRelationalTableModel>#include <QtSql/QSqlDatabase>#include <QtSql/QSqlError>#include <QtSql/QSqlQuery>#include <QtSql>class DataBaseEngine : public QSqlRelationalTableModel{ Q_OBJECTpublic: explicit DataBaseEngine(QObject *parent = 0); ~DataBaseEngine(); void createDB(); bool connectDB(); void addData(QMap<QString,QVariant>); Qt::ItemFlags flags(const QModelIndex &index) const; };#endif // DATABASEENGINE_H
#include "databaseengine.h"#include <QModelIndex>#include <QDebug>#include <QFile>#include <QStandardItem>#include <QSqlQueryModel>DataBaseEngine::DataBaseEngine(QObject *parent) : QSqlRelationalTableModel(parent){}DataBaseEngine::~DataBaseEngine(){}void DataBaseEngine::createDB(){ if (!QFile::exists(qApp->applicationDirPath() + "/test.sqlite")) { QSqlDatabase sdb = QSqlDatabase::addDatabase("QSQLITE"); sdb.setDatabaseName(qApp->applicationDirPath() + "/test.sqlite"); sdb.open(); // открывает Р±Рґ если РѕРЅР° есть Рё создаёт её если её нет QSqlQuery a_query; QString tableSession = "CREATE TABLE sessions (" " dateTimeSession datetime NOT NULL," " move_mouse int(11)," " keyboard int(11)," " flagBlock int(1)," " PRIMARY KEY (dateTimeSession)" " ); "; QString tableWindow = "CREATE TABLE listwindow (" " dateTimeSession datetime," " name varchar(50)," " title varchar(250)," " focus int(1)," " flagActivity int(1)," " PRIMARY KEY (name)," "FOREIGN KEY (dateTimeSession) REFERENCES sessions (dateTimeSession) ON DELETE NO ACTION ON UPDATE NO ACTION);"; bool flTable1 = a_query.exec(tableSession); bool flTable2 = a_query.exec(tableWindow); if (!flTable1) qDebug()<<"Не удалось создать таблицу Session..."; if (!flTable2) { qDebug() << "not create db..."<<sdb.lastError().text();; sdb.close(); } else qDebug()<<"БД успешно создана!"; } else if (connectDB()) qDebug()<<"К бд подключились!"; else qDebug()<<"Не удалось подключиться к бд...";}bool DataBaseEngine::connectDB(){ QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(qApp->applicationDirPath() + "/test.sqlite"); if(db.isOpen()) return true; else if (db.open()) return true; else qDebug()<<db.lastError().text();}void DataBaseEngine::addData(QMap<QString,QVariant> map){ qDebug()<<__FUNCTION__; QSqlQuery query; QString date = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"); qDebug()<<date<<"date"<<QDateTime::currentDateTime(); QString text_query = QString(" insert into sessions(move_mouse,keyboard,flagBlock,dateTimeSession) values(%1,%2,%3,'%4')") .arg(map.value("mouse").toInt()).arg(map.value("keyboard").toInt()).arg(map.value("screenLock").toBool()).arg(date); qDebug()<< text_query; bool b = query.exec(text_query); if (!b) qDebug() << "Ошибка выполнения 1-го запроса..."; QList<QVariant > list = map.value("windows").toList(); int countIndex = list.count(); for (register int i=0; i<countIndex; i++){ QMap<QString,QVariant> window = list.at(i).toMap(); QString text = QString("insert into listWindow(name,title,focus,flagActivity,dateTimeSession) values('%1','%2',%3,%4,'%5')") .arg(window.value("nameProgramm").toString()).arg(window.value("title").toString()).arg(window.value("focus").toBool()).arg(window.value("active").toBool()).arg(date); bool b = query.exec(text); }}Qt::ItemFlags DataBaseEngine::flags(const QModelIndex &index) const { Qt::ItemFlags flags = QSqlRelationalTableModel::flags(index); if (index.column() == 1) flags |= Qt::ItemIsEditable; return flags; }