TTkAbstractTableModel๐ถ๏ธ
- class TTkAbstractTableModel[source]๐ถ๏ธ
Bases:
objectTTkAbstractTableModelprovides a standard interface for models that represent their data as a two-dimensional array of items. It is not used directly, but must be subclassed.Since the model provides a more specialized interface than
TTkAbstractItemModel, it is not suitable for use with tree views.The
rowCount()andcolumnCount()functions return the dimensions of the table.Subclassing
When subclassing
TTkAbstractTableModel, you must implementrowCount(),columnCount(), anddata(). Well behaved models will also implementheaderData().Editable models need to implement
setData().Built-In Implementation
TTkTableModelListbasic subclass implementing a 2d list as data structureTTkTableModelCSVsubclass ofTTkTableModelListincluding the api to import csv dataTTkTableModelSQLite3subclass ofTTkTableModelListincluding support for sqlite3 databasesSignals๐ถ๏ธ
This signal is emitted whenever the data in an existing item changes.
This signal is emitted whenever the model changes.
Members๐ถ๏ธ
- dataChanged: pyTTkSignal๐ถ๏ธ
This signal is emitted whenever the data in an existing item changes.
If more items are affected, the pos/size definne the minimum area including all of those changes.
When reimplementing the
setData()function, this signal must be emitted explicitly.- Parameters:
pos (tuple(int,int)) โ the topLeft margin of the modified area
size (tuple(int,int)) โ the size of the modified area
- modelChanged: pyTTkSignal๐ถ๏ธ
This signal is emitted whenever the model changes.
When the model topology changes, this signal must be emitted explicitly.
Methods๐ถ๏ธ
- data(row: int, col: int) Any[source]๐ถ๏ธ
Returns the data stored for the item referred to by the row/column.
Note: If you do not have a value to return, return None instead of returning 0.
- Parameters:
row (int) โ the row position of the data
col (int) โ the column position of the data
- Returns:
object
- displayData(row: int, col: int) Tuple[TTkString, Alignment][source]๐ถ๏ธ
Returns the display representation and alignment for the data stored at the specified row/column position.
This method provides the formatted text representation of the cell data along with its preferred alignment for display purposes in table widgets. It combines the functionality of
ttkStringData()for text formatting with alignment information for proper cell rendering.The default implementation returns the
TTkStringrepresentation of the data with left alignment. Subclasses can override this method to provide custom formatting and alignment based on data type, column purpose, or other display requirements.Usage Examples:
Numeric columns might return right alignment for better visual presentation
Boolean columns might return center alignment with custom symbols
Date columns might return formatted date strings with specific alignment
- Parameters:
row (int) โ The row position of the data (0-based index)
col (int) โ The column position of the data (0-based index)
- Returns:
A tuple containing the formatted text and its preferred alignment
- Return type:
Tuple[
TTkString,TTkK.Alignment]
Example Implementation:
def displayData(self, row: int, col: int) -> Tuple[TTkString, TTkK.Alignment]: data = self.data(row, col) # Custom formatting based on column type if isinstance(data, (int, float)): # Right-align numeric data return TTkString(f"{data:,.2f}"), TTkK.Alignment.RIGHT_ALIGN elif isinstance(data, bool): # Center-align boolean with custom symbols symbol = "โ" if data else "โ" return TTkString(symbol), TTkK.Alignment.CENTER_ALIGN else: # Default left-align for text return self.ttkStringData(row, col), TTkK.Alignment.LEFT_ALIGN
- flags(row: int, col: int) ItemFlag[source]๐ถ๏ธ
Returns the item flags for the given row,column.
The base class implementation returns a combination of flags that enables the item (
ItemIsEnabled) and allows it to be selected (ItemIsSelectable).- Parameters:
row (int) โ the row position od the data
col (int) โ the column position of the data
- Returns:
- headerData(pos: int, orientation: Direction) TTkString[source]๐ถ๏ธ
Returns the data for the given role and section in the header with the specified orientation.
For horizontal headers, the section number corresponds to the column number. Similarly, for vertical headers, the section number corresponds to the row number.
- Parameters:
pos (int) โ the position (col or row) of the header
orientation (
TTkConstant.Direction) โ the orienttin of the header to be retrieved
- Returns:
- index(row: int, col: int) TTkModelIndex[source]๐ถ๏ธ
Returns the index of the item in the model specified by the given row, column.
- Parameters:
row (int) โ the row position of the index
col (int) โ the column position of the index
- Returns:
- insertColumn(column: int) bool[source]๐ถ๏ธ
Inserts a single column before the given column.
Note
This function calls the method
TTkAbstractTableModel.insertColumns().- Parameters:
column (int) โ The column index.
- Returns:
true if the column is inserted; otherwise returns false.
- Return type:
bool
- insertColumns(column: int, count: int) bool[source]๐ถ๏ธ
On models that support this, inserts <count> new columns into the model before the given column..
If column is 0, the columns are prepended to any existing columns.
If column is
TTkAbstractTableModel.columnCount(), the columns are appended to any existing columns.If you implement your own model, you can reimplement this function if you want to support insertions. Alternatively, you can provide your own API for altering the data.
Note
The base class implementation (
TTkAbstractTableModel) of this function does nothing and returns false.- Parameters:
column (int) โ Starting position of columns to insert (0-based index).
count (int) โ The number of columns to be inserted.
- Returns:
true if the columns were successfully inserted; otherwise returns false.
- Return type:
bool
- insertRow(row: int) bool[source]๐ถ๏ธ
Inserts a single row before the given row.
Note
This function calls the method
TTkAbstractTableModel.insertRows().- Parameters:
row (int) โ The row index.
- Returns:
true if the row is inserted; otherwise returns false.
- Return type:
bool
- insertRows(row: int, count: int) bool[source]๐ถ๏ธ
On models that support this, inserts count rows into the model before the given row.
If row is 0, the rows are prepended to any existing rows in the parent.
If row is
TTkAbstractTableModel.rowCount(), the rows are appended to any existing rows in the parent.If you implement your own model, you can reimplement this function if you want to support insertions. Alternatively, you can provide your own API for altering the data.
Note
The base class implementation (
TTkAbstractTableModel) of this function does nothing and returns false.- Parameters:
row (int) โ Starting position of rows to insert (0-based index)
count (int) โ The number of rows to be inserted.
- Returns:
true if the rows were successfully inserted; otherwise returns false.
- Return type:
bool
- removeColumn(column: int) bool[source]๐ถ๏ธ
Removes the given column.
Note
This function calls the method
TTkAbstractTableModel.removeColumns().- Parameters:
column (int) โ The column index.
- Returns:
true if the column is removed; otherwise returns false.
- Return type:
bool
- removeColumns(column: int, count: int) bool[source]๐ถ๏ธ
On models that support this, removes count columns starting with the given column.
If you implement your own model, you can reimplement this function if you want to support removing. Alternatively, you can provide your own API for altering the data.
Note
The base class implementation (
TTkAbstractTableModel) of this function does nothing and returns false.- Parameters:
column (int) โ Starting position of columns to remove (0-based index)
count (int) โ The number of columns to remove.
- Returns:
true if the columns were successfully removed; otherwise returns false.
- Return type:
bool
- removeRow(row: int) bool[source]๐ถ๏ธ
Removes the given row.
Note
This function calls the method
TTkAbstractTableModel.removeRows().- Parameters:
row (int) โ The row index.
- Returns:
true if the row is removed; otherwise returns false.
- Return type:
bool
- removeRows(row: int, count: int) bool[source]๐ถ๏ธ
On models that support this, removes count rows starting with the given row.
If you implement your own model, you can reimplement this function if you want to support removing. Alternatively, you can provide your own API for altering the data.
Note
The base class implementation (
TTkAbstractTableModel) of this function does nothing and returns false.- Parameters:
row (int) โ Starting position of rows to remove (0-based index)
count (int) โ The number of rows to remove.
- Returns:
true if the rows were successfully removed; otherwise returns false.
- Return type:
bool
- setData(row: int, col: int, data: object) bool[source]๐ถ๏ธ
Returns true if successful; otherwise returns false.
The
dataChanged()signal should be emitted if the data was successfully set.The base class implementation returns false. This function and
data()must be reimplemented for editable models.- Parameters:
row (int) โ the row position of the data
col (int) โ the column position of the data
data (object) โ the data to be set in the (row,col) position of the table
- Returns:
bool
- sort(column: int, order: SortOrder) None[source]๐ถ๏ธ
Sorts the model by column in the given order.
- Parameters:
column (int) โ The column index to be sorted, if -1 is provided the original unsorted order is used.
order (
TTkConstant.SortOrder) โ the sorting order