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
|
(
Emitted whenever the selection has changed. |
|
void
|
(
Emitted whenever the selection has changed. |
|
void
|
(
Emitted whenever the selection has changed. |
|
void
|
( )
Signals that selection has changed on the attached view. |
Constructors and destructor
|
(
Creates a new PiiTableModel and binds it to the |
|
|
( )
Destroys the model. |
Public member functions
|
bool
|
( )
Returns |
|
bool
|
( )
Returns |
|
bool
|
( )
Returns |
|
bool
|
( )
Returns |
|
void
|
( )
Clears all data, excluding column header definitions. |
|
int
|
(
Returns the number of columns in the model. |
|
QVariantMap
|
(
Returns the properties of the editor at |
|
(
Returns the editor used to edit items in the specified column. |
|
|
(
Returns all data associated with |
|
|
QVariantList
|
(
Returns all data associated with |
|
(
|
|
|
(
Returns the data associated with |
|
|
(
Returns the default value for cells in |
|
|
Qt::ItemFlags
|
(
|
|
(
|
|
|
bool
|
(
|
|
void
|
Inserts the given items as a new row to the model. |
|
bool
|
(
|
|
(
Returns the item in the given cell. |
|
|
(
Returns data associated with any role at the given model index. |
|
|
bool
|
(
|
|
bool
|
(
|
|
int
|
(
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
|
(
Enables/disables deletion of the last row in the model. |
|
void
|
(
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 |
|
void
|
(
Sets all data associated with |
|
void
|
Sets the data associated with |
|
bool
|
|
|
void
|
Sets the default |
|
bool
|
|
|
void
|
Sets the column header titles with one function call. |
|
void
|
Sets the displayed text in the given cell. |
|
void
|
(
Sets the value of the element at the given coordinates. |
|
QList<
PiiModelItem * >
|
(
Removes |
|
(
Returns the displayed text in the given cell. |
|
|
virtual QString
|
Converts |
|
(
Returns the data associated with |
Protected member functions
|
virtual QWidget *
|
Creates an editor for editing the item at the given coordinates. |
|
virtual PiiModelItem *
|
(
Creates a new item at the given coordinates. |
|
virtual void
|
Sets up the contents of |
|
virtual void
|
Modifies the cell at the given position based on the contents of
|
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 thevalidatorproperty (QVariantMap). -
IntegerSpinBoxEditor- a QSpinBox. Suitable for integer input. The value is an integer. If the spin box has theprefixa andsuffixproperties 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 theprefixa andsuffixproperties 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 theitemsproperty (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
parentview.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
trueif the selected rows can be deleted,falseotherwise.Deleting rows down is possible if a) at least one row is selected and b) either the canDeleteLast() flag is
trueor there are more than one row left. -
bool canDeleteLast
()Returns
trueif the last row can be deleted,falseotherwise. -
bool canMoveDown
()Returns
trueif the selected rows can be moved down,falseotherwise.Moving rows down is possible if a) at least one row is selected and b) the last row is not selected.
-
bool canMoveUp
()Returns
trueif the selected rows can be moved up,falseotherwise.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.
-
Returns the editor used to edit items in the specified column.
-
QStringList columnTexts
(- int column
Returns all data associated with
Qt::DisplayRolein the givencolumnas a string list. -
QVariantList columnValues
(- int column
Returns all data associated with
ColumnEditorValueRolein the givencolumnas 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
rolein the given cell.If there is no such cell or no such data, an invalid variant will be returned.
-
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.
-
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 returnfalseif 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);
LineEditorsupports 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. -
Sets the editor used to edit items in the specified column.
-
void setColumnTexts
Returns all data associated with
Qt::DisplayRolein the givencolumnas 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
ColumnEditorValueRolein the givencolumnas a variant list.The number of rows in the model will be adjusted to match the length of
values. -
void setData
Sets the data associated with
rolein the given cell. -
bool setData
-
Sets the default
valuefor cells incolumn. -
bool setHeaderData
-
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.
-
Sets the displayed text in the given cell.
-
void setValue
(- int row
- int column
- const QVariant & value
- ValueChangeBehavior behavior = ChangeTextAutomatically
Sets the value of the element at the given coordinates.
The value may be different from the displayed text. If
behaviorisChangeTextAutomatically, 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, setbehaviorDoNotChangeText. -
QList< PiiModelItem * > takeRow
(- int row
Removes
rowand returns its items as a list.The model no longer owns the pointers, and they must be deleted by the caller.
-
Returns the displayed text in the given cell.
-
Converts
valueinto 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
prefixandsuffix, if set. ThespecialValueTextproperty 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.
-
-
Returns the data associated with
ColumnEditorValueRolein the given cell. -
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.
-
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. -
Sets up the contents of
editorbased on the contents of the cell at the given position. -
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
enabledflag istrue, there are selected rows. -
void moveDownEnabled
(- bool enabled
[signal]Emitted whenever the selection has changed.
If the
enabledflag istrue, the selected rows can be moved down. -
void moveUpEnabled
(- bool enabled
[signal]Emitted whenever the selection has changed.
If the
enabledflag istrue, 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]
Add a note
Not a single note added yet. Be the first, add yours.