#include "MainWindow.h" MainWindow* mw; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); lineCognome = this->findChild("lineCognome"); lineNome = this->findChild("lineNome"); tableSoci = this->findChild("tableSoci"); } void MainWindow::connectDatabase(void) { /* retrieve settings for this application, group database */ QSettings settings("it.linux.golem", "gestionale"); 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()); query = QSqlQuery(db); 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())); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_buttonCerca_clicked() { if (!db.open()) connectDatabase(); 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(lineNome->text()) ); query.bindValue( ":cognome", QString("%%1%").arg(lineCognome->text()) ); if ( ! query.exec() ) { qDebug() << query.lastQuery(); return; } if ( query.size() == 0) { status(Ui::INFO, "no results found"); return; } if (sociModel != nullptr) delete sociModel; // TODO -- does this look a cunning thing? sociModel = new SociModel(nullptr, query); status(Ui::INFO, QString("%1 results found").arg(query.size())); tableSoci->setModel(sociModel); /* the signal is automagically added by some obscure qt build system, provided the function is named in a standard way */ //QObject::connect(tableSoci, SIGNAL(clicked(const QModelIndex&)), this, SLOT(on_tableSoci_clicked(const QModelIndex&))); tableSoci->show(); } void MainWindow::on_buttonNuovo_clicked() { /* TODO -- please implement me thanks */;} void MainWindow::on_tableSoci_clicked(const QModelIndex& index) { if (index.isValid()) { /* extracts id socio from first column of table view */ int id = index.model()->data(index.siblingAtColumn(0)).toInt(); if (id != 0) { /* if click is not on other fields than id... */ EditWindow* w = new EditWindow(id, this); w->show(); // TODO -- free() this window at the end... maybe you can use a self Signal/Slot? } } }