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  · 

QStandardItemModel Class Reference
[QtGui module]

The QStandardItemModel class provides a generic model for storing custom data. More...

#include <QStandardItemModel>

Inherits QAbstractItemModel.

Public Functions

  • QStandardItemModel ( QObject * parent = 0 )
  • QStandardItemModel ( int rows, int columns, QObject * parent = 0 )
  • void clear ()
  • virtual int columnCount ( const QModelIndex & parent = QModelIndex() ) const
  • virtual QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const
  • virtual Qt::ItemFlags flags ( const QModelIndex & index ) const
  • virtual bool hasChildren ( const QModelIndex & parent = QModelIndex() ) const
  • virtual QModelIndex index ( int row, int column, const QModelIndex & parent = QModelIndex() ) const
  • virtual bool insertColumns ( int column, int count, const QModelIndex & parent = QModelIndex() )
  • virtual bool insertRows ( int row, int count, const QModelIndex & parent = QModelIndex() )
  • virtual QModelIndex parent ( const QModelIndex & child ) const
  • virtual bool removeColumns ( int column, int count, const QModelIndex & parent = QModelIndex() )
  • virtual bool removeRows ( int row, int count, const QModelIndex & parent = QModelIndex() )
  • virtual int rowCount ( const QModelIndex & parent = QModelIndex() ) const
  • virtual bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole )

Additional Inherited Members


Detailed Description

The QStandardItemModel class provides a generic model for storing custom data.

QStandardItemModel can be used as a repository for standard Qt data types. It is one of the Model/View Classes and is part of Qt's 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:

        QStandardItemModel model(4, 2);
        for (int row = 0; row < 4; ++row) {
            for (int column = 0; column < 2; ++column) {
                QModelIndex index = model.index(row, column, QModelIndex());
                model.setData(index, QVariant((row+1) * (column+1)));
            }
        }

An example usage of QStandardItemModel to create a tree:

        QStandardItemModel *model = new QStandardItemModel();
        QModelIndex parent;
        for (int i = 0; i < 4; ++i) {
            parent = model->index(0, 0, parent);
            model->insertRows(0, 1, parent);
            model->insertColumns(0, 1, parent);
            QModelIndex index = model->index(0, 0, parent);
            model->setData(index, i);
        }

The current implementation of QStandardItemModel only supports adding children to the first column.

See also Model/View Programming, QAbstractItemModel, and Simple Tree Model example.


Member Function Documentation

QStandardItemModel::QStandardItemModel ( QObject * parent = 0 )

Creates an empty model with the given parent, containing no rows or columns.

See also insertColumns() and insertRows().

QStandardItemModel::QStandardItemModel ( int rows, int columns, QObject * parent = 0 )

Creates a model with the given parent, containing the given number of rows and columns.

See also columnCount() and rowCount().

QStandardItemModel::~QStandardItemModel ()

Destroys this model.

void QStandardItemModel::clear ()

Removes all items from the model and resets both the horizontal and vertical headers.

See also removeColumns() and removeRows().

int QStandardItemModel::columnCount ( const QModelIndex & parent = QModelIndex() ) const   [virtual]

Returns the number of columns in the model that contain items with the given parent.

Reimplemented from QAbstractItemModel.

See also rowCount() and insertColumns().

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

Returns the data for the given index and display role.

Reimplemented from QAbstractItemModel.

See also setData(), setHeaderData(), and index().

Qt::ItemFlags QStandardItemModel::flags ( const QModelIndex & index ) const   [virtual]

Returns the item flags for the given index.

This model returns returns a combination of flags that enables the item (Qt::ItemIsEnabled), allows it to be selected (Qt::ItemIsSelectable) and edited (Qt::ItemIsEditable).

Reimplemented from QAbstractItemModel.

See also Qt::ItemFlags.

bool QStandardItemModel::hasChildren ( const QModelIndex & parent = QModelIndex() ) const   [virtual]

Returns true if the given parent model index has child items; otherwise returns false.

Use the insertColumns() and insertRows() functions to add children.

Reimplemented from QAbstractItemModel.

See also rowCount(), columnCount(), and parent().

QModelIndex QStandardItemModel::index ( int row, int column, const QModelIndex & parent = QModelIndex() ) const   [virtual]

Returns the index of the item in the model specified by the given row, column, and parent index.

Reimplemented from QAbstractItemModel.

See also data().

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

Inserts count columns into the model, creating new items as children of the given parent. The new columns are inserted before the column specified. Returns true if the columns were successfully inserted; otherwise returns false.

If column is 0, the columns are prepended to any existing columns in the parent. If column is columnCount(), the columns are appended to any existing columns in the parent. If the given parent has no children, a single row with count columns is inserted.

Reimplemented from QAbstractItemModel.

See also insertColumn(), insertRows(), removeColumns(), and columnCount().

bool QStandardItemModel::insertRows ( int row, int count, const QModelIndex & parent = QModelIndex() )   [virtual]

Inserts count rows into the model, creating new items as children of the given parent. The new rows are inserted before the row specified. Returns true if the rows were successfully inserted; otherwise returns false.

If row is 0, the rows are prepended to any existing rows in the parent. If row is rowCount(), the rows are appended to any existing rows in the parent. If the given parent has no children, a single column with count rows is inserted. Note that a row with no columns will not show up in the treeview.

Reimplemented from QAbstractItemModel.

See also insertRow(), insertColumns(), removeRows(), and rowCount().

QModelIndex QStandardItemModel::parent ( const QModelIndex & child ) const   [virtual]

Returns the model index for the parent of the given child item.

Reimplemented from QAbstractItemModel.

See also hasChildren().

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

Removes count columns from the model, starting with the specified column. The items removed are children of the item represented by the given parent model index. Returns true if the columns were successfully removed; otherwise returns false.

Reimplemented from QAbstractItemModel.

See also clear(), insertColumns(), and columnCount().

bool QStandardItemModel::removeRows ( int row, int count, const QModelIndex & parent = QModelIndex() )   [virtual]

Removes count rows from the model, starting with the specified row. The items removed are children of the item represented by the given parent model index. Returns true if the rows were successfully removed; otherwise returns false.

Reimplemented from QAbstractItemModel.

See also clear(), insertRows(), and rowCount().

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

Returns the number of rows in the model that contain items with the given parent.

Reimplemented from QAbstractItemModel.

See also columnCount() and insertRows().

bool QStandardItemModel::setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole )   [virtual]

Sets the data for the given index and role to the value specified. The role is described by the Qt::ItemDataRole enum. Returns false if the given index is not valid; otherwise returns true.

Reimplemented from QAbstractItemModel.

See also data(), itemData(), and setHeaderData().

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 94
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. Pourquoi les programmeurs sont-ils moins payés que les gestionnaires de programmes ? Manquent-ils de pouvoir de négociation ? 42
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. 2017 : un quinquennat pour une nouvelle version du C++ ? Possible, selon Herb Sutter 7
Page suivante

Le blog Digia au hasard

Logo

Créer des applications avec un style Metro avec Qt, exemples en QML et C++, un article de Digia Qt traduit par Thibaut Cuvelier

Le blog Digia est l'endroit privilégié pour la communication sur l'édition commerciale de Qt, où des réponses publiques sont apportées aux questions les plus posées au support. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

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 4.1
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