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
Itemthat has to implement theQModelItemtrait. Roles are derived from theQModelItemimplementation. - 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
QTableModelBasetrait 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§
Sourcetype Item: QModelItem + Default + Clone
type Item: QModelItem + Default + Clone
The item type stored in the model.
Items must:
- Implement
QModelItemto integrate with Qt - Be
Defaultfor creating new items - Be
Clonefor safe data access and copying
Required Methods§
Sourcefn column_count(&self) -> usize
fn column_count(&self) -> usize
Returns the number of columns in the table.
Provided Methods§
Sourcefn set_unnotified(&mut self, _index: (usize, usize), _value: Self::Item) -> bool
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.
Sourcefn push_row_unnotified(&mut self, values: &[Self::Item])
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.
Sourcefn push_column_unnotified(&mut self, values: &[Self::Item])
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.
Sourcefn insert_row_unnotified(&mut self, _index: usize, _value: &[Self::Item])
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.
Sourcefn insert_column_unnotified(&mut self, _index: usize, _value: &[Self::Item])
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.
Sourcefn pop_row_unnotified(&mut self) -> Option<Vec<Self::Item>>
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.
Sourcefn pop_column_unnotified(&mut self) -> Option<Vec<Self::Item>>
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.
Sourcefn remove_row_unnotified(&mut self, _index: usize) -> Vec<Self::Item>
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.
Sourcefn remove_column_unnotified(&mut self, _index: usize) -> Vec<Self::Item>
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.
Sourcefn reset_unnotified(&mut self)
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.