Signals | Public Member Functions

SqlTableModel Class Reference

Extension of QSqlTableModel class and used throughout the application. More...

#include <Database.h>

Inheritance diagram for SqlTableModel:
Inheritance graph
[legend]
Collaboration diagram for SqlTableModel:
Collaboration graph
[legend]

List of all members.

Signals

void queryChanged ()

Public Member Functions

 SqlTableModel (QObject *parent=0, QSqlDatabase db=QSqlDatabase())
virtual void queryChange ()
void setTable (const QString &table)

Detailed Description

Extension of QSqlTableModel class and used throughout the application.


Constructor & Destructor Documentation

SqlTableModel::SqlTableModel ( QObject parent = 0,
QSqlDatabase  db = QSqlDatabase() 
)

                                                                                              : QSqlRelationalTableModel(parent, db)
{
    // Required by QSqlHelper because it can not inherit QObject and connect in it's constructor.
    QObject::connect(Database::instance(), SIGNAL(tableChanged(QString)),
                     this, SLOT(onTableChange(QString)), Qt::QueuedConnection);
}


Member Function Documentation

void SqlTableModel::queryChange (  )  [virtual]

{
    // This is because our MappedBaseEditor needs a pk to work well
    // Best would be to take the "real" pk, but then: what is a real pk in views?

    // first try to find a reasonable pk:
    if (primaryKey().isEmpty() && !rowIdentifierField().isEmpty()) {
        QSqlIndex index = primaryKey();
        for(int i = 0; i < record().count(); i++)
            // So here we simply take the rowIdentifierField as pk...
            if (record().field(i).name() == rowIdentifierField())
                index.append(record().field(i));
        if (!index.isEmpty()) qDebug() << "PK set to rowIdentifierField: " << index << " for table " << tableName();
        setPrimaryKey(index);
    }

    // second try to find a reasonable pk:
    if (primaryKey().isEmpty()) {
        QSqlIndex index = primaryKey();
        for(int i = 0; i < record().count(); i++)
            // So here we simply take the _id and/or _nr columns as pk...
            if (record().field(i).name().contains(QRegExp("_id|_nr")))
                index.append(record().field(i));
        if (!index.isEmpty()) qDebug() << "PK set to " << index << " for table " << tableName();
        setPrimaryKey(index);
    }

    // fourth try to find a reasonable pk:
    if (primaryKey().isEmpty()) {
        QSqlIndex index = primaryKey();
        for(int i = 0; i < record().count(); i++)
            // So here we simply take all the columns as pk...
            index.append(record().field(i));
        if (!index.isEmpty()) qDebug() << "PK set to all fields for table " << tableName();
        setPrimaryKey(index);
    }

    // fourth try to find pk: just give warning
    if (primaryKey().isEmpty())
        qWarning() << "No primary key set for table " << tableName() << ". This might cause problems.";

    // This is needed for the mapper to know when the indexes
    // become invalidated
    qDebug() << "Emitting queryChanged for table " << tableName();
    emit queryChanged();
}

void SqlTableModel::queryChanged (  )  [signal]

Referenced by queryChange().

void SqlTableModel::setTable ( const QString table  ) 

The documentation for this class was generated from the following files: