Into

Modules

Documentation

classPiiTableModel

#include <PiiTableModel.h>

A hybrid of a table model and an item "delegate".

Description

This model can be used with Qt's view classes to create editable lists and tables. PiiTableModel is useful with data such as database query results where all items in a table column contain similar data. The same editor is used for each row, but columns may have different editors.

This class uses PiiModelItem to store item and header data. Each item in the model has two associated values with different data roles:

  • Qt::DisplayRole - the text displayed in the table cell when the editor is not active.

  • ColumnEditorValueRole - the actual value of the cell, which may be different from the text. For example, the value of an element in a combo box column is the index of the selected item, but the cell displays the text associated with the index. See EditorType for data types associated with editors.

Public types

enum
{ ColumnDefaultValueRole = Qt::UserRole + 1, ColumnEditorTypeRole, ColumnEditorPropertiesRole, ColumnEditorValueRole }

Custom data roles used by PiiTableModel to store information about column editors and their values.

enum
{ LineEditor, IntegerSpinBoxEditor, DoubleSpinBoxEditor, ComboBoxEditor }

Supported editor types.

enum
{ ChangeTextAutomatically, DoNotChangeText }

A flag for setValue() that can be used to enable/disable automatic setting of the displayed text of the element.

Public Slots

void
( )

Inserts a new row at the end of the table and fill with it default values.

void

Deletes selected rows.

void

Moves selected rows down step up.

void

Moves selected rows one step up.

Signals

void

Signals that the current item has changed on the attached view.

void
(
  • bool enabled
)

Emitted whenever the selection has changed.

void
(
  • bool enabled
)

Emitted whenever the selection has changed.

void
(
  • bool enabled
)

Emitted whenever the selection has changed.

void

Signals that selection has changed on the attached view.

Constructors and destructor

(
  • QAbstractItemView * parent
)

Creates a new PiiTableModel and binds it to the parent view.

Destroys the model.

Public member functions

bool
( )

Returns true if the selected rows can be deleted, false otherwise.

bool

Returns true if the last row can be deleted, false otherwise.

bool

Returns true if the selected rows can be moved down, false otherwise.

bool
( )

Returns true if the selected rows can be moved up, false otherwise.

void
( )

Clears all data, excluding column header definitions.

int
(
  • const QModelIndex & parent = QModelIndex()
)

Returns the number of columns in the model.

QVariantMap
(
  • int column
)

Returns the properties of the editor at column.

(
  • int column
)

Returns the editor used to edit items in the specified column.

(
  • int column
)

Returns all data associated with Qt::DisplayRole in the given column as a string list.

QVariantList
(
  • int column
)

Returns all data associated with ColumnEditorValueRole in the given column as a variant list.

(
  • const QModelIndex & index
  • int role = Qt::DisplayRole
)
(
  • int row
  • int column
  • int role = Qt::DisplayRole
)

Returns the data associated with role in the given cell.

(
  • int column
)

Returns the default value for cells in column.

Qt::ItemFlags
(
  • const QModelIndex & index
)
(
  • int section
  • Qt::Orientation orientation
  • int role
)
bool
(
  • int column
  • int count = 1
  • const QModelIndex & parent = QModelIndex()
)
void
( )

Inserts the given items as a new row to the model.

bool
(
  • int row
  • int count = 1
  • const QModelIndex & parent = QModelIndex()
)
(
  • int row
  • int column = 0
)

Returns the item in the given cell.

QMap< int, QVariant >
(
  • const QModelIndex & index
)

Returns data associated with any role at the given model index.

bool
(
  • int column
  • int count = 1
  • const QModelIndex & parent = QModelIndex()
)
bool
(
  • int row
  • int count = 1
  • const QModelIndex & parent = QModelIndex()
)
int
(
  • const QModelIndex & parent = QModelIndex()
)

Returns the number of rows in the model.

QList< int >

Returns the indices of all rows in which at least one cell is selected.

void
(
  • bool canDeleteLast
)

Enables/disables deletion of the last row in the model.

void
(
  • int column
  • const QVariantMap & properties
)

Configures the column editor in the specified column.

void
( )

Sets the editor used to edit items in the specified column.

void
( )

Returns all data associated with Qt::DisplayRole in the given column as a string list.

void
(
  • int column
  • const QVariantList & values
)

Sets all data associated with ColumnEditorValueRole in the given column as a variant list.

void
(
  • int row
  • int column
  • const QVariant & value
  • int role
)

Sets the data associated with role in the given cell.

