C++: add code sample form model adapters

Fixes #4967
This commit is contained in:
Olivier Goffart 2025-03-31 15:27:52 +02:00
parent bdae506fd6
commit bf3c47f1fa

View file

@ -511,6 +511,14 @@ struct MapModelInner : private_api::ModelChangeListener
/// example a MapModel can be used to adapt a model that provides numbers to be a model
/// that exposes all numbers converted to strings, by calling `std::to_string` on each
/// value given in the mapping lambda expression.
///
/// \code
/// auto source_model = std::make_shared<slint::VectorModel<Person>>(...);
/// auto mapped_model = std::make_shared<slint::MapModel<Person, SharedString>>(
/// source_model, [](const Person &person) {
// return fmt::format("{} {}", person.first, person.last);
// });
/// \endcode
template<typename SourceModelData, typename MappedModelData = SourceModelData>
class MapModel : public Model<MappedModelData>
{
@ -686,6 +694,14 @@ struct SortModelInner : private_api::ModelChangeListener
/// The SortModel acts as an adapter model for a given source model by sorting all rows
/// with by order provided by the given sorting function. The sorting function is called for
/// pairs of elements of the source model.
///
/// \code
/// auto source_model = std::make_shared<slint::VectorModel<SharedString>>(
// std::vector<SharedString> { "lorem", "ipsum", "dolor" });
/// auto sorted_model = std::make_shared<slint::SortModel<SharedString>>(
/// source_model, [](auto lhs, auto rhs) { return lhs < rhs; }));
/// \endcode
template<typename ModelData>
class SortModel : public Model<ModelData>
{
@ -776,6 +792,12 @@ struct ReverseModelInner : private_api::ModelChangeListener
/// The ReverseModel acts as an adapter model for a given source model by reserving all rows.
/// This means that the first row in the source model is the last row of this model, the second
/// row is the second last, and so on.
///
/// \code
/// auto source_model = std::make_shared<slint::VectorModel<int>>(
// std::vector<int> { 1, 2, 3, 4, 5 });
/// auto reversed_model = std::make_shared<slint::ReverseModel<int>>(source_model);
/// \endcode
template<typename ModelData>
class ReverseModel : public Model<ModelData>
{