RelationMode Example

#include <QApplication>
#include <QtSql>
#include <QMessageBox>

#include "window.h"

static bool createConnection()
{
    QSqlDatabase db = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"));
    db.setDatabaseName(QObject::tr(":memory:"));
    if (!db.open())
    {
        QMessageBox::critical(0, qApp->tr("Cannot open database"),
            qApp->tr("Unable to establish a database connection.\n"
                     "This example needs SQLite support. Please read "
                     "the Qt SQL driver documentation for information how "
                     "to build it.\n\n"
                     "Click Cancel to exit."), QMessageBox::Cancel);
        return false;
    }

    QSqlQuery query;
    query.exec(QObject::tr("create table employee(id integer primary key, name varchar(20), city int, country int)"));
    query.exec(QObject::tr("insert into employee values(1, 'Espen', 5000, 47)"));
    query.exec(QObject::tr("insert into employee values(2, 'Harald', 80000, 49)"));
    query.exec(QObject::tr("insert into employee values(3, 'Sam', 100, 1)"));

    query.exec(QObject::tr("create table city(id int, name varchar(20))"));
    query.exec(QObject::tr("insert into city values(100, 'San Jose')"));
    query.exec(QObject::tr("insert into city values(5000, 'Oslo')"));
    query.exec(QObject::tr("insert into city values(80000, 'Munich')"));

    query.exec(QObject::tr("create table country(id int, name varchar(20))"));
    query.exec(QObject::tr("insert into country values(1, 'USA')"));
    query.exec(QObject::tr("insert into country values(47, 'Norway')"));
    query.exec(QObject::tr("insert into country values(49, 'Germany')"));
    return true;
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    app.setOrganizationName(QStringLiteral("Developer Machines"));
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
    app.setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::Round);
#endif
    if (!createConnection())
        return 1;
    Window window;
    window.show();
    return app.exec();
}