Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

QSqlQueryModel Class

The QSqlQueryModel class provides a read-only data model for SQL result sets. More...

 #include <QSqlQueryModel>

Inherits: QAbstractTableModel.

Inherited by: QSqlTableModel.

Public Functions

QAbstractItemModel(QObject * parent = 0)
QSqlQueryModel(QObject * parent = 0)
virtual ~QAbstractItemModel()
virtual ~QSqlQueryModel()
virtual QModelIndex buddy(const QModelIndex & index) const
virtual bool canDropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent) const
virtual bool canFetchMore(const QModelIndex & parent) const
virtual void clear()
virtual int columnCount(const QModelIndex & parent = QModelIndex()) const = 0
virtual QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const = 0
virtual bool dropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent)
virtual void fetchMore(const QModelIndex & parent)
virtual Qt::ItemFlags flags(const QModelIndex & index) const
virtual bool hasChildren(const QModelIndex & parent = QModelIndex()) const
bool hasIndex(int row, int column, const QModelIndex & parent = QModelIndex()) const
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const
virtual QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const = 0
bool insertColumn(int column, const QModelIndex & parent = QModelIndex())
virtual bool insertColumns(int column, int count, const QModelIndex & parent = QModelIndex())
bool insertRow(int row, const QModelIndex & parent = QModelIndex())
virtual bool insertRows(int row, int count, const QModelIndex & parent = QModelIndex())
virtual QMap<int, QVariant> itemData(const QModelIndex & index) const
QSqlError lastError() const
virtual QModelIndexList match(const QModelIndex & start, int role, const QVariant & value, int hits = 1, Qt::MatchFlags flags = Qt::MatchFlags( Qt::MatchStartsWith | Qt::MatchWrap )) const
virtual QMimeData * mimeData(const QModelIndexList & indexes) const
virtual QStringList mimeTypes() const
bool moveColumn(const QModelIndex & sourceParent, int sourceColumn, const QModelIndex & destinationParent, int destinationChild)
virtual bool moveColumns(const QModelIndex & sourceParent, int sourceColumn, int count, const QModelIndex & destinationParent, int destinationChild)
bool moveRow(const QModelIndex & sourceParent, int sourceRow, const QModelIndex & destinationParent, int destinationChild)
virtual bool moveRows(const QModelIndex & sourceParent, int sourceRow, int count, const QModelIndex & destinationParent, int destinationChild)
virtual QModelIndex parent(const QModelIndex & index) const = 0
QSqlQuery query() const
QSqlRecord record(int row) const
QSqlRecord record() const
bool removeColumn(int column, const QModelIndex & parent = QModelIndex())
virtual bool removeColumns(int column, int count, const QModelIndex & parent = QModelIndex())
bool removeRow(int row, const QModelIndex & parent = QModelIndex())
virtual bool removeRows(int row, int count, const QModelIndex & parent = QModelIndex())
virtual QHash<int, QByteArray> roleNames() const
virtual int rowCount(const QModelIndex & parent = QModelIndex()) const = 0
virtual bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole)
virtual bool setHeaderData(int section, Qt::Orientation orientation, const QVariant & value, int role = Qt::EditRole)
virtual bool setItemData(const QModelIndex & index, const QMap<int, QVariant> & roles)
void setQuery(const QSqlQuery & query)
void setQuery(const QString & query, const QSqlDatabase & db = QSqlDatabase())
QModelIndex sibling(int row, int column, const QModelIndex & index) const
virtual void sort(int column, Qt::SortOrder order = Qt::AscendingOrder)
virtual QSize span(const QModelIndex & index) const
virtual Qt::DropActions supportedDragActions() const
virtual Qt::DropActions supportedDropActions() const

Reimplemented Public Functions

virtual bool canFetchMore(const QModelIndex & parent = QModelIndex()) const
virtual int columnCount(const QModelIndex & index = QModelIndex()) const
virtual QVariant data(const QModelIndex & item, int role = Qt::DisplayRole) const
virtual void fetchMore(const QModelIndex & parent = QModelIndex())
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const
virtual bool insertColumns(int column, int count, const QModelIndex & parent = QModelIndex())
virtual bool removeColumns(int column, int count, const QModelIndex & parent = QModelIndex())
virtual int rowCount(const QModelIndex & parent = QModelIndex()) const
virtual bool setHeaderData(int section, Qt::Orientation orientation, const QVariant & value, int role = Qt::EditRole)

Public Slots

virtual void revert()
virtual bool submit()
  • 1 public slot inherited from QObject

Signals

void columnsAboutToBeInserted(const QModelIndex & parent, int start, int end)
void columnsAboutToBeMoved(const QModelIndex & sourceParent, int sourceStart, int sourceEnd, const QModelIndex & destinationParent, int destinationColumn)
void columnsAboutToBeRemoved(const QModelIndex & parent, int start, int end)
void columnsInserted(const QModelIndex & parent, int start, int end)
void columnsMoved(const QModelIndex & sourceParent, int sourceStart, int sourceEnd, const QModelIndex & destinationParent, int destinationColumn)
void columnsRemoved(const QModelIndex & parent, int start, int end)
void dataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight, const QSet<int> & roles = QSet<int> ())
void headerDataChanged(Qt::Orientation orientation, int first, int last)
void layoutAboutToBeChanged(const QList<QPersistentModelIndex> & parents = QList<QPersistentModelIndex> ())
void layoutChanged(const QList<QPersistentModelIndex> & parents = QList<QPersistentModelIndex> ())
void modelAboutToBeReset()
void modelReset()
void rowsAboutToBeInserted(const QModelIndex & parent, int start, int end)
void rowsAboutToBeMoved(const QModelIndex & sourceParent, int sourceStart, int sourceEnd, const QModelIndex & destinationParent, int destinationRow)
void rowsAboutToBeRemoved(const QModelIndex & parent, int start, int end)
void rowsInserted(const QModelIndex & parent, int start, int end)
void rowsMoved(const QModelIndex & sourceParent, int sourceStart, int sourceEnd, const QModelIndex & destinationParent, int destinationRow)
void rowsRemoved(const QModelIndex & parent, int start, int end)

Protected Functions

void beginInsertColumns(const QModelIndex & parent, int first, int last)
void beginInsertRows(const QModelIndex & parent, int first, int last)
bool beginMoveColumns(const QModelIndex & sourceParent, int sourceFirst, int sourceLast, const QModelIndex & destinationParent, int destinationChild)
bool beginMoveRows(const QModelIndex & sourceParent, int sourceFirst, int sourceLast, const QModelIndex & destinationParent, int destinationChild)
void beginRemoveColumns(const QModelIndex & parent, int first, int last)
void beginRemoveRows(const QModelIndex & parent, int first, int last)
void beginResetModel()
void changePersistentIndex(const QModelIndex & from, const QModelIndex & to)
void changePersistentIndexList(const QModelIndexList & from, const QModelIndexList & to)
QModelIndex createIndex(int row, int column, void * ptr = 0) const
QModelIndex createIndex(int row, int column, quint32 id) const
void endInsertColumns()
void endInsertRows()
void endMoveColumns()
void endMoveRows()
void endRemoveColumns()
void endRemoveRows()
void endResetModel()
virtual QModelIndex indexInQuery(const QModelIndex & item) const
QModelIndexList persistentIndexList() const
virtual void queryChange()
void setLastError(const QSqlError & error)
  • 9 protected functions inherited from QObject

Additional Inherited Members

  • 1 property inherited from QObject
  • 11 static public members inherited from QObject

Detailed Description

The QSqlQueryModel class provides a read-only data model for SQL result sets.

QSqlQueryModel is a high-level interface for executing SQL statements and traversing the result set. It is built on top of the lower-level QSqlQuery and can be used to provide data to view classes such as QTableView. For example:

     QSqlQueryModel *model = new QSqlQueryModel;
     model->setQuery("SELECT name, salary FROM employee");
     model->setHeaderData(0, Qt::Horizontal, tr("Name"));
     model->setHeaderData(1, Qt::Horizontal, tr("Salary"));

     QTableView *view = new QTableView;
     view->setModel(model);
     view->show();

We set the model's query, then we set up the labels displayed in the view header.

QSqlQueryModel can also be used to access a database programmatically, without binding it to a view:

     QSqlQueryModel model;
     model.setQuery("SELECT * FROM employee");
     int salary = model.record(4).value("salary").toInt();

The code snippet above extracts the salary field from record 4 in the result set of the query SELECT * from employee. Assuming that salary is column 2, we can rewrite the last line as follows:

     int salary = model.data(model.index(4, 2)).toInt();

The model is read-only by default. To make it read-write, you must subclass it and reimplement setData() and flags(). Another option is to use QSqlTableModel, which provides a read-write model based on a single database table.

The sql/querymodel example illustrates how to use QSqlQueryModel to display the result of a query. It also shows how to subclass QSqlQueryModel to customize the contents of the data before showing it to the user, and how to create a read-write model based on QSqlQueryModel.

If the database doesn't return the amount of selected rows in a query, the model will fetch rows incrementally. See fetchMore() for more information.

See also QSqlTableModel, QSqlRelationalTableModel, QSqlQuery, Model/View Programming, and Query Model Example.

Member Function Documentation

QSqlQueryModel::QSqlQueryModel(QObject * parent = 0)

Creates an empty QSqlQueryModel with the given parent.

QSqlQueryModel::~QSqlQueryModel() [virtual]

Destroys the object and frees any allocated resources.

See also clear().

void QSqlQueryModel::beginResetModel() [protected]

bool QSqlQueryModel::canFetchMore(const QModelIndex & parent = QModelIndex()) const [virtual]

Reimplemented from QAbstractItemModel::canFetchMore().

Returns true if it is possible to read more rows from the database. This only affects databases that don't report back the size of a query (see QSqlDriver::hasFeature()).

parent should always be an invalid QModelIndex.

This function was introduced in Qt 4.1.

See also fetchMore().

void QSqlQueryModel::clear() [virtual]

Clears the model and releases any acquired resource.

int QSqlQueryModel::columnCount(const QModelIndex & index = QModelIndex()) const [virtual]

Reimplemented from QAbstractItemModel::columnCount().

QVariant QSqlQueryModel::data(const QModelIndex & item, int role = Qt::DisplayRole) const [virtual]

Reimplemented from QAbstractItemModel::data().

Returns the value for the specified item and role.

If item is out of bounds or if an error occurred, an invalid QVariant is returned.

See also lastError().

void QSqlQueryModel::endResetModel() [protected]

void QSqlQueryModel::fetchMore(const QModelIndex & parent = QModelIndex()) [virtual]

Reimplemented from QAbstractItemModel::fetchMore().

Fetches more rows from a database. This only affects databases that don't report back the size of a query (see QSqlDriver::hasFeature()).

To force fetching of the entire result set, you can use the following:

 while (myModel->canFetchMore())
     myModel->fetchMore();

parent should always be an invalid QModelIndex.

This function was introduced in Qt 4.1.

See also canFetchMore().

QVariant QSqlQueryModel::headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const [virtual]

Reimplemented from QAbstractItemModel::headerData().

Returns the header data for the given role in the section of the header with the specified orientation.

See also setHeaderData().

QModelIndex QSqlQueryModel::indexInQuery(const QModelIndex & item) const [virtual protected]

Returns the index of the value in the database result set for the given item in the model.

The return value is identical to item if no columns or rows have been inserted, removed, or moved around.

Returns an invalid model index if item is out of bounds or if item does not point to a value in the result set.

See also QSqlTableModel::indexInQuery(), insertColumns(), and removeColumns().

bool QSqlQueryModel::insertColumns(int column, int count, const QModelIndex & parent = QModelIndex()) [virtual]

