Created
April 16, 2014 00:53
-
-
Save ulvham/10794122 to your computer and use it in GitHub Desktop.
new_scrub1.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "database.h" | |
#include "axobjects.h" | |
#include "viewmodel.h" | |
#include "settings.h" | |
bool flag_exit=false; | |
QStringList listcheck; | |
QStringList listcheck1; | |
QString pathdb; | |
QString currentcontragent; | |
DataBase db; | |
My1SqlModel *sqlmodel = new My1SqlModel(); | |
viewmodel::viewmodel(QWidget *parent) : | |
QWidget(parent) | |
{ | |
//this->setWindowFlags(Qt::CustomizeWindowHint); | |
QVBoxLayout *layout0 = new QVBoxLayout(); | |
layout0->setObjectName ("vbox"); | |
QVBoxLayout *layout01 = new QVBoxLayout(); | |
layout0->setObjectName ("vbox01"); | |
QHBoxLayout *layout = new QHBoxLayout(); | |
layout->setObjectName ("hbox"); | |
layout->setSpacing (0); | |
layout->setMargin (0); | |
// QLabel* labl1 = new QLabel("###"); | |
// labl1->hide (); | |
// labl1->setObjectName ("lableid"); | |
// QLineEdit* le1 = new QLineEdit(); | |
// le1->hide (); | |
// le1->setObjectName ("lineedit1"); | |
// QLineEdit* le2 = new QLineEdit(); | |
// le2->hide (); | |
// le2->setObjectName ("lineedit2"); | |
// QLineEdit* le3 = new QLineEdit(); | |
// le3->hide (); | |
// le3->setObjectName ("lineedit3"); | |
// QLineEdit* le4 = new QLineEdit(); | |
// le4->hide (); | |
// le4->setObjectName ("lineedit4"); | |
// QLineEdit* le5 = new QLineEdit(); | |
// le5->hide (); | |
// le5->setObjectName ("lineedit5"); | |
// QPushButton* b_upd = new QPushButton(); | |
// b_upd->hide (); | |
// b_upd->setObjectName ("b_update1"); | |
// connect(b_upd, SIGNAL(clicked()), this, SLOT(update1())); | |
QPushButton *b_ok = new QPushButton("Close"); | |
QPushButton *b_test = new QPushButton("Выбрать базу"); | |
QPushButton *base_add_item = new QPushButton("Добавить объём к выбранному объекту"); | |
QProgressBar *pbar = new QProgressBar(); | |
QTableView* ls = new QTableView(); | |
QTableView* ls1 = new QTableView(); | |
ls1->hide (); | |
QTableView* ls2 = new QTableView(); | |
ls2->hide (); | |
ls->setObjectName ("table_view_00"); | |
ls1->setObjectName ("table_view_01"); | |
ls2->setObjectName ("table_view_02"); | |
pbar->setMaximum (0); | |
pbar->setMinimum (0); | |
pbar->setValue (0); | |
pbar->setVisible (false); | |
pbar->setTextVisible (false); | |
pbar->setAlignment (Qt::AlignHCenter); | |
pbar->setAlignment (Qt::AlignVCenter); | |
pbar->setMaximumHeight (8); | |
pbar->setObjectName ("progress"); | |
layout->addWidget(ls); | |
// layout01->addWidget (labl1); | |
// layout01->addWidget (le1); | |
// layout01->addWidget (le2); | |
// layout01->addWidget (le3); | |
// layout01->addWidget (le4); | |
// layout01->addWidget (le5); | |
// layout01->addWidget (b_upd); | |
layout01->addWidget(ls1); | |
layout->addLayout (layout01); | |
layout->addWidget(ls2); | |
layout->addWidget(pbar); | |
layout0->addLayout (layout); | |
layout0->addWidget(b_ok); | |
layout0->addWidget(b_test); | |
layout0->addWidget(base_add_item); | |
this->setLayout(layout0); | |
this->setWindowTitle ("Подтвердите"); | |
connect(b_ok, SIGNAL(clicked()), this, SLOT(close())); | |
connect(b_test, SIGNAL(clicked()), this, SLOT(opendb())); | |
connect(base_add_item, SIGNAL(clicked()), this, SLOT(add_v())); | |
//connect(ls1, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(test02(QModelIndex))); | |
QSettings settings("base"); | |
QString path_db = settings.value("databasepathclosemonth").toString (); | |
QString query_db = "SELECT c.name, c.num_contract FROM contragents c ORDER BY c.name ASC"; | |
QString query_db2 = "SELECT o.name, o.address FROM objects o"; | |
QString query_db3 = "SELECT v.year, v.month, v.v FROM v_gaz v"; | |
db.open (path_db,"*.mdb"); | |
// QSqlQueryModel *sqlmodel = new QSqlQueryModel(this); | |
// sqlmodel->setObjectName ("model_00"); | |
// sqlmodel->setQuery(query_db); | |
// QSettings settings("base"); | |
// QString path_db = settings.value("databasepathclosemonth").toString (); | |
// QString query_db = "SELECT c.name, c.num_contract FROM contragents c ORDER BY c.name ASC"; | |
// db.open (path_db,"*.mdb"); | |
sqlmodel->iscolumnwithcheck << 0; | |
sqlmodel->setObjectName ("model_00"); | |
sqlmodel->setQuery(query_db); | |
//ui->tableView->setModel (sqlmodel); | |
// while (sqlmodel->canFetchMore ()) | |
// { | |
// sqlmodel->fetchMore (); | |
// } | |
// QStringList sqlgead; | |
// qDebug() << sqlmodel->rowCount (); | |
// QStandardItemModel *model = new QStandardItemModel(); | |
// for (int varcol = 0; varcol < sqlmodel->columnCount (); ++varcol) { | |
// for (int varrow = 0; varrow < sqlmodel->rowCount (); ++varrow) { | |
// QStandardItem* checkBox = new QStandardItem(true); | |
// if (varcol==0) { | |
// checkBox->setCheckable(true); | |
// checkBox->setCheckState(Qt::Unchecked); | |
// } | |
// checkBox->setData (sqlmodel->index (varrow,varcol).data (),0); | |
// model->setItem(varrow, varcol, checkBox); | |
// } | |
// sqlgead << sqlmodel->headerData (varcol,Qt::Horizontal).toString (); | |
// } | |
// model->setHorizontalHeaderLabels (sqlgead); | |
ls->setModel(sqlmodel); | |
ls->setSortingEnabled (true); | |
ls->sortByColumn (0,Qt::AscendingOrder ); | |
ls->setSelectionBehavior(QAbstractItemView::SelectRows); | |
ls->resizeColumnsToContents (); | |
ls->resizeRowsToContents (); | |
ls->setEditTriggers (QTableView::NoEditTriggers); | |
QAbstractItemModel* modelscrub = ls->model (); | |
if (modelscrub) | |
{ | |
connect (modelscrub,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(test01(const QModelIndex &, const QModelIndex &))); | |
} | |
//connect(ls,SIGNAL(doubleClicked(QModelIndex)), this, SLOT(view_params(QModelIndex))); | |
//connect (ls, SIGNAL(clicked(QModelIndex)), this, SLOT(selecting_(const QModelIndex &))); | |
//ls->setModel(sqlmodel); | |
QSqlQueryModel *sqlmodel2 = new QSqlQueryModel(this); | |
sqlmodel2->setObjectName ("model_01"); | |
//sqlmodel2->setQuery(query_db2); | |
//sqlmodel2->removeRows (0,sqlmodel2->rowCount ()); | |
ls1->setModel(sqlmodel2); | |
QSqlQueryModel *sqlmodel3 = new QSqlQueryModel(this); | |
sqlmodel3->setObjectName ("model_02"); | |
//sqlmodel3->setQuery(query_db3); | |
//sqlmodel3->removeRows (0,sqlmodel3->rowCount ()); | |
ls2->setModel(sqlmodel3); | |
QFutureWatcher<void> watcher; | |
connect (&watcher, SIGNAL(finished()), this, SLOT(close())); | |
QFuture<void> future = QtConcurrent::run(this,&viewmodel::serverCycle); | |
qApp->processEvents(); | |
} | |
viewmodel::~viewmodel() | |
{ | |
flag_exit=true; | |
db.close (); | |
qDebug() << "exit0"; | |
} | |
void viewmodel::serverCycle() | |
{ | |
while (!flag_exit) { | |
Sleep(2000); | |
if (flag_exit) break; | |
qApp->processEvents(); | |
QTableView *tv = this->findChild<QTableView *>("table_view_00"); | |
QModelIndexList selectedList = tv->selectionModel()->selectedIndexes (); | |
//My1SqlModel *model = My1SqlModel::findChild<My1SqlModel *>("model_00"); | |
//sqlmodel->updatenew (); | |
sqlmodel->reboot (); | |
//QAbstractItemModel* model1 = tv->model (); | |
//qDebug() << sqlmodel->boolhash; | |
//по мере запроса сверять с абстрактной моделью, и изменять, добавлять и удалять. далее сохранность галок | |
// sqlmodel->setQuery(model->query().lastQuery()); | |
//qDebug() << model->query().lastQuery(); | |
//tv->setModel (model); | |
//tv->setModel (model1); | |
// QStandardItemModel *model_ = new QStandardItemModel(); | |
// for (int varcol = 0; varcol < model->columnCount (); ++varcol) { | |
// for (int varrow = 0; varrow < model->rowCount (); ++varrow) { | |
// QStandardItem* checkBox = new QStandardItem(true); | |
// if (varcol==0) { | |
// checkBox->setCheckable(true); | |
// foreach (QModelIndex i, selectedList) { | |
// if (model->index (varrow,varcol).data ()==i.data ()){ | |
// checkBox->setCheckState(Qt::Checked); | |
// qDebug() << i.data (); | |
// } | |
// //else checkBox->setCheckState(Qt::Unchecked); | |
// ////tv->selectionModel ()->select(i, QItemSelectionModel::Select); | |
// //qDebug() << i.flags (); | |
// } | |
// //checkBox->setCheckState(Qt::Unchecked); | |
// } | |
// checkBox->setData (model->index (varrow,varcol).data (),0); | |
// model_->setItem(varrow, varcol, checkBox); | |
// } | |
// } | |
// tv->setModel(model_); | |
// tv->setSortingEnabled (true); | |
// tv->setSelectionBehavior(QAbstractItemView::SelectRows); | |
// tv->resizeColumnsToContents (); | |
// tv->resizeRowsToContents (); | |
// QAbstractItemModel* modelscrub = tv->model (); | |
// if (modelscrub) | |
// { | |
// connect (modelscrub,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(test01(const QModelIndex &, const QModelIndex &))); | |
// } | |
// QSqlQueryModel *model1 = this->findChild<QSqlQueryModel *>("model_01"); | |
// model1->setQuery(model1->query().lastQuery()); | |
// QSqlQueryModel *model2 = this->findChild<QSqlQueryModel *>("model_02"); | |
// model2->setQuery(model2->query().lastQuery()); | |
qDebug() << selectedList.count(); | |
//-- | |
foreach (QModelIndex i, selectedList) { | |
tv->selectionModel ()->select(i, QItemSelectionModel::Select); | |
qDebug() << i.flags (); | |
} | |
} | |
} | |
void viewmodel::testt() | |
{ | |
QFutureWatcher<void> watcher; | |
while (true) { | |
QFuture<void> future = QtConcurrent::run(this,&viewmodel::serverCycle); | |
watcher.setFuture(future); | |
QProgressBar *probar = this->findChild<QProgressBar *>("progress"); | |
while (watcher.isRunning ()) { | |
probar->setVisible (true); | |
qApp->processEvents(); | |
} | |
probar->setVisible (false); | |
future.waitForFinished (); | |
qApp->processEvents(); | |
} | |
} | |
void viewmodel::opendb() | |
{ | |
pathdb = QFileDialog::getOpenFileName(this, "change base", "", "Base (*.mdb);;All (*.*)"); | |
QSettings settings("base"); | |
settings.setValue("databasepathclosemonth", pathdb); | |
// int path = settings.value("databasepathclosemonth").toString (); | |
qDebug() << pathdb; | |
} | |
void viewmodel::add_v() | |
{ | |
QList<QStringList> newt; | |
QTableView *ls = this->findChild<QTableView *>("table_view_01"); | |
//QSqlQueryModel *sqlmodel = this->findChild<QSqlQueryModel *>("model_02"); | |
const QAbstractItemModel* model = ls->model (); | |
bool ok;bool ok2; | |
QStringList items; | |
items << "Январь" << "Февраль" << "Март" << "Апрель" << "Май" << "Июнь" << "Июль" << "Август" << "Сентябрь" << "Октябрь" << "Ноябрь" << "Декабрь"; | |
QString month_ = QInputDialog::getItem(this, "Месяц","Выберите месяц:", items, 0, false, &ok); | |
if (ok && !items.isEmpty()) | |
for (int var = 0; var < model->rowCount (); ++var) { | |
QModelIndex mi = ls->model ()->index (var,0); | |
Qt::CheckState check_ = (Qt::CheckState)model->data (mi, Qt::CheckStateRole).toInt (); | |
if (check_ == Qt::Checked) { | |
QString sqlid=QString("SELECT ob.id FROM objects ob WHERE ob.name=\'%1\'").arg(mi.model ()->index (var,0).data (0).toString ()); | |
newt = db.db_in_list (sqlid); | |
//int zn = newt[0][0].toInt (); | |
double d = QInputDialog::getDouble(this, mi.model ()->index (var,0).data (0).toString (), "Объём за: "+month_, rand ()%1000+500, -1000000, 1000000, 0, &ok2); | |
if (!ok) d=0; | |
QString sql = QString("INSERT INTO v_gaz (year, month, v, objects_link) VALUES (2015,'%1',%3, %2)").arg(month_).arg(newt[0][0]).arg(d); | |
db.do_this (sql); | |
} | |
} | |
//QString sql = QString("INSERT INTO v_gaz (year, month, v, objects_link) VALUES (2015,'Январь',5555, %1)").arg(); | |
//db->doit (sql); | |
} | |
void viewmodel::update1() | |
{ | |
QLineEdit *le1 = this->findChild<QLineEdit *>("lineedit1"); | |
QLineEdit *le2 = this->findChild<QLineEdit *>("lineedit2"); | |
QLineEdit *le3 = this->findChild<QLineEdit *>("lineedit3"); | |
QLineEdit *le4 = this->findChild<QLineEdit *>("lineedit4"); | |
QLineEdit *le5 = this->findChild<QLineEdit *>("lineedit5"); | |
QLabel *labl1 = this->findChild<QLabel *>("lableid"); | |
QString query_upd = QString("UPDATE contragents SET name = \'%2\' WHERE name = \'%1\'").arg(currentcontragent).arg (le1->text ()); | |
db.do_this (query_upd); | |
} | |
void viewmodel::view_params(const QModelIndex &model) | |
{ | |
QWidget* widget_01 = new QWidget(); | |
QVBoxLayout *layout01 = new QVBoxLayout(); | |
QLabel* labl1 = new QLabel("###"); | |
labl1->hide (); | |
labl1->setObjectName ("lableid"); | |
QLineEdit* le1 = new QLineEdit(); | |
le1->hide (); | |
le1->setObjectName ("lineedit1"); | |
QLineEdit* le2 = new QLineEdit(); | |
le2->hide (); | |
le2->setObjectName ("lineedit2"); | |
QLineEdit* le3 = new QLineEdit(); | |
le3->hide (); | |
le3->setObjectName ("lineedit3"); | |
QLineEdit* le4 = new QLineEdit(); | |
le4->hide (); | |
le4->setObjectName ("lineedit4"); | |
QLineEdit* le5 = new QLineEdit(); | |
le5->hide (); | |
le5->setObjectName ("lineedit5"); | |
QPushButton* b_upd = new QPushButton(); | |
b_upd->hide (); | |
b_upd->setObjectName ("b_update1"); | |
connect(b_upd, SIGNAL(clicked()), this, SLOT(update1())); | |
layout01->addWidget (labl1); | |
layout01->addWidget (le1); | |
layout01->addWidget (le2); | |
layout01->addWidget (le3); | |
layout01->addWidget (le4); | |
layout01->addWidget (le5); | |
layout01->addWidget (b_upd); | |
widget_01->setLayout(layout01); | |
widget_01->setWindowTitle ("info"); | |
QString query_for_lineedit = QString("SELECT * FROM contragents c WHERE c.name=\'%1\'").arg(model.data (0).toString ()); | |
QList<QStringList> nlist = db.db_in_list (query_for_lineedit); | |
// qDebug() << nlist[0][0]; | |
currentcontragent = nlist[0][1]; | |
labl1->setText (nlist[0][0]); | |
le1->setText (nlist[0][1]); | |
le2->setText (nlist[0][2]); | |
le3->setText (nlist[0][3]); | |
le4->setText (nlist[0][4]); | |
le5->setText (nlist[0][5]); | |
} | |
void viewmodel::test02(const QModelIndex & topmodel, const QModelIndex & bottommodel) | |
{ | |
const QAbstractItemModel* model = topmodel.model (); | |
QTableView *ls0 = this->findChild<QTableView *>("table_view_01"); | |
QTableView *ls = this->findChild<QTableView *>("table_view_02"); | |
QSqlQueryModel *sqlmodel = this->findChild<QSqlQueryModel *>("model_02"); | |
QModelIndexList selectedList =ls0->selectionModel()->selectedIndexes (); | |
// QStandardItemModel *mod_ = new QStandardItemModel(); | |
// foreach (QModelIndex i, selectedList) { | |
// model-> | |
// ls0->selectionModel ()->select(i, QItemSelectionModel::Select); | |
// qDebug() << mod_->buddy (i).data (0).toString (); | |
// } | |
// for (int varrow = 0; varrow < model->rowCount (); ++varrow) { | |
// QModelIndex mi = topmodel.model ()->index (varrow,0); | |
// Qt::CheckState check_ = (Qt::CheckState)model->data (mi, Qt::CheckStateRole).toInt (); | |
// mi.model ()->index (var,0). | |
// QModelIndex ind = ls->selectionModel ()->selectedRows () | |
// QStandardItem* checkBox = new QStandardItem(true); | |
// if (varcol==0) { | |
// checkBox->setCheckable(true); | |
// checkBox->setCheckState(Qt::Unchecked); | |
// } | |
// checkBox->setData (sqlmodel->index (varrow,varcol).data (),0); | |
// model->setItem(varrow, varcol, checkBox); | |
// } | |
//QStringList listcheck; | |
QString listsql= ""; | |
int checkcount=0; | |
for (int var = 0; var < model->rowCount (); ++var) { | |
QModelIndex mi = topmodel.model ()->index (var,0); | |
Qt::CheckState check_ = (Qt::CheckState)model->data (mi, Qt::CheckStateRole).toInt (); | |
if ((check_ == Qt::Checked) && (checkcount!=0)) { | |
listcheck << mi.model ()->index (var,0).data (0).toString (); | |
//ls0->selectionModel ()->select(mi, QItemSelectionModel::Select); | |
listsql += " UNION ALL " + QString("SELECT o.name, v.type, v.year, v.month, v.v FROM objects o,v_gaz v WHERE v.objects_link=(SELECT ob.id FROM objects ob WHERE ob.name=\'%1\') AND o.id=v.objects_link").arg(mi.model ()->index (var,0).data (0).toString ()); | |
checkcount++; | |
} else if ((check_ == Qt::Checked) && (checkcount==0)) | |
{ | |
listcheck << mi.model ()->index (var,0).data (0).toString (); | |
//ls0->selectionModel ()->select(mi, QItemSelectionModel::Select); | |
listsql += QString("SELECT o.name, v.type, v.year, v.month, v.v FROM objects o,v_gaz v WHERE v.objects_link=(SELECT ob.id FROM objects ob WHERE ob.name=\'%1\') AND o.id=v.objects_link").arg(mi.model ()->index (var,0).data (0).toString ()); | |
checkcount++; | |
} | |
else if (check_ == Qt::Unchecked) | |
{ | |
listcheck.removeOne (mi.model ()->index (var,0).data (0).toString ()); | |
//checkcount--; | |
//ls0->selectionModel ()->select(mi,QItemSelectionModel::Deselect ); | |
} | |
} | |
//ls0->selectionModel ()->select (topmodel,QItemSelectionModel::Select); | |
sqlmodel->setQuery(listsql); | |
//qDebug() << checkcount; | |
QStandardItemModel *modelnew = new QStandardItemModel(); | |
for (int varcol = 0; varcol < sqlmodel->columnCount (); ++varcol) { | |
for (int varrow = 0; varrow < sqlmodel->rowCount (); ++varrow) { | |
QStandardItem* checkBox = new QStandardItem(true); | |
if (varcol==0) { | |
checkBox->setCheckable(true); | |
checkBox->setCheckState(Qt::Unchecked); | |
} | |
checkBox->setData (sqlmodel->index (varrow,varcol).data (),0); | |
// foreach (QModelIndex i, selectedList) { | |
// ls0->selectionModel ()->select(i, QItemSelectionModel::Select); | |
// qDebug() << mod_->buddy (i).data (0).toString (); | |
// } | |
modelnew->setItem(varrow, varcol, checkBox); | |
} | |
} | |
if (checkcount==0) { | |
ls->hide (); | |
} else { | |
ls->show (); | |
} | |
ls->setModel(modelnew); | |
ls->setSortingEnabled (true); | |
ls->resizeColumnsToContents (); | |
ls->resizeRowsToContents (); | |
} | |
void viewmodel::test01(const QModelIndex & topmodel, const QModelIndex & bottommodel) | |
{ | |
// QLineEdit *le1 = this->findChild<QLineEdit *>("lineedit1"); | |
// QLineEdit *le2 = this->findChild<QLineEdit *>("lineedit2"); | |
// QLineEdit *le3 = this->findChild<QLineEdit *>("lineedit3"); | |
// QLineEdit *le4 = this->findChild<QLineEdit *>("lineedit4"); | |
// QLineEdit *le5 = this->findChild<QLineEdit *>("lineedit5"); | |
// QLabel *labl1 = this->findChild<QLabel *>("lableid"); | |
// QPushButton *b_upd = this->findChild<QPushButton *>("b_update1"); | |
int checkcount=0; | |
const QAbstractItemModel* model = topmodel.model (); | |
QTableView *ls0 = this->findChild<QTableView *>("table_view_00"); | |
QTableView *ls = this->findChild<QTableView *>("table_view_01"); | |
QSqlQueryModel *sqlmodel = this->findChild<QSqlQueryModel *>("model_01"); | |
QList<QStringList> selections; | |
QList<int> selectionsid; | |
QModelIndexList selectedList = ls0->selectionModel()->selectedIndexes ();//// | |
foreach (QModelIndex i, selectedList) { | |
//tv->selectionModel ()->select(i, QItemSelectionModel::Select); | |
if (i.column ()==0) { | |
selections << i.sibling (i.row (),0).data ().toStringList (); | |
selectionsid << i.row (); | |
//qDebug() << selections << selectionsid; | |
} | |
} | |
if (checkcount==1) { | |
//------------------------ | |
QLineEdit* name01 = new QLineEdit("123"); | |
name01->setObjectName ("test01"); | |
QHBoxLayout *hbox = this->findChild<QHBoxLayout *>("hbox"); | |
hbox->addWidget (name01); | |
//------------------------ | |
} else { | |
QLineEdit *name01 = this->findChild<QLineEdit *>("test01"); | |
delete name01; | |
} | |
//QModelIndexList selectedList =ls0->selectionModel()->selectedIndexes (); | |
// QStandardItemModel *mod_ = new QStandardItemModel(); | |
// foreach (QModelIndex i, selectedList) { | |
// model-> | |
// ls0->selectionModel ()->select(i, QItemSelectionModel::Select); | |
// qDebug() << mod_->buddy (i).data (0).toString (); | |
// } | |
// for (int varrow = 0; varrow < model->rowCount (); ++varrow) { | |
// QModelIndex mi = topmodel.model ()->index (varrow,0); | |
// Qt::CheckState check_ = (Qt::CheckState)model->data (mi, Qt::CheckStateRole).toInt (); | |
// mi.model ()->index (var,0). | |
// QModelIndex ind = ls->selectionModel ()->selectedRows () | |
// QStandardItem* checkBox = new QStandardItem(true); | |
// if (varcol==0) { | |
// checkBox->setCheckable(true); | |
// checkBox->setCheckState(Qt::Unchecked); | |
// } | |
// checkBox->setData (sqlmodel->index (varrow,varcol).data (),0); | |
// model->setItem(varrow, varcol, checkBox); | |
// } | |
QString listsql= ""; | |
for (int var = 0; var < model->rowCount (); ++var) { | |
QModelIndex mi = topmodel.model ()->index (var,0); | |
Qt::CheckState check_ = (Qt::CheckState)model->data (mi, Qt::CheckStateRole).toInt (); | |
if ((check_ == Qt::Checked) && (checkcount!=0)) { | |
listcheck1 << mi.model ()->index (var,0).data (0).toString (); | |
//ls0->selectionModel ()->select(mi, QItemSelectionModel::Select); | |
listsql += " UNION ALL " + QString("SELECT o.name, o.address FROM objects o,contragents c WHERE o.contragent_link=(SELECT con.id FROM contragents con WHERE con.name=\'%1\') AND c.id=o.contragent_link").arg(mi.model ()->index (var,0).data (0).toString ()); | |
checkcount++; | |
} else if ((check_ == Qt::Checked) && (checkcount==0)) | |
{ | |
listcheck1 << mi.model ()->index (var,0).data (0).toString (); | |
//ls0->selectionModel ()->select(mi, QItemSelectionModel::Select); | |
listsql += QString("SELECT o.name, o.address FROM objects o,contragents c WHERE o.contragent_link=(SELECT con.id FROM contragents con WHERE con.name=\'%1\') AND c.id=o.contragent_link").arg(mi.model ()->index (var,0).data (0).toString ()); | |
checkcount++; | |
// QString query_for_lineedit = QString("SELECT * FROM contragents c WHERE c.name=\'%1\'").arg(mi.model ()->index (var,0).data (0).toString ()); | |
// QList<QStringList> nlist = db.db_in_list (query_for_lineedit); | |
//// qDebug() << nlist[0][0]; | |
// currentcontragent = nlist[0][1]; | |
// labl1->setText (nlist[0][0]); | |
// le1->setText (nlist[0][1]); | |
// le2->setText (nlist[0][2]); | |
// le3->setText (nlist[0][3]); | |
// le4->setText (nlist[0][4]); | |
// le5->setText (nlist[0][5]); | |
} | |
else if (check_ == Qt::Unchecked) | |
{ | |
listcheck1.removeOne (mi.model ()->index (var,0).data (0).toString ()); | |
//ls0->selectionModel ()->select(mi,QItemSelectionModel::Deselect ); | |
} | |
} | |
//ls0->selectionModel ()->select (topmodel,QItemSelectionModel::Select); | |
sqlmodel->setQuery(listsql); | |
//qDebug() << listcheck1; | |
QStandardItemModel *modelnew = new QStandardItemModel(); | |
for (int varcol = 0; varcol < sqlmodel->columnCount (); ++varcol) { | |
for (int varrow = 0; varrow < sqlmodel->rowCount (); ++varrow) { | |
QStandardItem* checkBox = new QStandardItem(true); | |
if (varcol==0) { | |
checkBox->setCheckable(true); | |
checkBox->setCheckState(Qt::Unchecked); | |
} | |
checkBox->setData (sqlmodel->index (varrow,varcol).data (),0); | |
// foreach (QModelIndex i, selectedList) { | |
// ls0->selectionModel ()->select(i, QItemSelectionModel::Select); | |
// qDebug() << mod_->buddy (i).data (0).toString (); | |
// } | |
modelnew->setItem(varrow, varcol, checkBox); | |
} | |
} | |
if (checkcount==0) { | |
ls->hide (); | |
// le1->hide (); | |
// le2->hide (); | |
// le3->hide (); | |
// le4->hide (); | |
// le5->hide (); | |
// labl1->hide (); | |
// b_upd->hide (); | |
} else { | |
ls->show (); | |
// le1->show (); | |
// le2->show (); | |
// le3->show (); | |
// le4->show (); | |
// le5->show (); | |
// labl1->show (); | |
// b_upd->show (); | |
} | |
ls->setModel(modelnew); | |
QAbstractItemModel* modelscrub = ls->model (); | |
if (modelscrub) | |
{ | |
connect (modelscrub,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(test02(const QModelIndex &, const QModelIndex &))); | |
} | |
} | |
void viewmodel::selecting_(const QModelIndex & topmodel) | |
{ | |
const QAbstractItemModel* model = topmodel.model (); | |
QTableView *ls0 = this->findChild<QTableView *>("table_view_00"); | |
for (int var = 0; var < model->rowCount (); ++var) { | |
QModelIndex mi = topmodel.model ()->index (var,0); | |
Qt::CheckState check_ = (Qt::CheckState)model->data (mi, Qt::CheckStateRole).toInt (); | |
if (check_ == Qt::Checked) { | |
//ls0->selectionModel ()->select(mi, QItemSelectionModel::Clear); | |
ls0->selectionModel ()->select(mi, QItemSelectionModel::Select); | |
} | |
else if (check_ == Qt::Unchecked) | |
{ | |
//ls0->selectionModel ()->select(mi, QItemSelectionModel::Clear); | |
ls0->selectionModel ()->select(mi,QItemSelectionModel::Deselect ); | |
} | |
} | |
ls0->selectionModel ()->select (topmodel,QItemSelectionModel::Select); | |
} | |
void viewmodel::hidepar() | |
{ | |
} | |
void My1SqlModel::reboot() | |
{ | |
My1SqlModel::setQuery(My1SqlModel::query().lastQuery()); | |
// while (sqlmodel->canFetchMore ()) | |
// { | |
// sqlmodel->fetchMore (); | |
// } | |
} | |
My1SqlModel::My1SqlModel() : | |
QSqlQueryModel() | |
{ | |
} | |
bool My1SqlModel::setData(const QModelIndex &index, const QVariant &value, int role) | |
{ | |
if (role == Qt::CheckStateRole&&index.column()==0) | |
{ | |
bool state = (value==Qt::Checked?true:false); | |
if (boolhash.contains (index)){ | |
boolhash.remove (index); | |
}else{ | |
boolhash.insert(index,state); | |
} | |
} | |
return true; | |
} | |
Qt::ItemFlags My1SqlModel::flags(const QModelIndex &index) const | |
{ | |
return (QSqlQueryModel::flags(index)|Qt::ItemIsUserCheckable); | |
} | |
QVariant My1SqlModel::data(const QModelIndex &index,int role) const { | |
QVariant value=QSqlQueryModel::data(index,role); | |
if (iscolumnwithcheck.contains (index.column()) && role==Qt::CheckStateRole) { | |
if (boolhash.contains (index)){ | |
return (boolhash.value (index)) ? Qt::Checked : Qt::Unchecked; | |
}else{ | |
return Qt::Unchecked; | |
} | |
} else { | |
return value; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef VIEWMODEL_H | |
#define VIEWMODEL_H | |
#include <QWidget> | |
#include <QtCore> | |
#include <QtSql/QSqlDatabase> | |
#include <QtSql/QSqlRecord> | |
#include <QtSql/QSqlQuery> | |
#include <QtSql/qsql.h> | |
#include <QtSql/QSqlError> | |
#include <QDebug> | |
#include <QTextCodec> | |
#include <ActiveQt/qaxobject.h> | |
#include <ActiveQt/qaxbase.h> | |
#include <QMessageBox> | |
#include <QFileDialog> | |
#include <QSettings> | |
#include <QGraphicsTextItem> | |
#include <QGraphicsView> | |
#include <QDesktopWidget> | |
#include <QTextEdit> | |
#include <QListWidget> | |
#include <QToolTip> | |
#include <QFrame> | |
#include <QLineEdit> | |
#include <QTime> | |
#include <QTimer> | |
#include <QSignalMapper> | |
#include <QToolTip> | |
#include <QCheckBox> | |
#include <QDateEdit> | |
#include <QFormLayout> | |
#include <QPushButton> | |
#include <QLCDNumber> | |
#include <QSlider> | |
#include <QTableWidget> | |
#include <QTableWidgetItem> | |
#include <QtMath> | |
#include <QProcess> | |
#include <QThread> | |
#include <QtConcurrent/QtConcurrent> | |
#include <QFuture> | |
#include <QFutureWatcher> | |
#include <QProgressDialog> | |
#include <QProgressBar> | |
#include <QListView> | |
#include <QTableView> | |
#include <QTableWidget> | |
#include <QStandardItem> | |
#include <QSqlTableModel> | |
#include "database.h" | |
#include <QFileDialog> | |
#include <QSettings> | |
#include <QInputDialog> | |
#include <QLineEdit> | |
#include <QLabel> | |
class viewmodel : public QWidget | |
{ | |
Q_OBJECT | |
public: | |
explicit viewmodel(QWidget *parent = 0); | |
~viewmodel(); | |
signals: | |
public slots: | |
void serverCycle(); | |
void testt(); | |
void opendb(); | |
void add_v(); | |
void update1(); | |
void view_params(const QModelIndex & model); | |
void test01(const QModelIndex & model, const QModelIndex & bottommodel); | |
void selecting_(const QModelIndex &topmodel); | |
void test02(const QModelIndex &topmodel, const QModelIndex &bottommodel); | |
void hidepar(); | |
}; | |
class My1SqlModel : public QSqlQueryModel | |
{ | |
public: | |
void reboot(); | |
QList<int> iscolumnwithcheck; | |
QHash<QModelIndex,bool> boolhash; | |
My1SqlModel(); | |
bool setData (const QModelIndex &index, const QVariant &value, int role); | |
Qt::ItemFlags flags(const QModelIndex &index) const; | |
QVariant data(const QModelIndex &index, int role) const; | |
// QVariant headerData (int section,Qt::Orientation orientation,int role) const; | |
}; | |
#endif // VIEWMODEL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "database.h" | |
DataBase::DataBase() | |
{ | |
DataBase::db = QSqlDatabase::addDatabase(QLatin1String("QODBC")); | |
} | |
void DataBase::open(QString path,QString file_mask) | |
{ | |
QString connect = QString("DRIVER={Microsoft Access Driver (%2)};FIL={MS Access};DBQ=%1;").arg(path).arg(file_mask); //*.mdb | |
DataBase::db.setDatabaseName(connect); | |
if (!DataBase::db.open()) { | |
qDebug() << QString("DataBase Error: \n [%1] D'NOT Open!").arg(DataBase::path_db); | |
} | |
DataBase::path_db = path; | |
} | |
void DataBase::close() | |
{ | |
DataBase::db.close (); | |
} | |
void DataBase::do_this(QString query) | |
{ | |
//? ? | |
QSqlQuery my_query(DataBase::db); | |
//my_query.prepare(query); | |
// int param; | |
// if (param_list.size()==0) {param=-1;} | |
// else | |
// {param=param_list.size();} | |
// for (int i = 0; i < param; ++i) | |
// { | |
// my_query.addBindValue(param_list.at(i)); | |
// } | |
my_query.exec (query); | |
} | |
QList<QStringList> DataBase::db_in_list(QString query) | |
{ | |
QList<QStringList> list; | |
QStringList strlist; | |
QSqlQuery my_query(DataBase::db); | |
// my_query.prepare(query); | |
// int param; | |
// if (param_list.size()==0) {param=-1;} | |
// else | |
// {param=param_list.size();} | |
// qDebug() << param; | |
// for (int i = 0; i < param; ++i) | |
// { | |
// my_query.addBindValue(param_list.at(i)); | |
// qDebug() << param_list.at(i); | |
// } | |
my_query.exec (query); | |
// qDebug() << my_query.executedQuery (); | |
if (my_query.isActive()) { | |
QSqlRecord rec; | |
while (my_query.next()) { | |
rec = my_query.record(); | |
for (int var = 0; var < rec.count(); ++var) { | |
strlist << my_query.value (var).toString (); | |
} | |
list << strlist; | |
strlist.clear (); | |
} | |
} | |
return list; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef DATABASE_H | |
#define DATABASE_H | |
#include <QtSql/QSqlDatabase> | |
#include <QtSql/QSqlRecord> | |
#include <QtSql/QSqlQuery> | |
#include <QtSql/QSql> | |
#include <QtSql/QSqlError> | |
#include <QtSql/QSqlQueryModel> | |
#include <QList> | |
#include <QStringList> | |
#include <QtCore> | |
class DataBase | |
{ | |
public: | |
DataBase(); | |
void open(QString, QString); | |
void close(); | |
void do_this(QString); | |
QList<QStringList> db_in_list(QString); | |
QString path_db; | |
QSqlDatabase db; | |
}; | |
#endif // DATABASE_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment