?? qsqlquerymodel.cpp
字號:
*/void QSqlQueryModel::setQuery(const QSqlQuery &query){ Q_D(QSqlQueryModel); QSqlRecord newRec = query.record(); bool columnsChanged = (newRec != d->rec); bool hasQuerySize = d->query.driver()->hasFeature(QSqlDriver::QuerySize); if (d->colOffsets.size() != newRec.count() || columnsChanged) { d->colOffsets.resize(newRec.count()); memset(d->colOffsets.data(), 0, d->colOffsets.size() * sizeof(int)); } beginRemoveRows(QModelIndex(), 0, qMax(d->bottom.row(), 0)); d->bottom = QModelIndex(); d->error = QSqlError(); d->atEnd = false; d->query = query; d->rec = newRec; endRemoveRows(); if (columnsChanged) reset(); if (!query.isActive() || query.isForwardOnly()) { d->atEnd = true; d->bottom = QModelIndex(); if (query.isForwardOnly()) d->error = QSqlError(QLatin1String("Forward-only queries " "cannot be used in a data model"), QString(), QSqlError::ConnectionError); else d->error = query.lastError(); return; } QModelIndex newBottom; if (hasQuerySize) { newBottom = createIndex(d->query.size() - 1, d->rec.count() - 1); beginInsertRows(QModelIndex(), 0, qMax(0, newBottom.row())); d->bottom = createIndex(d->query.size() - 1, columnsChanged ? 0 : d->rec.count() - 1); d->atEnd = true; endInsertRows(); } else { newBottom = createIndex(-1, d->rec.count() - 1); } if (columnsChanged) { beginInsertColumns(QModelIndex(), 0, newBottom.column()); d->bottom = newBottom; endInsertColumns(); } else { d->bottom = newBottom; } queryChange(); // fetchMore does the rowsInserted stuff for incremental models fetchMore();}/*! \overload Executes the query \a query for the given database connection \a db. If no 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: \code QSqlQueryModel model; model.setQuery("select * from MyTable"); if (model.lastError().isValid()) qDebug() << model.lastError(); \endcode \sa query(), queryChange(), lastError()*/void QSqlQueryModel::setQuery(const QString &query, const QSqlDatabase &db){ setQuery(QSqlQuery(query, db));}/*! Clears the model and releases any acquired resource.*/void QSqlQueryModel::clear(){ Q_D(QSqlQueryModel); d->error = QSqlError(); d->atEnd = true; d->query.clear(); d->rec.clear(); d->colOffsets.clear(); d->bottom = QModelIndex(); d->headers.clear();}/*! Sets the caption for a horizontal header for the specified \a role to \a value. This is useful if the model is used to display data in a view (e.g., QTableView). Returns true if \a orientation is Qt::Horizontal and the \a 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. \sa data() */bool QSqlQueryModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role){ Q_D(QSqlQueryModel); if (orientation != Qt::Horizontal || section < 0) return false; if (d->headers.size() <= section) d->headers.resize(qMax(section + 1, 16)); d->headers[section][role] = value; emit headerDataChanged(orientation, section, section); return true;}/*! Returns the QSqlQuery associated with this model. \sa setQuery()*/QSqlQuery QSqlQueryModel::query() const{ Q_D(const QSqlQueryModel); return d->query;}/*! Returns information about the last error that occurred on the database.*/QSqlError QSqlQueryModel::lastError() const{ Q_D(const QSqlQueryModel); return d->error;}/*! Protected function which allows derived classes to set the value of the last error that occurred on the database to \a error. \sa lastError()*/void QSqlQueryModel::setLastError(const QSqlError &error){ Q_D(QSqlQueryModel); d->error = error;}/*! Returns the record containing information about the fields of the current query. If \a 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. \sa QSqlRecord::isEmpty()*/QSqlRecord QSqlQueryModel::record(int row) const{ Q_D(const QSqlQueryModel); if (row < 0) return d->rec; QSqlRecord rec = d->rec; for (int i = 0; i < rec.count(); ++i) rec.setValue(i, data(createIndex(row, i), Qt::EditRole)); return rec;}/*! \overload 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. \sa QSqlRecord::isEmpty() */QSqlRecord QSqlQueryModel::record() const{ Q_D(const QSqlQueryModel); return d->rec;}/*! Inserts \a count columns into the model at position \a column. The \a parent parameter must always be an invalid QModelIndex, since the model does not support parent-child relationships. Returns true if \a 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: \quotefromfile snippets/sqldatabase/sqldatabase.cpp \skipto QSqlQueryModel_snippets \skipto MyModel::data( \printuntil /^\}/ \sa removeColumns()*/bool QSqlQueryModel::insertColumns(int column, int count, const QModelIndex &parent){ Q_D(QSqlQueryModel); if (count <= 0 || parent.isValid() || column < 0 || column > d->rec.count()) return false; beginInsertColumns(parent, column, column + count - 1); for (int c = 0; c < count; ++c) { QSqlField field; field.setReadOnly(true); field.setGenerated(false); d->rec.insert(column, field); if (d->colOffsets.size() < d->rec.count()) { int nVal = d->colOffsets.isEmpty() ? 0 : d->colOffsets[d->colOffsets.size() - 1]; d->colOffsets.append(nVal); Q_ASSERT(d->colOffsets.size() >= d->rec.count()); } for (int i = column + 1; i < d->colOffsets.count(); ++i) ++d->colOffsets[i]; } endInsertColumns(); return true;}/*! Removes \a count columns from the model starting from position \a column. The \a 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. */bool QSqlQueryModel::removeColumns(int column, int count, const QModelIndex &parent){ Q_D(QSqlQueryModel); if (count <= 0 || parent.isValid() || column < 0 || column >= d->rec.count()) return false; beginRemoveColumns(parent, column, column + count - 1); int i; for (i = 0; i < count; ++i) d->rec.remove(column); for (i = column; i < d->colOffsets.count(); ++i) d->colOffsets[i] -= count; endRemoveColumns(); return true;}/*! Returns the index of the value in the database result set for the given \a item in the model. The return value is identical to \a item if no columns or rows have been inserted, removed, or moved around. Returns an invalid model index if \a item is out of bounds or if \a item does not point to a value in the result set. \sa QSqlTableModel::indexInQuery(), insertColumns(), removeColumns()*/QModelIndex QSqlQueryModel::indexInQuery(const QModelIndex &item) const{ Q_D(const QSqlQueryModel); if (item.column() < 0 || item.column() >= d->rec.count() || !d->rec.isGenerated(item.column())) return QModelIndex(); return createIndex(item.row(), item.column() - d->colOffsets[item.column()], item.internalPointer());}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -