RentBook

UI Comparison to old QWidget version

The UI of this new version of the application is implemented with Qt Quick Components. The old version was written with native Symbian C++ using QWidget classes.

See the difference of these two UI from separate page

RentBook old and the new UI:

Class Diagram

RentBook QML application uses classes from Qt side for database management and for telephony calls. Database model classes RentItem, Renter and Rent transports data from Qt database to QML UI.

Database tables

Using Database from Qt

Adds SQLite database to the list of database connections. Try to open existing database file or create the new one.

bool DatabaseManager::openDB()
{
    db = QSqlDatabase::addDatabase("QSQLITE");

    QString path(QDir::home().path());
    path.append(QDir::separator()).append("rentbook2.0.db.sqlite");
    path = QDir::toNativeSeparators(path);
    db.setDatabaseName(path);

    return db.open();
}

Creates renter -table. The primary key column identifies the table row from others.

bool DatabaseManager::createRenterTable()
{
    bool ret = false;
    if (db.isOpen()) {
        QSqlQuery query;
        ret = query.exec("CREATE TABLE renter "
                         "(id integer primary key, "
                         "name varchar(50), "
                         "phone varchar(20), "
                         "popular integer)");
    }
    return ret;
}

Insert the new renter.

int DatabaseManager::insertRenter(const QVariant& name, const QVariant& phone)
{
    int renterId = -1;
    if (db.isOpen()) {
        QSqlQuery query;
        bool ret = query.prepare("INSERT INTO renter (id, name, phone, popular) "
                                 "VALUES (:id, :name, :phone, :popular)");
        if (ret) {
            renterId = nextId();
            query.bindValue(":id", renterId);
            query.bindValue(":name", name);
            query.bindValue(":phone", phone);
            query.bindValue(":popular", false);
            query.exec();
        }
    }
    return renterId;
}

Update the renter data.

void DatabaseManager::updateRenter(const int renterId, const QVariant& name, const QVariant& phone)
{
    QSqlQuery query;
    bool ret = query.prepare("UPDATE renter SET name = :name, phone = :phone where id = :id");
    if (ret) {
        query.bindValue(":name", name);
        query.bindValue(":phone", phone);
        query.bindValue(":id", renterId);
        ret = query.exec();
    }
}

Calling database from QML

The database class is stored as QDeclarativeView context property. Use"db" string in QML code to get handle to the database.

QDeclarativeView view;
DatabaseManager* db = new DatabaseManager();
view.rootContext()->setContextProperty("db", db);

Calling the database from QML.

db.insertRenter("Renter name", "phone");

InsertRenter -method is defined as Q_INVOKABLE in DatabaseManager.h so that can be called from QML.

Q_INVOKABLE int insertRenter(const QVariant& name, const QVariant& phone);

List of rents from Qt to QML

Use QList QObject array for getting rents list from Qt to QML.

class DatabaseManager: public QObject
{
    Q_OBJECT

public:
    Q_INVOKABLE QList<QObject*> rents();
};

Rent object have to be derived from QObject and have needed Q_PROPERTY properties for getting data.

class Rent : public QObject
{
    Q_OBJECT
    Q_PROPERTY(int index READ index WRITE setIndex)
    Q_PROPERTY(int itemId READ itemId WRITE setItemId)
    Q_PROPERTY(int renterId READ renterId WRITE setRenterId)
    Q_PROPERTY(int date READ date WRITE setDate)
    Q_PROPERTY(int rentBlockIndex READ rentBlockIndex WRITE setRentBlockIndex)

public:
    Rent(QObject *parent = 0);
    ~Rent();

    int index() const;
    void setIndex(const int);

    int rentBlockIndex() const;
    void setRentBlockIndex(const int);

    int itemId() const;
    void setItemId(const int);

    int renterId() const;
    void setRenterId(const int);

    int date() const;
    void setDate(const int);

private:
    int m_id;
    int m_itemId;
    int m_renterId;
    int m_date;
    int m_rentBlockIndex;
};

Calling rents -method from QML that returns list of data.

var rents = db.rents();

for (var i = 0; i < rents.length; i++) {
    var item = rents[i];
    console.log("index=" + item.index);
}

All downloads

Attachments

Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2011 All rights reserved