bool
(
  • const QModelIndex & index
  • const QVariant & value
  • int role
)
void
( )

Sets the default value for cells in column.

bool
(
  • int section
  • Qt::Orientation
  • const QVariant & value
  • int role
)
void
( )

Sets the column header titles with one function call.

void
(
  • int row
  • int column
  • const QString & text
)

Sets the displayed text in the given cell.

void
( )

Sets the value of the element at the given coordinates.

(
  • int row
)

Removes row and returns its items as a list.

(
  • int row
  • int column
)

Returns the displayed text in the given cell.

virtual QString
( )

Converts value into user-displayable text.

(
  • int row
  • int column
)

Returns the data associated with ColumnEditorValueRole in the given cell.

Protected member functions

virtual QWidget *
(
  • QWidget * parent
  • int row
  • int column
)

Creates an editor for editing the item at the given coordinates.

virtual PiiModelItem *
(
  • int row
  • int column
)

Creates a new item at the given coordinates.

virtual void
(
  • QWidget * editor
  • int row
  • int column
)

Sets up the contents of editor based on the contents of the cell at the given position.

virtual void
(
  • QWidget * editor
  • int row
  • int column
)

Modifies the cell at the given position based on the contents of editor.

Enumeration details

  • enum ColumnEditorDataRole

    Custom data roles used by PiiTableModel to store information about column editors and their values.

    • ColumnDefaultValueRole - the default value for a cell in a column. Any variant. Used only in header items.

    • ColumnEditorTypeRole - the type of the column editor. An integer. See EditorType for valid values. Used only in header items.

    • ColumnEditorPropertiesRole - a QVariantMap that can be used to customize the column editor's properties. Used only in header items.

    • ColumnEditorValueRole - the current value of the item, in the editor's internal representation. Any variant type. Used only in table data items.

  • enum EditorType

    Supported editor types.

    • LineEditor - a QLineEdit. Suitable for text input with or without validation. The value and text are the same. To configure a validator for the editor, use the validator property (QVariantMap).

    • IntegerSpinBoxEditor - a QSpinBox. Suitable for integer input. The value is an integer. If the spin box has the prefix a and suffix properties set, the text will be different from the value.

    • DoubleSpinBoxEditor - a QDoubleSpinBox. Suitable for double-precision value input. The value is a double. If the spin box has the prefix a and suffix properties set, the text will be different from the value.

    • ComboBoxEditor - a QComboBox. Suitable for multiple choices type input. The value is an integer that stores the index of the selected choice. The text is the corresponding element in the combo box. To set the selectable items, use the items property (StringList).

  • enum ValueChangeBehavior

    A flag for setValue() that can be used to enable/disable automatic setting of the displayed text of the element.

Function details

  • PiiTableModel

    (
    • QAbstractItemView * parent
    )

    Creates a new PiiTableModel and binds it to the parent view.

    Since PiiTableModel is not a pure model, but works also as the "delegate", it cannot be used with many views simultaneously.

  • ~PiiTableModel

    ()

    Destroys the model.

  • bool canDelete

    ()

    Returns true if the selected rows can be deleted, false otherwise.

    Deleting rows down is possible if a) at least one row is selected and b) either the canDeleteLast() flag is true or there are more than one row left.

  • bool canDeleteLast

    ()

    Returns true if the last row can be deleted, false otherwise.

  • bool canMoveDown

    ()

    Returns true if the selected rows can be moved down, false otherwise.

    Moving rows down is possible if a) at least one row is selected and b) the last row is not selected.

  • bool canMoveUp

    ()

    Returns true if the selected rows can be moved up, false otherwise.

    Moving rows up is possible if a) at least one row is selected and b) the first row is not selected.

  • void clear

    ()

    Clears all data, excluding column header definitions.

  • int columnCount

    (
    • const QModelIndex & parent = QModelIndex()
    )

    Returns the number of columns in the model.

  • QVariantMap columnEditorProperties

    (
    • int column
    )
    [inline]

    Returns the properties of the editor at column.

    This function returns the properties set with setColumnEditorProperties() only. No other properties will be returned.

  • EditorType columnEditorType

    (
    • int column
    )
    [inline]

    Returns the editor used to edit items in the specified column.

  • QStringList columnTexts

    (
    • int column
    )

    Returns all data associated with Qt::DisplayRole in the given column as a string list.

  • QVariantList columnValues

    (
    • int column
    )

    Returns all data associated with ColumnEditorValueRole in the given column as a variant list.

  • QVariant data

    (
    • const QModelIndex & index
    • int role = Qt::DisplayRole
    )
  • QVariant data

    (
    • int row
    • int column
    • int role = Qt::DisplayRole
    )

    Returns the data associated with role in the given cell.

    If there is no such cell or no such data, an invalid variant will be returned.

  • QVariant defaultValue

    (
    • int column
    )
    [inline]

    Returns the default value for cells in column.

  • Qt::ItemFlags flags

    (
    • const QModelIndex & index
    )
  • QVariant headerData

    (
    • int section
    • Qt::Orientation orientation
    • int role
    )
  • bool insertColumns

    (
    • int column
    • int count = 1
    • const QModelIndex & parent = QModelIndex()
    )
  • void insertRow

    ( )

    Inserts the given items as a new row to the model.

    Parameters
    items

    a list of items. The length of this list must equal the number of columns in the model. The model takes the ownership of the pointers.

    row

    insert the row after this index. -1 means last.

  • bool insertRows

    (
    • int row
    • int count = 1
    • const QModelIndex & parent = QModelIndex()
    )
  • PiiModelItem * itemAt

    (
    • int row
    • int column = 0
    )

    Returns the item in the given cell.

    If there is no such cell, 0 will be returned.

  • QMap< int, QVariant > itemData

    (
    • const QModelIndex & index
    )

    Returns data associated with any role at the given model index.

    See PiiModelItem::_d().

  • bool removeColumns

    (
    • int column
    • int count = 1
    • const QModelIndex & parent = QModelIndex()
    )
  • bool removeRows

    (
    • int row
    • int count = 1
    • const QModelIndex & parent = QModelIndex()
    )
  • int rowCount

    (
    • const QModelIndex & parent = QModelIndex()
    )

    Returns the number of rows in the model.

  • QList< int > selectedRows

    ()

    Returns the indices of all rows in which at least one cell is selected.

    The list is sorted in ascending order.

  • void setCanDeleteLast

    (
    • bool canDeleteLast
    )

    Enables/disables deletion of the last row in the model.

    If this flag is set to false, canDelete() will return false if there is only one row left.

  • void setColumnEditorProperties

    (
    • int column
    • const QVariantMap & properties
    )
    [inline]

    Configures the column editor in the specified column.

    Except for a few special properties recognized by PiiTableModel, the given properties will be directly transferred to the editor widget.

     pModel->setHeaderTitles(QStringList() << "Combo" << "IntSpin"
                                           << "DoubleSpin" << "Text");
     // Combo box for column 0
     pModel->setColumnEditorType(0, PiiTableModel::ComboBoxEditor);
     QVariantMap map;
     map["items"] = QStringList() << tr("Jaa") << tr("Ei");
     // Create icons automatically from the given colors
     map["colors"] = QVariantList() << QColor(Qt::green) << QColor(Qt::red);
     // Use custom icons
     map["icons"] = QVariantList() << QIcon(":yes.svg") << QIcon(":no.svg");
     pModel->setColumnEditorProperties(0, map);
    
     pModel->setColumnEditorType(1, PiiTableModel::IntegerSpinBoxEditor);
     map.clear();
     map["minimum"] = 2;
     map["singleStep"] = 2;
     map["maximum"] = 24;
     pModel->setColumnEditorProperties(1, map);
    
     pModel->setColumnEditorType(2, PiiTableModel::DoubleSpinBoxEditor);
     map.clear();
     map["minimum"] = 1.0;
     map["maximum"] = 100.0;
     pModel->setColumnEditorProperties(2, map);
    
     pModel->setColumnEditorType(3, PiiTableModel::LineEditor);
     map.clear();
     QVariantMap mapValidator;
     mapValidator["regexp"] = "[1-9][0-9]{3}-[0-9]{2}-[0-9]{2}";
     map["validator"] = mapValidator;
     pModel->setColumnEditorProperties(3, map);
    

    LineEditor supports regexp and numeric validators. If the editor properties contains a QVariantMap named "validator", its properties will be used to configure a validator. The validator's type is auto-detected based on the properties. If "regexp" is present, a QRegExpValidator will be used. If "top" and "bottom" are present, a QIntValidator or QDoubleValidator will be used, based on the type (int or double) of the limits.

  • void setColumnEditorType

    ( )
    [inline]

    Sets the editor used to edit items in the specified column.

  • void setColumnTexts

    ( )

    Returns all data associated with Qt::DisplayRole in the given column as a string list.

    The number of rows in the model will be adjusted to match the length of texts.

  • void setColumnValues

    (
    • int column
    • const QVariantList & values
    )

    Sets all data associated with ColumnEditorValueRole in the given column as a variant list.

    The number of rows in the model will be adjusted to match the length of values.

  • void setData

    (
    • int row
    • int column
    • const QVariant & value
    • int role
    )

    Sets the data associated with role in the given cell.

  • bool setData

    (
    • const QModelIndex & index
    • const QVariant & value
    • int role
    )
  • void setDefaultValue

    ( )
    [inline]

    Sets the default value for cells in column.

  • bool setHeaderData

    (
    • int section
    • Qt::Orientation
    • const QVariant & value
    • int role
    )
  • void setHeaderTitles

    ( )

    Sets the column header titles with one function call.

    Parameters
    headerTitles

    the titles. The length of this list must equal the number of columns.

  • void setText

    (
    • int row
    • int column
    • const QString & text
    )
    [inline]

    Sets the displayed text in the given cell.

  • void setValue

    ( )

    Sets the value of the element at the given coordinates.

    The value may be different from the displayed text. If behavior is ChangeTextAutomatically, the text will be automatically set based on column configuration. For example, setting the value of a combo box column automatically changes the text based on the combo's item list. To disable the default behavior, set behavior DoNotChangeText.

  • QList< PiiModelItem * > takeRow

    (
    • int row
    )

    Removes row and returns its items as a list.

    The model no longer owns the pointers, and they must be deleted by the caller.

  • QString text

    (
    • int row
    • int column
    )
    [inline]

    Returns the displayed text in the given cell.

  • virtual QString textForValue

    ( )
    [virtual]

    Converts value into user-displayable text.

    The default implementation returns different text depending on the column editor.

    • LineEditor - the value will be converted to a QString

    • {Integer,Double}SpinBoxEditor - the value will be converted to a QString and wrapped into the editor's prefix and suffix, if set. The specialValueText property of the spin box will be taken into account.

    • ComboBoxEditor - the value is converted to an int, and the corresponding text in the combo's item list will be returned.

  • QVariant value

    (
    • int row
    • int column
    )
    [inline]

    Returns the data associated with ColumnEditorValueRole in the given cell.

  • virtual QWidget * createEditor

    (
    • QWidget * parent
    • int row
    • int column
    )
    [protected, virtual]

    Creates an editor for editing the item at the given coordinates.

    The default implementation creates one of the supported editor types, based on header data. If you override this function, you probably need to override setEditorData(), setModelData(), and textForValue() as well.

  • virtual PiiModelItem * createItem

    (
    • int row
    • int column
    )
    [protected, virtual]

    Creates a new item at the given coordinates.

    The default implementation sets the item's value (ColumnEditorValueRole) to the default value of the column, if given.

  • virtual void setEditorData

    (
    • QWidget * editor
    • int row
    • int column
    )
    [protected, virtual]

    Sets up the contents of editor based on the contents of the cell at the given position.

  • virtual void setModelData

    (
    • QWidget * editor
    • int row
    • int column
    )
    [protected, virtual]

    Modifies the cell at the given position based on the contents of editor.

  • void currentItemChanged

    ()
    [signal]

    Signals that the current item has changed on the attached view.

  • void deleteEnabled

    (
    • bool enabled
    )
    [signal]

    Emitted whenever the selection has changed.

    If the enabled flag is true, there are selected rows.

  • void moveDownEnabled

    (
    • bool enabled
    )
    [signal]

    Emitted whenever the selection has changed.

    If the enabled flag is true, the selected rows can be moved down.

  • void moveUpEnabled

    (
    • bool enabled
    )
    [signal]

    Emitted whenever the selection has changed.

    If the enabled flag is true, the selected rows can be moved up.

  • void selectionChanged

    ()
    [signal]

    Signals that selection has changed on the attached view.

  • void addRow

    ()
    [slot]

    Inserts a new row at the end of the table and fill with it default values.

  • void deleteSelectedRows

    ()
    [slot]

    Deletes selected rows.

  • void moveSelectedRowsDown

    ()
    [slot]

    Moves selected rows down step up.

    Does nothing if there are now selected rows or the last row is selected.

  • void moveSelectedRowsUp

    ()
    [slot]

    Moves selected rows one step up.

    Does nothing if there are now selected rows or the first row is selected.

  • void enableControls

    ()
    [slot]
Notes (0)

Add a note

Not a single note added yet. Be the first, add yours.