Basic socio search + edit draft

This commit is contained in:
giomba 2019-08-05 23:52:33 +02:00
parent 9f0569894b
commit 61059d12c8
9 changed files with 288 additions and 22 deletions

21
SociModel.cpp Normal file
View File

@ -0,0 +1,21 @@
#include "SociModel.h"
SociModel::SociModel(QObject* parent, QSqlQuery& query) : QAbstractTableModel(parent), query(query) {
;
}
int SociModel::rowCount(const QModelIndex&) const {
return query.size();
}
int SociModel::columnCount(const QModelIndex&) const {
return query.record().count();
}
QVariant SociModel::data(const QModelIndex& index, int role) const {
if (role == Qt::DisplayRole) {
query.seek(index.row());
return query.value(index.column()).toString();
}
return QVariant();
}

21
SociModel.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef SOCIMODEL_H
#define SOCIMODEL_H
#include <QAbstractTableModel>
#include <QDebug>
#include <QSqlQuery>
#include <QSqlRecord>
class SociModel : public QAbstractTableModel {
Q_OBJECT
private:
QSqlQuery& query;
public:
SociModel(QObject* parent, QSqlQuery& query);
int rowCount(const QModelIndex& parent = QModelIndex()) const;
int columnCount(const QModelIndex& parent = QModelIndex()) const;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
};
#endif

View File

@ -1,11 +1,9 @@
#include "editwindow.h"
EditWindow::EditWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::EditWindow)
{
EditWindow::EditWindow(int idSocio, QWidget* parent) : QMainWindow(parent), ui(new Ui::EditWindow) {
ui->setupUi(this);
/* retrieve all elements of the GUI */
lineID = this->findChild<QLineEdit*>("lineID");
lineNome = this->findChild<QLineEdit*>("lineNome");
lineCognome = this->findChild<QLineEdit*>("lineCognome");
@ -17,10 +15,7 @@ EditWindow::EditWindow(QWidget *parent) :
comboFonte = this->findChild<QComboBox*>("comboFonte");
checkAbilitaQuestionario = this->findChild<QCheckBox*>("checkAbilitaQuestionario");
populateUi();
}
bool EditWindow::populateUi(void) {
/* retrieve professione and fonte from database */
query.prepare("SELECT id, professione FROM professione ORDER BY professione");
query.exec();
while (query.next()) {
@ -33,18 +28,24 @@ bool EditWindow::populateUi(void) {
comboFonte->addItem(query.value(1).toString(), query.value(0).toInt());
}
return true;
/* populate fields from database */
query.prepare("SELECT s.id, s.nome, s.cognome, \
s.dataNascita, s.comuneResidenza, s.email, s.professione, s.fonte, s.abilitaQuestionario, \
q.dataCompilazione \
FROM socio AS s LEFT OUTER JOIN questionario AS q ON s.id = q.socio \
WHERE s.id = :id");
query.bindValue( ":id", idSocio );
query.exec();
query.first();
showFound(); // TODO
}
/*
void EditWindow::on_buttonCerca_clicked() {
QLineEdit* lineCognome = this->findChild<QLineEdit*>("lineCognome");
query.prepare("SELECT s.id, s.nome, s.cognome, \
s.dataNascita, s.comuneResidenza, s.email, s.professione, s.fonte, s.abilitaQuestionario, \
q.dataCompilazione \
FROM socio AS s LEFT OUTER JOIN questionario AS q ON s.id = q.socio \
WHERE s.cognome LIKE :cognome");
query.bindValue( ":cognome", QString("%%1%").arg(lineCognome->text()) );
if ( ! query.exec() ) { setStatus(Ui::ERROR, "query error"); qDebug() << query.lastQuery(); return; }
if ( query.size() == 0) { setStatus(Ui::INFO, "no results found"); return; }
@ -53,6 +54,7 @@ void EditWindow::on_buttonCerca_clicked() {
query.first();
showFound();
}
*/
void EditWindow::showFound() {
lineID->setText(query.value( query.record().indexOf("id") ).toString());

View File

@ -23,18 +23,16 @@ namespace Ui {
};
}
class EditWindow : public QMainWindow
{
class EditWindow : public QMainWindow {
Q_OBJECT
public:
explicit EditWindow(QWidget *parent = nullptr);
explicit EditWindow(int idSocio, QWidget *parent);
~EditWindow();
void setStatus(Ui::StatusType type, QString message);
private slots:
void on_buttonCerca_clicked();
void on_buttonNuovo_clicked();
void on_buttonModifica_clicked();
void on_buttonSuccessivo_clicked();

View File

@ -26,16 +26,23 @@ CONFIG += c++11
SOURCES += \
db.cpp \
editwindow.cpp \
fullquery.cpp \
main.cpp \
editwindow.cpp
mainwindow.cpp \
SociModel.cpp
HEADERS += \
db.h \
editwindow.h \
fullquery.h \
editwindow.h
mainwindow.h \
SociModel.h
FORMS += \
mainwindow.ui \
editwindow.ui
# Default rules for deployment.

View File

@ -5,6 +5,7 @@
#include "db.h"
#include "editwindow.h"
#include "mainwindow.h"
int main(int argc, char *argv[])
{
@ -25,7 +26,7 @@ int main(int argc, char *argv[])
msgBox.exec();
}
EditWindow w;
MainWindow w;
w.show();
return a.exec();

46
mainwindow.cpp Normal file
View File

@ -0,0 +1,46 @@
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
lineCognome = this->findChild<QLineEdit*>("lineCognome");
lineNome = this->findChild<QLineEdit*>("lineCognome");
tableSoci = this->findChild<QTableView*>("tableSoci");
}
MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::on_buttonCerca_clicked() {
query.prepare("SELECT id, nome, cognome FROM socio WHERE cognome LIKE :cognome");
query.bindValue( ":cognome", QString("%%1%").arg(lineCognome->text()) );
if ( ! query.exec() ) { qDebug() << query.lastQuery(); return; }
//if ( query.size() == 0) { setStatus(Ui::INFO, "no results found"); return; }
if (sociModel != nullptr) delete sociModel; // TODO -- does this look a cunning thing?
sociModel = new SociModel(nullptr, query);
//setStatus(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()) {
int id = index.data().toInt(); /* retrieve id socio */
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?
}
}
}

44
mainwindow.h Normal file
View File

@ -0,0 +1,44 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QDebug>
#include <QSqlQuery>
#include <QTableWidgetItem>
#include "editwindow.h"
#include "mainwindow.h"
#include "SociModel.h"
#include "ui_mainwindow.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
//void setStatus(Ui::StatusType type, QString message);
private slots:
void on_buttonCerca_clicked();
void on_buttonNuovo_clicked();
void on_tableSoci_clicked(const QModelIndex& index);
private:
SociModel* sociModel = nullptr;
QLineEdit* lineNome = nullptr;
QLineEdit* lineCognome = nullptr;
QTableView* tableSoci = nullptr;
// QDateEdit* dateDataNascita = nullptr;
QSqlQuery query;
Ui::MainWindow *ui;
};
#endif

126
mainwindow.ui Normal file
View File

@ -0,0 +1,126 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>635</width>
<height>362</height>
</rect>
</property>
<property name="windowTitle">
<string>Main Window</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="buttonCerca">
<property name="geometry">
<rect>
<x>430</x>
<y>20</y>
<width>91</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>Cerca</string>
</property>
</widget>
<widget class="QLineEdit" name="lineNome">
<property name="geometry">
<rect>
<x>10</x>
<y>20</y>
<width>201</width>
<height>32</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="buttonNuovo">
<property name="geometry">
<rect>
<x>530</x>
<y>20</y>
<width>91</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>Nuovo</string>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>10</x>
<y>0</y>
<width>38</width>
<height>18</height>
</rect>
</property>
<property name="text">
<string>Nome</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>220</x>
<y>0</y>
<width>60</width>
<height>18</height>
</rect>
</property>
<property name="text">
<string>Cognome</string>
</property>
</widget>
<widget class="QLineEdit" name="lineCognome">
<property name="geometry">
<rect>
<x>220</x>
<y>20</y>
<width>201</width>
<height>32</height>
</rect>
</property>
</widget>
<widget class="QTableView" name="tableSoci">
<property name="geometry">
<rect>
<x>10</x>
<y>60</y>
<width>611</width>
<height>241</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>635</width>
<height>30</height>
</rect>
</property>
<widget class="QMenu" name="menuwhat_s_this">
<property name="title">
<string>File</string>
</property>
<addaction name="actionExit"/>
</widget>
<addaction name="menuwhat_s_this"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="actionExit">
<property name="text">
<string>Exit</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>