tabbed UI
This commit is contained in:
parent
a5e6b72aca
commit
dcdcb6f3dd
|
@ -1,10 +1,10 @@
|
|||
#include "EditWindow.h"
|
||||
#include "ui_EditWindow.h"
|
||||
|
||||
EditWindow::EditWindow(int idSocio, QWidget* parent) : QMainWindow(parent), ui(new Ui::EditWindow) {
|
||||
ui->setupUi(this);
|
||||
|
||||
/* connect query with common global database */
|
||||
query = QSqlQuery(db);
|
||||
QSqlQuery query;
|
||||
|
||||
model = new QSqlRelationalTableModel(this);
|
||||
model->setTable("socio");
|
||||
|
@ -21,7 +21,7 @@ EditWindow::EditWindow(int idSocio, QWidget* parent) : QMainWindow(parent), ui(n
|
|||
|
||||
if (! model->select()) {
|
||||
qDebug() << fullQuery(query);
|
||||
qDebug() << db.lastError().text();
|
||||
qDebug() << query.lastError().text();
|
||||
Ui::status(Ui::ERROR, "query error");
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
#ifndef EDITWINDOW_H
|
||||
#define EDITWINDOW_H
|
||||
#pragma once
|
||||
|
||||
#include <QDebug>
|
||||
#include <QMainWindow>
|
||||
|
@ -13,7 +12,6 @@
|
|||
#include "EditWindow.h"
|
||||
#include "fullQuery.h"
|
||||
#include "status.h"
|
||||
#include "ui_EditWindow.h"
|
||||
|
||||
namespace Ui {
|
||||
class EditWindow;
|
||||
|
@ -37,6 +35,3 @@ private:
|
|||
|
||||
Ui::EditWindow *ui;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -1,81 +1,15 @@
|
|||
#include "MainWindow.h"
|
||||
#include "ui_MainWindow.h"
|
||||
|
||||
MainWindow* mw;
|
||||
QStatusBar* statusBar = nullptr;
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
|
||||
ui->setupUi(this);
|
||||
}
|
||||
|
||||
void MainWindow::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()));
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow() {
|
||||
delete ui;
|
||||
}
|
||||
|
||||
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()) );
|
||||
|
||||
if (! query.exec()) { status(Ui::ERROR, fullQuery(query)); return; }
|
||||
if ( query.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);
|
||||
/* 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();
|
||||
}
|
||||
|
||||
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());
|
||||
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 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.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();
|
||||
}
|
||||
|
||||
QSqlDatabase db = QSqlDatabase::database();
|
||||
if (db.isOpen()) {
|
||||
status(Ui::INFO, QString("Connected to %1").arg(db.hostName()));
|
||||
} else {
|
||||
status(Ui::ERROR, "Cannot connect to database: " + db.lastError().text());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,14 +1,12 @@
|
|||
#ifndef MAINWINDOW_H
|
||||
#define MAINWINDOW_H
|
||||
#pragma once
|
||||
|
||||
#include <QDebug>
|
||||
#include <QSqlQuery>
|
||||
#include <QSettings>
|
||||
#include <QTableWidgetItem>
|
||||
|
||||
#include "SociListTab.h"
|
||||
#include "EditWindow.h"
|
||||
#include "MainWindow.h"
|
||||
#include "ui_MainWindow.h"
|
||||
|
||||
namespace Ui {
|
||||
class MainWindow;
|
||||
|
@ -21,20 +19,11 @@ private:
|
|||
Ui::MainWindow *ui;
|
||||
QSqlQueryModel model;
|
||||
|
||||
void connectDatabase(void);
|
||||
|
||||
public:
|
||||
explicit MainWindow(QWidget *parent = nullptr);
|
||||
~MainWindow();
|
||||
|
||||
//void setStatus(Ui::StatusType type, QString message);
|
||||
MainWindow(QWidget *parent = nullptr);
|
||||
|
||||
private slots:
|
||||
void on_buttonCerca_clicked();
|
||||
void on_buttonNuovo_clicked();
|
||||
void on_tableSoci_clicked(const QModelIndex& index);
|
||||
|
||||
};
|
||||
|
||||
extern MainWindow* mw;
|
||||
|
||||
#endif
|
||||
extern QStatusBar* statusBar;
|
||||
|
|
|
@ -16,48 +16,30 @@
|
|||
<widget class="QWidget" name="centralwidget">
|
||||
<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>
|
||||
<widget class="QTabWidget" name="tabWidget">
|
||||
<property name="currentIndex">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<widget class="QWidget" name="tab">
|
||||
<attribute name="title">
|
||||
<string>Soci</string>
|
||||
</attribute>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="0" column="0">
|
||||
<widget class="SociListTab" name="widget" native="true"/>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QWidget" name="tab_2">
|
||||
<attribute name="title">
|
||||
<string>Rinnovi</string>
|
||||
</attribute>
|
||||
</widget>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QStatusBar" name="statusBar"/>
|
||||
<widget class="QMenuBar" name="menubar">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
|
@ -75,13 +57,20 @@
|
|||
</widget>
|
||||
<addaction name="menuwhat_s_this"/>
|
||||
</widget>
|
||||
<widget class="QStatusBar" name="statusBar"/>
|
||||
<action name="actionExit">
|
||||
<property name="text">
|
||||
<string>Exit</string>
|
||||
</property>
|
||||
</action>
|
||||
</widget>
|
||||
<customwidgets>
|
||||
<customwidget>
|
||||
<class>SociListTab</class>
|
||||
<extends>QWidget</extends>
|
||||
<header>SociListTab.h</header>
|
||||
<container>1</container>
|
||||
</customwidget>
|
||||
</customwidgets>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
#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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
#pragma once
|
||||
|
||||
#include <QDebug>
|
||||
#include <QSqlQuery>
|
||||
#include <QSqlQueryModel>
|
||||
#include <QSettings>
|
||||
#include <QTableWidgetItem>
|
||||
|
||||
#include "EditWindow.h"
|
||||
|
||||
#include "status.h"
|
||||
|
||||
namespace Ui {
|
||||
class SociListTab;
|
||||
}
|
||||
|
||||
class SociListTab : public QWidget {
|
||||
Q_OBJECT
|
||||
|
||||
private:
|
||||
Ui::SociListTab *ui;
|
||||
QSqlQueryModel model;
|
||||
|
||||
public:
|
||||
SociListTab(QWidget *parent = nullptr);
|
||||
|
||||
private slots:
|
||||
void on_buttonCerca_clicked();
|
||||
void on_buttonNuovo_clicked();
|
||||
void on_tableSoci_clicked(const QModelIndex& index);
|
||||
};
|
|
@ -0,0 +1,71 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>SociListTab</class>
|
||||
<widget class="QWidget" name="MainWindow">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>623</width>
|
||||
<height>421</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Gestionale Argento</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout_3">
|
||||
<item row="0" column="0">
|
||||
<layout class="QGridLayout" name="gridLayout_2">
|
||||
<item row="0" column="0">
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<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="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="1" column="1">
|
||||
<widget class="QLineEdit" name="lineCognome"/>
|
||||
</item>
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
<string>Nome</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="0" colspan="4">
|
||||
<widget class="QTableView" name="tableSoci"/>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
<action name="actionExit">
|
||||
<property name="text">
|
||||
<string>Exit</string>
|
||||
</property>
|
||||
</action>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
|
@ -24,25 +24,26 @@ DEFINES += QT_DEPRECATED_WARNINGS
|
|||
CONFIG += c++11
|
||||
|
||||
SOURCES += \
|
||||
db.cpp \
|
||||
EditWindow.cpp \
|
||||
main.cpp \
|
||||
MainWindow.cpp \
|
||||
status.cpp \
|
||||
fullQuery.cpp
|
||||
fullQuery.cpp \
|
||||
SociListTab.cpp
|
||||
|
||||
|
||||
HEADERS += \
|
||||
db.h \
|
||||
EditWindow.h \
|
||||
MainWindow.h \
|
||||
status.h \
|
||||
fullQuery.h
|
||||
fullQuery.h \
|
||||
SociListTab.h
|
||||
|
||||
|
||||
FORMS += \
|
||||
MainWindow.ui \
|
||||
EditWindow.ui
|
||||
EditWindow.ui \
|
||||
SociListTab.ui
|
||||
|
||||
# Default rules for deployment.
|
||||
qnx: target.path = /tmp/$${TARGET}/bin
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
#include "db.h"
|
||||
|
||||
QSqlDatabase db;
|
12
argento/db.h
12
argento/db.h
|
@ -1,12 +0,0 @@
|
|||
#ifndef DB_H
|
||||
#define DB_H
|
||||
|
||||
#include <QSqlDatabase>
|
||||
#include <QSqlError>
|
||||
#include <QSqlQuery>
|
||||
#include <QSqlRecord>
|
||||
|
||||
extern QSqlDatabase db;
|
||||
extern QSqlQuery query;
|
||||
|
||||
#endif
|
|
@ -10,12 +10,22 @@ int main(int argc, char *argv[])
|
|||
{
|
||||
QApplication a(argc, argv);
|
||||
|
||||
mw = new MainWindow;
|
||||
mw->show();
|
||||
/* retrieve settings for this application, group database */
|
||||
QSettings settings("it.linux.golem", "argento");
|
||||
settings.beginGroup("database");
|
||||
|
||||
int ret = a.exec();
|
||||
/* open database and attach query */
|
||||
QSqlDatabase 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());
|
||||
|
||||
delete mw;
|
||||
db.open();
|
||||
|
||||
return ret;
|
||||
MainWindow window;
|
||||
statusBar = window.statusBar();
|
||||
window.show();
|
||||
|
||||
return a.exec();
|
||||
}
|
||||
|
|
|
@ -3,8 +3,7 @@
|
|||
namespace Ui {
|
||||
|
||||
void status(StatusType type, QString message) {
|
||||
//QStatusBar* statusBar = mw->findChild<QStatusBar*>("statusBar");
|
||||
QStatusBar* statusBar = mw->statusBar();
|
||||
//QStatusBar* statusBar = mw->statusBar();
|
||||
QString prefix;
|
||||
int timeout = 0;
|
||||
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
#ifndef STATUS_H
|
||||
#define STATUS_H
|
||||
#pragma once
|
||||
|
||||
#include <QStatusBar>
|
||||
#include <QString>
|
||||
|
@ -19,4 +18,3 @@ namespace Ui {
|
|||
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue