Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QSqlQueryModel Class

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

Header:#include <QSqlQueryModel>
Inherits:QAbstractTableModel
Inherited By:

QSqlTableModel

Public Functions

QSqlQueryModel(QObject * parent = 0)
virtual~QSqlQueryModel()
virtual voidclear()
QSqlErrorlastError() const
QSqlQueryquery() const
QSqlRecordrecord(int row) const
QSqlRecordrecord() const
voidsetQuery(const QSqlQuery & query)
voidsetQuery(const QString & query, const QSqlDatabase & db = QSqlDatabase())

Reimplemented Public Functions

virtual boolcanFetchMore(const QModelIndex & parent = QModelIndex()) const
virtual intcolumnCount(const QModelIndex & index = QModelIndex()) const
virtual QVariantdata(const QModelIndex & item, int role = Qt::DisplayRole) const
virtual voidfetchMore(const QModelIndex & parent = QModelIndex())
virtual QVariantheaderData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const
virtual boolinsertColumns(int column, int count, const QModelIndex & parent = QModelIndex())
virtual boolremoveColumns(int column, int count, const QModelIndex & parent = QModelIndex())
virtual introwCount(const QModelIndex & parent = QModelIndex()) const
virtual boolsetHeaderData(int section, Qt::Orientation orientation, const QVariant & value, int role = Qt::EditRole)

Protected Functions

QModelIndexindexInQuery(const QModelIndex & item) const
virtual voidqueryChange()
voidsetLastError(const QSqlError & error)

Additional Inherited Members

Detailed Description

TheQSqlQueryModel 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-levelQSqlQuery and can be used to provide data to view classes such asQTableView. For example:

QSqlQueryModel*model=newQSqlQueryModel;    model->setQuery("SELECT name, salary FROM employee");    model->setHeaderData(0,Qt::Horizontal, tr("Name"));    model->setHeaderData(1,Qt::Horizontal, tr("Salary"));QTableView*view=newQTableView;    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 thesalary field from record 4 in the result set of the querySELECT * from employee. Assuming thatsalary 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 reimplementsetData() andflags(). Another option is to useQSqlTableModel, which provides a read-write model based on a single database table.

Thesql/querymodel example illustrates how to useQSqlQueryModel to display the result of a query. It also shows how to subclassQSqlQueryModel to customize the contents of the data before showing it to the user, and how to create a read-write model based onQSqlQueryModel.

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

See alsoQSqlTableModel,QSqlRelationalTableModel,QSqlQuery,Model/View Programming, andQuery Model Example.

Member Function Documentation

QSqlQueryModel::QSqlQueryModel(QObject * parent = 0)

Creates an emptyQSqlQueryModel with the givenparent.

[virtual]QSqlQueryModel::~QSqlQueryModel()

Destroys the object and frees any allocated resources.

See alsoclear().

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

Reimplemented fromQAbstractItemModel::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 (seeQSqlDriver::hasFeature()).

parent should always be an invalidQModelIndex.

This function was introduced in Qt 4.1.

See alsofetchMore().

[virtual]void QSqlQueryModel::clear()

Clears the model and releases any acquired resource.

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

Reimplemented fromQAbstractItemModel::columnCount().

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

Reimplemented fromQAbstractItemModel::data().

Returns the value for the specifieditem androle.

Ifitem is out of bounds or if an error occurred, an invalidQVariant is returned.

See alsolastError().

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

Reimplemented fromQAbstractItemModel::fetchMore().

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

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

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

parent should always be an invalidQModelIndex.

This function was introduced in Qt 4.1.

See alsocanFetchMore().

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

Reimplemented fromQAbstractItemModel::headerData().

Returns the header data for the givenrole in thesection of the header with the specifiedorientation.

See alsosetHeaderData().

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

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

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

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

See alsoQSqlTableModel::indexInQuery(),insertColumns(), andremoveColumns().

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

Reimplemented fromQAbstractItemModel::insertColumns().

Insertscount columns into the model at positioncolumn. Theparent parameter must always be an invalidQModelIndex, since the model does not support parent-child relationships.

Returns true ifcolumn is within bounds; otherwise returns false.

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

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

See alsoremoveColumns().

QSqlError QSqlQueryModel::lastError() const

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

See alsosetLastError() andquery().

QSqlQuery QSqlQueryModel::query() const

Returns theQSqlQuery associated with this model.

See alsosetQuery().

[virtual protected]void QSqlQueryModel::queryChange()

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

query() returns the new query.

See alsoquery() andsetQuery().

QSqlRecord QSqlQueryModel::record(int row) const

Returns the record containing information about the fields of the current query. Ifrow 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 alsoQSqlRecord::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 alsoQSqlRecord::isEmpty().

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

Reimplemented fromQAbstractItemModel::removeColumns().

Removescount columns from the model starting from positioncolumn. Theparent parameter must always be an invalidQModelIndex, since the model does not support parent-child relationships.

Removing columns effectively hides them. It does not affect the underlyingQSqlQuery.

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

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

Reimplemented fromQAbstractItemModel::rowCount().

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

parent should always be an invalidQModelIndex.

This function was introduced in Qt 4.1.

See alsocanFetchMore() andQSqlDriver::hasFeature().

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

Reimplemented fromQAbstractItemModel::setHeaderData().

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

Returns true iforientation isQt::Horizontal and thesection 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 alsoheaderData() anddata().

[protected]void QSqlQueryModel::setLastError(constQSqlError & error)

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

See alsolastError().

void QSqlQueryModel::setQuery(constQSqlQuery & query)

Resets the model and sets the data provider to be the givenquery. 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 alsoquery(),QSqlQuery::isActive(),QSqlQuery::setForwardOnly(), andlastError().

void QSqlQueryModel::setQuery(constQString & query, constQSqlDatabase & db = QSqlDatabase())

This is an overloaded function.

Executes the queryquery for the given database connectiondb. 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 alsoquery(),queryChange(), andlastError().

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp