⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qstandarditemmodel.cpp

📁 qt-x11-opensource-src-4.1.4.tar.gz源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file.  Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qstandarditemmodel.h"#ifndef QT_NO_STANDARDITEMMODEL#include <qpair.h>#include <qvariant.h>#include <qvector.h>#include <private/qstandarditemmodel_p.h>#include <qdebug.h>/*!    \class QStandardItemModel    \brief The QStandardItemModel class provides a generic model for storing custom data.    \ingroup model-view    QStandardItemModel can be used as a repository for standard Qt    data types. It is one of the \l {Model/View Classes} and is part    of Qt's \l {Model/View Programming}{model/view} framework.    Data is written to the model, and read back, using the standard    QAbstractItemModel interface. The way each item of information is    referenced depends on how the data is inserted into the model. The    QStandardItemModel class reimplements several of the    QAbstractItemModel class's functions, and implements in addition a    clear() function which removes all items from the model and resets    both the horizontal and vertical headers.    For performance and flexibility, you may want to subclass    QAbstractItemModel to provide support for different kinds of    repositories. For example, the QDirModel provides a model    interface to the underlying file system, and does not actually    store file information internally.    An example usage of QStandardItemModel to create a table:    \quotefromfile itemviews/spinboxdelegate/main.cpp    \skipto QStandardItemModel model    \printline model    \skipto for (int row    \printuntil }    \printline }    An example usage of QStandardItemModel to create a tree:    \quotefromfile snippets/qstandarditemmodel/main.cpp    \skipto QStandardItemModel    \printuntil }    The current implementation of QStandardItemModel only supports adding    children to the first column.    \sa {Model/View Programming}, QAbstractItemModel,    {itemviews/simpletreemodel}{Simple Tree Model example}*//*!    Creates an empty model with the given \a parent, containing no    rows or columns.    \sa insertColumns(), insertRows()*/QStandardItemModel::QStandardItemModel(QObject *parent)    : QAbstractItemModel(*new QStandardItemModelPrivate(0, 0), parent){    // nothing}/*!    Creates a model with the given \a parent, containing the given    number of \a rows and \a columns.    \sa columnCount(), rowCount()*/QStandardItemModel::QStandardItemModel(int rows, int columns, QObject *parent)    : QAbstractItemModel(*new QStandardItemModelPrivate(rows, columns), parent){    // nothing}/*!    Destroys this model.*/QStandardItemModel::~QStandardItemModel(){}/*!    Returns the index of the item in the model specified by the given    \a row, \a column, and \a parent index.    \sa data()*/QModelIndex QStandardItemModel::index(int row, int column, const QModelIndex &parent) const{    Q_D(const QStandardItemModel);    if (hasIndex(row, column, parent)) {        if (parent.isValid()) {            QStdModelRow *parentRow = d->containedRow(parent, false);            return createIndex(row, column, parentRow);        } else {            return createIndex(row, column, 0);        }    }    return QModelIndex();}/*!    Returns the model index for the parent of the given \a child item.    \sa hasChildren()*/QModelIndex QStandardItemModel::parent(const QModelIndex &child) const{    Q_D(const QStandardItemModel);    if (child.isValid() && child.internalPointer()) {        QStdModelRow *parentRow = static_cast<QStdModelRow*>(child.internalPointer());        QStdModelRow *grandParentRow = parentRow ? parentRow->p : 0;        QVector<QStdModelRow*> grandParentChildren = d->topLevelRows;        if (grandParentRow)            grandParentChildren = grandParentRow->childrenRows;        // ### slow, use ptr trick        int row = grandParentChildren.indexOf(parentRow);        return createIndex(row, 0, grandParentRow);    }    return QModelIndex();}/*!    Returns the number of rows in the model that contain items with the given    \a parent.    \sa columnCount(), insertRows()*/int QStandardItemModel::rowCount(const QModelIndex &parent) const{    Q_D(const QStandardItemModel);    if (parent.column() > 0)        return 0;    QStdModelRow *modelRow = d->containedRow(parent, true);    if (modelRow)        return modelRow->childrenRows.count();    return d->topLevelRows.count();}/*!    Returns the number of columns in the model that contain items with the given    \a parent.    \sa rowCount(), insertColumns()*/int QStandardItemModel::columnCount(const QModelIndex &parent) const{    Q_D(const QStandardItemModel);    if (parent.column() > 0)        return 0;    QStdModelRow *modelRow = d->containedRow(parent, true);    if (modelRow)        return modelRow->childrenColumns;    return d->topLevelColumns;}/*!    Returns true if the given \a parent model index has child items;    otherwise returns false.    Use the insertColumns() and insertRows() functions to add    children.    \sa rowCount(), columnCount(), parent()*/bool QStandardItemModel::hasChildren(const QModelIndex &parent) const{    Q_D(const QStandardItemModel);    if (parent.column() > 0)        return false;    if (parent.isValid()) {        QStdModelRow *modelRow = d->containedRow(parent, true);        if (modelRow)            return modelRow->childrenRows.count() && modelRow->childrenColumns;    } else {        return !d->topLevelRows.isEmpty() && d->topLevelColumns;    }    return false;}/*!    Returns the data for the given \a index and display \a role.    \sa setData(), setHeaderData(), index()*/QVariant QStandardItemModel::data(const QModelIndex &index, int role) const{    Q_D(const QStandardItemModel);    // Don't need to check if index is valid because modelRow will    QStdModelRow *modelRow = d->containedRow(index, false);    if (modelRow && modelRow->items.count() > index.column()) {        QStdModelItem *item = modelRow->items.at(index.column());        if (item) {            role = (role == Qt::EditRole ? Qt::DisplayRole : role);            return item->value(role);        }    }    return QVariant();}/*!    Sets the data for the given \a index and \a role to the \a value    specified. The role is described by the Qt::ItemDataRole enum.    Returns false if the given \a index is not valid; otherwise    returns true.    \sa data(), itemData(), setHeaderData()*/bool QStandardItemModel::setData(const QModelIndex &index, const QVariant &value, int role){    Q_D(const QStandardItemModel);    if (!index.isValid())        return false;    QStdModelRow *modelRow = d->containedRow(index, true);    int count = modelRow->items.count();    // make room for enough items    if (count <= index.column())        modelRow->items.insert(count, index.column() + 1 - count, 0);    // make sure we have a QStdModelItem at the position    if (!modelRow->items.at(index.column()))        modelRow->items[index.column()] = new QStdModelItem;    role = (role == Qt::EditRole ? Qt::DisplayRole : role);    modelRow->items.at(index.column())->setValue(role, value);    emit dataChanged(index, index);    return true;}/*!  \reimp*/QVariant QStandardItemModel::headerData(int section, Qt::Orientation orientation, int role) const{    Q_D(const QStandardItemModel);    if (section < 0        || (orientation == Qt::Horizontal && section >= columnCount())        || (orientation == Qt::Vertical && section >= rowCount()))        return QVariant();    const QStdModelItem *headerItem = 0;    const QVector<QStdModelItem*> &header = (orientation == Qt::Horizontal                                             ? d->horizontalHeader : d->verticalHeader);    role = (role == Qt::EditRole ? Qt::DisplayRole : role);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -