QTableModel

Trait QTableModel 

Source
pub trait QTableModel {
    type Item: QModelItem + Default + Clone;

Show 13 methods // Required methods fn row_count(&self) -> usize; fn column_count(&self) -> usize; fn get(&self, index: (usize, usize)) -> Option<&Self::Item>; // Provided methods fn set_unnotified( &mut self, _index: (usize, usize), _value: Self::Item, ) -> bool { ... } fn push_row_unnotified(&mut self, values: &[Self::Item]) { ... } fn push_column_unnotified(&mut self, values: &[Self::Item]) { ... } fn insert_row_unnotified(&mut self, _index: usize, _value: &[Self::Item]) { ... } fn insert_column_unnotified(&mut self, _index: usize, _value: &[Self::Item]) { ... } fn pop_row_unnotified(&mut self) -> Option<Vec<Self::Item>> { ... } fn pop_column_unnotified(&mut self) -> Option<Vec<Self::Item>> { ... } fn remove_row_unnotified(&mut self, _index: usize) -> Vec<Self::Item> { ... } fn remove_column_unnotified(&mut self, _index: usize) -> Vec<Self::Item> { ... } fn reset_unnotified(&mut self) { ... }
}
Expand description

A trait representing a table-based Qt model.

QTableModel provides an interface for table-like data structures that are exposed to Qt through the Model-View concept. https://doc.qt.io/qt-6/qtquick-modelviewsdata-modelview.html.

This trait requires the qobject macro to set up the correct Qt proxy. The macro will further generate functionality in the form of the QTableModelBase trait that supplements the QTableModel functionality.

§Design

  • The model owns items of associated type Item that has to implement the QModelItem trait. Roles are derived from the QModelItem implementation.
  • Mutation methods are provided in an unnotified form, meaning they modify the underlying data without emitting Qt model signals.
  • These methods are used by the automatically implemented QTableModelBase trait to create methods that collaborate the UI about changes in collections.

As a minimum you have to implement the methods QTableModel::row_count, QTableModel::column_count and QTableModel::get to create a readable list model. Further methods can be implemented to make the model fully mutable.

Methods that do not return an Option or a boolean value must succeed and perform exactly the operation described in the documentation to avoid invalidating the synchronization between any views and the underlying data. No additional structural changes may occur outside the provided functions.

Note, that default implementations may panic! if the corresponding method is not overridden. It is your responsibility to make sure that these functions are not called from QML.

§Example

use qtbridge::qobject;
#[qobject(Base = QTableModel)]
mod backend {
    use qtbridge::{QTableModel, QTableModelBase};

    #[derive(Default)]
    pub struct Backend {
        string_data: Vec<Vec<String>>,
    }
    impl QTableModel for Backend {
        type Item = String;

        fn len(&self) -> usize {
            self.string_data.len()
        }
        fn column_count(&self) -> usize {
            self.string_data[0].len
        }
        fn get(&self, index: (usize, usize)) -> Option<&Self::Item> {
            self.string_list.get(index.0)?.get(index.1)
        }
    }
}

The table model can be used in QML views as follows

TableView {
    model: backend
    delegate: Text {
        required property string value
        text: value
    }
}

Required Associated Types§

Source

type Item: QModelItem + Default + Clone

The item type stored in the model.

Items must:

  • Implement QModelItem to integrate with Qt
  • Be Default for creating new items
  • Be Clone for safe data access and copying

Required Methods§

Source

fn row_count(&self) -> usize

Returns the number of rows in the table.

Source

fn column_count(&self) -> usize

Returns the number of columns in the table.

Source

fn get(&self, index: (usize, usize)) -> Option<&Self::Item>

Returns a reference to the item at index, or None if the index is out of bounds.

Provided Methods§

Source

fn set_unnotified(&mut self, _index: (usize, usize), _value: Self::Item) -> bool

Sets the item at index. Reimplement this function but call QTableModelBase::set to notify Qt about the modification.

Returns true if the value was successfully set, or false if the operation failed (e.g., index out of bounds or value fails validation by the business logic).

The default implementation does nothing and returns false.

Source

fn push_row_unnotified(&mut self, values: &[Self::Item])

Appends a row of items to the end of the model. Reimplement this function but call QTableModelBase::push_row to notify Qt about the modification.

The function has to accept the value. Validation has to be done before this function is called.

The default implementation falls back to QTableModel::insert_row_unnotified, which in turn panics by default.

Source

fn push_column_unnotified(&mut self, values: &[Self::Item])

Appends a column of items to the end of the model. Reimplement this function but call QTableModelBase::push_column to notify Qt about the modification.

The function has to accept the value. Validation has to be done before this function is called.

The default implementation falls back to QTableModel::insert_column_unnotified, which in turn panics by default.

Source

fn insert_row_unnotified(&mut self, _index: usize, _value: &[Self::Item])

Inserts a row with value at index. Reimplement this function but call QTableModelBase::insert_row to notify Qt about the modification.

The function has to accept the value. Validation has to be done before this function is called.

Panics by default. Implementors must override this method to support insertion.

Source

fn insert_column_unnotified(&mut self, _index: usize, _value: &[Self::Item])

Inserts a column with values at index. Reimplement this function but call QTableModelBase::insert_column to notify Qt about the modification.

The function has to accept the value. Validation has to be done before this function is called.

Panics by default. Implementors must override this method to support insertion.

Source

fn pop_row_unnotified(&mut self) -> Option<Vec<Self::Item>>

Removes and returns the last row in the model. Reimplement this function but call QTableModelBase::pop_row to notify Qt about the modification.

Returns None if the model is empty. If the model is not empty, the function has to guarantee the success of the operation.

The default implementation falls back to QTableModel::remove_row_unnotified, which in turn panics by default.

Source

fn pop_column_unnotified(&mut self) -> Option<Vec<Self::Item>>

Removes and returns the last column in the model. Reimplement this function but call QTableModelBase::pop_column to notify Qt about the modification.

Returns None if the model is empty. If the model is not empty, the function has to guarantee the success of the operation.

The default implementation falls back to QTableModel::remove_column_unnotified, which in turn panics by default.

Source

fn remove_row_unnotified(&mut self, _index: usize) -> Vec<Self::Item>

Removes and returns the item at index. Reimplement this function but call QTableModelBase::remove_row to notify Qt about the modification.

The index must be valid and the model has to guarantee the success of the operation.

Panics by default. Implementors must override this method to support removal.

Source

fn remove_column_unnotified(&mut self, _index: usize) -> Vec<Self::Item>

Removes and returns the column at index. Reimplement this function but call QTableModelBase::remove_column to notify Qt about the modification.

The index must be valid and the model has to guarantee the success of the operation.

Panics by default. Implementors must override this method to support removal.

Source

fn reset_unnotified(&mut self)

Resets the model’s internal storage. Reimplement this function but call QTableModelBase::reset to notify Qt about the modification.

Panics by default. Implementors must override this method to support a model reset.

After QTableModel::reset_unnotified returns, the internal storage must reflect the new model state: QTableModel::row_count and QTableModel::get must be consistent with the updated storage.

Implementors§