qsqltablemodel.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtSql module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QSQLTABLEMODEL_H
00043 #define QSQLTABLEMODEL_H
00044 
00045 #include <QtSql/qsqldatabase.h>
00046 #include <QtSql/qsqlquerymodel.h>
00047 
00048 QT_BEGIN_HEADER
00049 
00050 QT_BEGIN_NAMESPACE
00051 
00052 QT_MODULE(Sql)
00053 
00054 class QSqlTableModelPrivate;
00055 class QSqlRecord;
00056 class QSqlField;
00057 class QSqlIndex;
00058 
00059 class Q_SQL_EXPORT QSqlTableModel: public QSqlQueryModel
00060 {
00061     Q_OBJECT
00062     Q_DECLARE_PRIVATE(QSqlTableModel)
00063 
00064 public:
00065     enum EditStrategy {OnFieldChange, OnRowChange, OnManualSubmit};
00066 
00067     explicit QSqlTableModel(QObject *parent = 0, QSqlDatabase db = QSqlDatabase());
00068     virtual ~QSqlTableModel();
00069 
00070     virtual bool select();
00071 
00072     virtual void setTable(const QString &tableName);
00073     QString tableName() const;
00074 
00075     Qt::ItemFlags flags(const QModelIndex &index) const;
00076 
00077     QVariant data(const QModelIndex &idx, int role = Qt::DisplayRole) const;
00078     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
00079 
00080     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
00081 
00082     bool isDirty(const QModelIndex &index) const;
00083     void clear();
00084 
00085     virtual void setEditStrategy(EditStrategy strategy);
00086     EditStrategy editStrategy() const;
00087 
00088     QSqlIndex primaryKey() const;
00089     QSqlDatabase database() const;
00090     int fieldIndex(const QString &fieldName) const;
00091 
00092     void sort(int column, Qt::SortOrder order);
00093     virtual void setSort(int column, Qt::SortOrder order);
00094 
00095     QString filter() const;
00096     virtual void setFilter(const QString &filter);
00097 
00098     int rowCount(const QModelIndex &parent = QModelIndex()) const;
00099 
00100     bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex());
00101     bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
00102     bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
00103 
00104     bool insertRecord(int row, const QSqlRecord &record);
00105     bool setRecord(int row, const QSqlRecord &record);
00106 
00107     virtual void revertRow(int row);
00108 
00109 public Q_SLOTS:
00110     bool submit();
00111     void revert();
00112 
00113     bool submitAll();
00114     void revertAll();
00115 
00116 Q_SIGNALS:
00117     void primeInsert(int row, QSqlRecord &record);
00118 
00119     void beforeInsert(QSqlRecord &record);
00120     void beforeUpdate(int row, QSqlRecord &record);
00121     void beforeDelete(int row);
00122 
00123 protected:
00124     QSqlTableModel(QSqlTableModelPrivate &dd, QObject *parent = 0, QSqlDatabase db = QSqlDatabase());
00125 
00126     virtual bool updateRowInTable(int row, const QSqlRecord &values);
00127     virtual bool insertRowIntoTable(const QSqlRecord &values);
00128     virtual bool deleteRowFromTable(int row);
00129     virtual QString orderByClause() const;
00130     virtual QString selectStatement() const;
00131 
00132     void setPrimaryKey(const QSqlIndex &key);
00133     void setQuery(const QSqlQuery &query);
00134     QModelIndex indexInQuery(const QModelIndex &item) const;
00135 };
00136 
00137 QT_END_NAMESPACE
00138 
00139 QT_END_HEADER
00140 
00141 #endif // QSQLTABLEMODEL_H