Reimplemented from QAbstractItemModel::insertColumns().

Inserts count columns into the model at position column. The parent parameter must always be an invalid QModelIndex, since the model does not support parent-child relationships.

Returns true if column is within bounds; otherwise returns false.

By default, inserted columns are empty. To fill them with data, reimplement data() and handle any inserted column separately:

 QVariant MyModel::data(const QModelIndex &item, int role) const
 {
     if (item.column() == m_specialColumnNo) {
         // handle column separately
     }
     return QSqlQueryModel::data(item, role);
 }

See also removeColumns().

QSqlError QSqlQueryModel::lastError() const

Returns information about the last error that occurred on the database.

See also setLastError() and query().

QSqlQuery QSqlQueryModel::query() const

Returns the QSqlQuery associated with this model.

See also setQuery().

void QSqlQueryModel::queryChange() [virtual protected]

This virtual function is called whenever the query changes. The default implementation does nothing.

query() returns the new query.

See also query() and setQuery().

QSqlRecord QSqlQueryModel::record(int row) const

Returns the record containing information about the fields of the current query. If row is the index of a valid row, the record will be populated with values from that row.

If the model is not initialized, an empty record will be returned.

See also QSqlRecord::isEmpty().

QSqlRecord QSqlQueryModel::record() const

This is an overloaded function.

Returns an empty record containing information about the fields of the current query.

If the model is not initialized, an empty record will be returned.

See also QSqlRecord::isEmpty().

bool QSqlQueryModel::removeColumns(int column, int count, const QModelIndex & parent = QModelIndex()) [virtual]

Reimplemented from QAbstractItemModel::removeColumns().

Removes count columns from the model starting from position column. The parent parameter must always be an invalid QModelIndex, since the model does not support parent-child relationships.

Removing columns effectively hides them. It does not affect the underlying QSqlQuery.

Returns true if the columns were removed; otherwise returns false.

int QSqlQueryModel::rowCount(const QModelIndex & parent = QModelIndex()) const [virtual]

Reimplemented from QAbstractItemModel::rowCount().

If the database supports returning the size of a query (see QSqlDriver::hasFeature()), the amount of rows of the current query is returned. Otherwise, returns the amount of rows currently cached on the client.

parent should always be an invalid QModelIndex.

This function was introduced in Qt 4.1.

See also canFetchMore() and QSqlDriver::hasFeature().

bool QSqlQueryModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant & value, int role = Qt::EditRole) [virtual]

Reimplemented from QAbstractItemModel::setHeaderData().

Sets the caption for a horizontal header for the specified role to value. This is useful if the model is used to display data in a view (e.g., QTableView).

Returns true if orientation is Qt::Horizontal and the section refers to a valid section; otherwise returns false.

Note that this function cannot be used to modify values in the database since the model is read-only.

See also headerData() and data().

void QSqlQueryModel::setLastError(const QSqlError & error) [protected]

Protected function which allows derived classes to set the value of the last error that occurred on the database to error.

See also lastError().

void QSqlQueryModel::setQuery(const QSqlQuery & query)

Resets the model and sets the data provider to be the given query. Note that the query must be active and must not be isForwardOnly().

lastError() can be used to retrieve verbose information if there was an error setting the query.

Note: Calling setQuery() will remove any inserted columns.

See also query(), QSqlQuery::isActive(), QSqlQuery::setForwardOnly(), and lastError().

void QSqlQueryModel::setQuery(const QString & query, const QSqlDatabase & db = QSqlDatabase())

This is an overloaded function.

Executes the query query for the given database connection db. If no database (or an invalid database) is specified, the default connection is used.

lastError() can be used to retrieve verbose information if there was an error setting the query.

Example:

 QSqlQueryModel model;
 model.setQuery("select * from MyTable");
 if (model.lastError().isValid())
     qDebug() << model.lastError();

See also query(), queryChange(), and lastError().

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 5.0-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web