fixed MVC for QSqlQuery
This commit is contained in:
parent
0046817a49
commit
a5e6b72aca
|
@ -10,8 +10,8 @@
|
|||
#include <iostream>
|
||||
|
||||
#include "db.h"
|
||||
#include "fullQuery.h"
|
||||
#include "EditWindow.h"
|
||||
#include "fullQuery.h"
|
||||
#include "status.h"
|
||||
#include "ui_EditWindow.h"
|
||||
|
||||
|
|
|
@ -17,7 +17,6 @@ void MainWindow::connectDatabase(void) {
|
|||
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()));
|
||||
|
||||
|
@ -34,6 +33,7 @@ MainWindow::~MainWindow() {
|
|||
void MainWindow::on_buttonCerca_clicked() {
|
||||
if (!db.open()) connectDatabase();
|
||||
|
||||
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()) );
|
||||
|
@ -41,12 +41,11 @@ void MainWindow::on_buttonCerca_clicked() {
|
|||
if (! query.exec()) { status(Ui::ERROR, fullQuery(query)); 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);
|
||||
model.setQuery(query);
|
||||
|
||||
status(Ui::INFO, QString("%1 results found").arg(query.size()));
|
||||
|
||||
ui->tableSoci->setModel(sociModel);
|
||||
ui->tableSoci->setModel(&model);
|
||||
/* 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&)));
|
||||
ui->tableSoci->show();
|
||||
|
@ -55,6 +54,7 @@ void MainWindow::on_buttonCerca_clicked() {
|
|||
void MainWindow::on_buttonNuovo_clicked() {
|
||||
if (! db.open()) connectDatabase();
|
||||
|
||||
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());
|
||||
|
|
|
@ -8,7 +8,6 @@
|
|||
|
||||
#include "EditWindow.h"
|
||||
#include "MainWindow.h"
|
||||
#include "SociModel.h"
|
||||
#include "ui_MainWindow.h"
|
||||
|
||||
namespace Ui {
|
||||
|
@ -18,6 +17,12 @@ namespace Ui {
|
|||
class MainWindow : public QMainWindow {
|
||||
Q_OBJECT
|
||||
|
||||
private:
|
||||
Ui::MainWindow *ui;
|
||||
QSqlQueryModel model;
|
||||
|
||||
void connectDatabase(void);
|
||||
|
||||
public:
|
||||
explicit MainWindow(QWidget *parent = nullptr);
|
||||
~MainWindow();
|
||||
|
@ -28,14 +33,6 @@ private slots:
|
|||
void on_buttonCerca_clicked();
|
||||
void on_buttonNuovo_clicked();
|
||||
void on_tableSoci_clicked(const QModelIndex& index);
|
||||
|
||||
private:
|
||||
SociModel* sociModel = nullptr;
|
||||
QSqlQuery query;
|
||||
|
||||
Ui::MainWindow *ui;
|
||||
|
||||
void connectDatabase(void);
|
||||
};
|
||||
|
||||
extern MainWindow* mw;
|
||||
|
|
|
@ -6,103 +6,64 @@
|
|||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>635</width>
|
||||
<height>362</height>
|
||||
<width>623</width>
|
||||
<height>421</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Main Window</string>
|
||||
<string>Gestionale Argento</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>
|
||||
<layout class="QGridLayout" name="gridLayout_2">
|
||||
<item row="0" column="0">
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
<string>Nome</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QLabel" name="label_2">
|
||||
<property name="text">
|
||||
<string>Cognome</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QLineEdit" name="lineNome"/>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="QLineEdit" name="lineCognome"/>
|
||||
</item>
|
||||
<item row="1" column="2">
|
||||
<widget class="QPushButton" name="buttonCerca">
|
||||
<property name="text">
|
||||
<string>Cerca</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="3">
|
||||
<widget class="QPushButton" name="buttonNuovo">
|
||||
<property name="text">
|
||||
<string>Nuovo</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="0" colspan="4">
|
||||
<widget class="QTableView" name="tableSoci"/>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QMenuBar" name="menubar">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>635</width>
|
||||
<width>623</width>
|
||||
<height>30</height>
|
||||
</rect>
|
||||
</property>
|
||||
|
|
|
@ -1,21 +0,0 @@
|
|||
#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();
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
#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
|
|
@ -26,20 +26,18 @@ CONFIG += c++11
|
|||
SOURCES += \
|
||||
db.cpp \
|
||||
EditWindow.cpp \
|
||||
fullQuery.cpp \
|
||||
main.cpp \
|
||||
MainWindow.cpp \
|
||||
status.cpp \
|
||||
SociModel.cpp
|
||||
fullQuery.cpp
|
||||
|
||||
|
||||
HEADERS += \
|
||||
db.h \
|
||||
EditWindow.h \
|
||||
fullQuery.h \
|
||||
MainWindow.h \
|
||||
status.h \
|
||||
SociModel.h
|
||||
fullQuery.h
|
||||
|
||||
|
||||
FORMS += \
|
||||
|
|
Loading…
Reference in New Issue