#include "SociListTab.h" #include "ui_SociListTab.h" SociListTab::SociListTab(QWidget *parent) : QWidget(parent), ui(new Ui::SociListTab) { ui->setupUi(this); } /* void SociListTab::connectDatabase(void) { // retrieve settings for this application, group database QSettings settings("it.linux.golem", "argento"); settings.beginGroup("database"); // open database and attach query db = QSqlDatabase::addDatabase("QMYSQL"); // QMYSQL <-- database driver db.setHostName(settings.value("hostname").toString()); db.setDatabaseName(settings.value("database").toString()); db.setUserName(settings.value("username").toString()); db.setPassword(settings.value("password").toString()); status(Ui::INFO, QString("Connecting to %1...").arg(settings.value("hostname").toString())); if (! db.open()) status(Ui::ERROR, db.lastError().text()); else status(Ui::INFO, QString("Connected to %1").arg(settings.value("hostname").toString())); } */ void SociListTab::on_buttonCerca_clicked() { QSqlQuery query; query.prepare("SELECT id, nome, cognome FROM socio WHERE LOWER(nome) LIKE LOWER(:nome) AND LOWER(cognome) LIKE LOWER(:cognome)"); query.bindValue( ":nome", QString("%%1%").arg(ui->lineNome->text()) ); query.bindValue( ":cognome", QString("%%1%").arg(ui->lineCognome->text()) ); if (! query.exec()) { qDebug() << "query not executed: " << query.lastError().text(); status(Ui::ERROR, fullQuery(query)); return; } if ( query.size() == 0) { qDebug() << "size == 0"; status(Ui::INFO, "no results found"); return; } model.setQuery(query); status(Ui::INFO, QString("%1 results found").arg(query.size())); ui->tableSoci->setModel(&model); ui->tableSoci->show(); } void SociListTab::on_buttonNuovo_clicked() { QSqlQuery query; query.prepare("INSERT INTO socio (nome, cognome) VALUES (NULLIF(:nome, ''), NULLIF(:cognome, ''))"); query.bindValue(":nome", ui->lineNome->text()); query.bindValue(":cognome", ui->lineCognome->text()); if (! query.exec()) { status(Ui::ERROR, fullQuery(query)); return; } if (! query.lastInsertId().isValid()) { status(Ui::ERROR, fullQuery(query)); return; } EditWindow*w = new EditWindow(query.lastInsertId().toInt(), this); w->setAttribute(Qt::WA_DeleteOnClose); w->show(); } void SociListTab::on_tableSoci_clicked(const QModelIndex& index) { if (index.isValid()) { /* extracts id socio from first column of table view */ int id = index.model()->data(index.sibling(index.row(), 0)).toInt(); if (id != 0) { /* if click is not on other fields than id... */ EditWindow* w = new EditWindow(id, this); w->setAttribute(Qt::WA_DeleteOnClose); /* delete window at the end */ w->show(); } } }