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:
@@ -34,10 +34,8 @@ Kirigami.ApplicationWindow {
|
||||
}
|
||||
]
|
||||
title: "Vehicles"
|
||||
|
||||
Kirigami.CardsListView {
|
||||
Layouts.Layout.fillWidth: true
|
||||
Layouts.Layout.fillHeight: true
|
||||
anchors.fill: parent
|
||||
id: vehicleView
|
||||
model: vehicleModel
|
||||
delegate: Kirigami.Card {
|
||||
@@ -45,6 +43,28 @@ Kirigami.ApplicationWindow {
|
||||
banner {
|
||||
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 {}
|
||||
}
|
||||
@@ -73,13 +93,14 @@ Kirigami.ApplicationWindow {
|
||||
name: "addvehicle"
|
||||
Component {
|
||||
Kirigami.Page {
|
||||
id: vehicleAddPage
|
||||
title: qsTr("Add Vehicle")
|
||||
Layouts.ColumnLayout {
|
||||
anchors.fill: parent
|
||||
Kirigami.FormLayout {
|
||||
Layouts.Layout.alignment: Qt.AlignHCenter
|
||||
Layouts.Layout.fillWidth: true
|
||||
width: page.width
|
||||
width: vehicleAddPage.width
|
||||
|
||||
Item {
|
||||
Kirigami.FormData.isSection: true
|
||||
@@ -87,21 +108,25 @@ Kirigami.ApplicationWindow {
|
||||
}
|
||||
|
||||
Controls.TextField {
|
||||
id: nameField
|
||||
selectByMouse: true
|
||||
Kirigami.FormData.label: qsTr("Name")+":"
|
||||
}
|
||||
|
||||
Controls.TextField {
|
||||
id: makeField
|
||||
selectByMouse: true
|
||||
Kirigami.FormData.label: qsTr("Make")+":"
|
||||
}
|
||||
|
||||
Controls.TextField {
|
||||
id: modelField
|
||||
selectByMouse: true
|
||||
Kirigami.FormData.label: qsTr("Model")+":"
|
||||
}
|
||||
|
||||
Controls.TextField {
|
||||
id: yearField
|
||||
selectByMouse: true
|
||||
Kirigami.FormData.label: qsTr("Year")+":"
|
||||
inputMethodHints: Qt.ImhDigitsOnly
|
||||
@@ -112,6 +137,7 @@ Kirigami.ApplicationWindow {
|
||||
}
|
||||
|
||||
Controls.TextField {
|
||||
id: vinField
|
||||
selectByMouse: true
|
||||
Kirigami.FormData.label: qsTr("VIN")+":"
|
||||
}
|
||||
@@ -120,6 +146,7 @@ Kirigami.ApplicationWindow {
|
||||
text: qsTr("Add")
|
||||
highlighted: true
|
||||
onClicked: {
|
||||
vehicleModel.addNewVehicle(nameField.text, makeField.text, modelField.text, parseInt(yearField.text), "", vinField.text);
|
||||
router.popRoute();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
if(role < Qt::UserRole)
|
||||
|
||||
@@ -29,7 +29,7 @@ class SqlServiceRecord : public QSqlTableModel
|
||||
public:
|
||||
explicit SqlServiceRecord(QObject *parent = nullptr, QSqlDatabase db = QSqlDatabase());
|
||||
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;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -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);
|
||||
if(role < Qt::UserRole)
|
||||
@@ -51,6 +51,28 @@ QVariant SqlVehicle::data(const QModelIndex& index, int role) const
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -29,7 +29,8 @@ class SqlVehicle : public QSqlTableModel
|
||||
public:
|
||||
explicit SqlVehicle(QObject *parent = nullptr, QSqlDatabase db = QSqlDatabase());
|
||||
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:
|
||||
|
||||
|
||||
@@ -58,7 +58,7 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
|
||||
QFile(db.databaseName()).open(QIODevice::ReadWrite);
|
||||
//Create table structure
|
||||
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.commit();
|
||||
db.close();
|
||||
@@ -72,6 +72,8 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
|
||||
|
||||
SqlVehicle vehicles(nullptr, db);
|
||||
SqlServiceRecord records(nullptr, db);
|
||||
vehicles.select();
|
||||
records.select();
|
||||
|
||||
QQmlApplicationEngine engine;
|
||||
engine.rootContext()->setContextProperty(QStringLiteral("appAboutData"), QVariant::fromValue(aboutData));
|
||||
|
||||
Reference in New Issue
Block a user