rename db column to not conflict with qml. Make sure to call select on first load of table models to poulate.

Make model classes have some functions accessible from qml. Add custom model method that allows QML to easily add new items.
Add Card layout that contains data from db.
This commit is contained in:
2020-06-07 22:36:32 -04:00
parent 6edccb050b
commit 5b8cd46e80
6 changed files with 61 additions and 9 deletions

View File

@@ -34,10 +34,8 @@ Kirigami.ApplicationWindow {
} }
] ]
title: "Vehicles" title: "Vehicles"
Kirigami.CardsListView { Kirigami.CardsListView {
Layouts.Layout.fillWidth: true anchors.fill: parent
Layouts.Layout.fillHeight: true
id: vehicleView id: vehicleView
model: vehicleModel model: vehicleModel
delegate: Kirigami.Card { delegate: Kirigami.Card {
@@ -45,6 +43,28 @@ Kirigami.ApplicationWindow {
banner { banner {
title: name title: name
} }
header: Row {
layoutDirection: Qt.RightToLeft
topPadding: 10.0
rightPadding: 10.0
Controls.Label {
text: vin
}
}
contentItem: Item{
implicitHeight: Kirigami.Units.gridUnit * 4
Layouts.ColumnLayout {
Controls.Label {
text: maker
}
Controls.Label {
text: vmodel
}
Controls.Label {
text: year
}
}
}
} }
Controls.ScrollBar.vertical: Controls.ScrollBar {} Controls.ScrollBar.vertical: Controls.ScrollBar {}
} }
@@ -73,13 +93,14 @@ Kirigami.ApplicationWindow {
name: "addvehicle" name: "addvehicle"
Component { Component {
Kirigami.Page { Kirigami.Page {
id: vehicleAddPage
title: qsTr("Add Vehicle") title: qsTr("Add Vehicle")
Layouts.ColumnLayout { Layouts.ColumnLayout {
anchors.fill: parent anchors.fill: parent
Kirigami.FormLayout { Kirigami.FormLayout {
Layouts.Layout.alignment: Qt.AlignHCenter Layouts.Layout.alignment: Qt.AlignHCenter
Layouts.Layout.fillWidth: true Layouts.Layout.fillWidth: true
width: page.width width: vehicleAddPage.width
Item { Item {
Kirigami.FormData.isSection: true Kirigami.FormData.isSection: true
@@ -87,21 +108,25 @@ Kirigami.ApplicationWindow {
} }
Controls.TextField { Controls.TextField {
id: nameField
selectByMouse: true selectByMouse: true
Kirigami.FormData.label: qsTr("Name")+":" Kirigami.FormData.label: qsTr("Name")+":"
} }
Controls.TextField { Controls.TextField {
id: makeField
selectByMouse: true selectByMouse: true
Kirigami.FormData.label: qsTr("Make")+":" Kirigami.FormData.label: qsTr("Make")+":"
} }
Controls.TextField { Controls.TextField {
id: modelField
selectByMouse: true selectByMouse: true
Kirigami.FormData.label: qsTr("Model")+":" Kirigami.FormData.label: qsTr("Model")+":"
} }
Controls.TextField { Controls.TextField {
id: yearField
selectByMouse: true selectByMouse: true
Kirigami.FormData.label: qsTr("Year")+":" Kirigami.FormData.label: qsTr("Year")+":"
inputMethodHints: Qt.ImhDigitsOnly inputMethodHints: Qt.ImhDigitsOnly
@@ -112,6 +137,7 @@ Kirigami.ApplicationWindow {
} }
Controls.TextField { Controls.TextField {
id: vinField
selectByMouse: true selectByMouse: true
Kirigami.FormData.label: qsTr("VIN")+":" Kirigami.FormData.label: qsTr("VIN")+":"
} }
@@ -120,6 +146,7 @@ Kirigami.ApplicationWindow {
text: qsTr("Add") text: qsTr("Add")
highlighted: true highlighted: true
onClicked: { onClicked: {
vehicleModel.addNewVehicle(nameField.text, makeField.text, modelField.text, parseInt(yearField.text), "", vinField.text);
router.popRoute(); router.popRoute();
} }
} }

View File

@@ -35,7 +35,7 @@ QHash<int, QByteArray> SqlServiceRecord::roleNames() const
} }
QVariant SqlServiceRecord::data(const QModelIndex& index, int role) const QVariant SqlServiceRecord::data ( const QModelIndex& index, int role ) const
{ {
QVariant value = QSqlQueryModel::data(index, role); QVariant value = QSqlQueryModel::data(index, role);
if(role < Qt::UserRole) if(role < Qt::UserRole)

View File

@@ -29,7 +29,7 @@ class SqlServiceRecord : public QSqlTableModel
public: public:
explicit SqlServiceRecord(QObject *parent = nullptr, QSqlDatabase db = QSqlDatabase()); explicit SqlServiceRecord(QObject *parent = nullptr, QSqlDatabase db = QSqlDatabase());
QHash<int, QByteArray> roleNames() const override; QHash<int, QByteArray> roleNames() const override;
QVariant data(const QModelIndex &index, int role) const override; Q_INVOKABLE QVariant data(const QModelIndex &index, int role) const override;
}; };

View File

@@ -35,7 +35,7 @@ QHash<int, QByteArray> SqlVehicle::roleNames() const
} }
QVariant SqlVehicle::data(const QModelIndex& index, int role) const QVariant SqlVehicle::data ( const QModelIndex& index, int role ) const
{ {
QVariant value = QSqlQueryModel::data(index, role); QVariant value = QSqlQueryModel::data(index, role);
if(role < Qt::UserRole) if(role < Qt::UserRole)
@@ -51,6 +51,28 @@ QVariant SqlVehicle::data(const QModelIndex& index, int role) const
return value; return value;
} }
void SqlVehicle::addNewVehicle ( const QString name, const QString maker, const QString model, const int year, const QString image, const QString vin )
{
QSqlRecord newItem = this->record();
newItem.remove(newItem.indexOf("id"));
newItem.setValue("name", name);
newItem.setValue("maker", maker);
newItem.setValue("vmodel", model);
newItem.setValue("year", year);
newItem.setValue("image", image);
newItem.setValue("vin", vin);
this->insertRecord(-1, newItem);
if(this->submitAll()) {
printf("inserted new vehicle record");
this->database().commit();
} else {
this->database().rollback();
printf("database error");
}
}

View File

@@ -29,7 +29,8 @@ class SqlVehicle : public QSqlTableModel
public: public:
explicit SqlVehicle(QObject *parent = nullptr, QSqlDatabase db = QSqlDatabase()); explicit SqlVehicle(QObject *parent = nullptr, QSqlDatabase db = QSqlDatabase());
QHash<int, QByteArray> roleNames() const override; QHash<int, QByteArray> roleNames() const override;
QVariant data(const QModelIndex &index, int role) const override; Q_INVOKABLE QVariant data(const QModelIndex &index, int role) const override;
Q_INVOKABLE void addNewVehicle(const QString name, const QString maker, const QString model, const int year, const QString image, const QString vin);
private: private:

View File

@@ -58,7 +58,7 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
QFile(db.databaseName()).open(QIODevice::ReadWrite); QFile(db.databaseName()).open(QIODevice::ReadWrite);
//Create table structure //Create table structure
db.open(); db.open();
db.exec("CREATE TABLE vehicles (id INTEGER PRIMARY KEY, name TEXT, maker TEXT, model TEXT, year INTEGER, image TEXT, vin TEXT);"); db.exec("CREATE TABLE vehicles (id INTEGER PRIMARY KEY, name TEXT, maker TEXT, vmodel TEXT, year INTEGER, image TEXT, vin TEXT);");
db.exec("CREATE TABLE records (id INTEGER PRIMARY KEY, servicetype TEXT, date INTEGER, miles INTEGER, notes TEXT);"); db.exec("CREATE TABLE records (id INTEGER PRIMARY KEY, servicetype TEXT, date INTEGER, miles INTEGER, notes TEXT);");
db.commit(); db.commit();
db.close(); db.close();
@@ -72,6 +72,8 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
SqlVehicle vehicles(nullptr, db); SqlVehicle vehicles(nullptr, db);
SqlServiceRecord records(nullptr, db); SqlServiceRecord records(nullptr, db);
vehicles.select();
records.select();
QQmlApplicationEngine engine; QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty(QStringLiteral("appAboutData"), QVariant::fromValue(aboutData)); engine.rootContext()->setContextProperty(QStringLiteral("appAboutData"), QVariant::fromValue(aboutData));