-
Playing around with custom models, I found a peculiar behavior of While this is perfectly fine when using directly Here an example: // appwindow.slint
import { ListView, VerticalBox, Switch } from "std-widgets.slint";
export component AppWindow inherits Window {
in property <[int]> model;
in-out property only-evens <=> only-evens-switch.checked;
in-out property ascending <=> ascending-switch.checked;
callback only-evens-changed <=> only-evens-switch.toggled;
callback ascending-changed <=> ascending-switch.toggled;
VerticalBox {
only-evens-switch := Switch {
text: "Only evens";
}
ascending-switch := Switch {
text: "Ascending";
}
ListView {
for item in root.model: Text {
text: item;
}
}
}
} // main.cpp
#include <memory>
#include <utility>
#include <print>
#include "appwindow.h"
#include "slint.h"
class MyFilterModel: public slint::FilterModel<int>
{
bool onlyEvens{false};
[[nodiscard]] auto filter(const int& element) const -> bool
{
std::println("Filtering with onlyEvens={}; {} -> {}", onlyEvens, element, (!onlyEvens || element % 2 == 0));
return !onlyEvens || element % 2 == 0;
}
public:
MyFilterModel(std::shared_ptr<Model<int>> source_model):
slint::FilterModel<int>{std::move(source_model), [this](const int& element) { return filter(element); }}
{
std::println("MyFilterModel initialized, onlyEvens={}", onlyEvens);
}
[[nodiscard]] auto getOnlyEvens() const -> bool { return onlyEvens; }
void setOnlyEvens(bool value)
{
std::println("Updating filter; {} -> {}", onlyEvens, value);
onlyEvens = value;
reset();
}
};
class MySortModel: public slint::SortModel<int>
{
bool ascending{true};
[[nodiscard]] auto sort(const int& first, const int& second) const -> bool
{
std::println("Sorting with ascending={}; {} | {} -> {}", ascending, first, second, (ascending ? first < second : first >= second));
return ascending ? first < second : first >= second;
}
public:
MySortModel(std::shared_ptr<Model<int>> source_model):
slint::SortModel<int>{std::move(source_model),
[this](const int& first, const int& second) { return sort(first, second); }}
{
std::println("MySortModel initialized, ascending={}", ascending);
}
[[nodiscard]] auto getAscending() const -> bool { return ascending; }
void setAscending(bool value)
{
std::println("Updating sort; {} -> {}", ascending, value);
ascending = value;
reset();
}
};
auto main([[maybe_unused]] int argc, [[maybe_unused]] char** argv) -> int
{
auto appWindow{AppWindow::create()};
const auto model{std::make_shared<slint::VectorModel<int>>(std::vector<int>{1, 2, 3, 4, 5})};
const auto filterModel{std::make_shared<MyFilterModel>(model)};
const auto sortModel{std::make_shared<MySortModel>(filterModel)};
appWindow->set_only_evens(filterModel->getOnlyEvens());
appWindow->on_only_evens_changed([appWindow, filterModel]() {
filterModel->setOnlyEvens(appWindow->get_only_evens());
});
appWindow->set_ascending(sortModel->getAscending());
appWindow->on_ascending_changed([appWindow, sortModel]() { sortModel->setAscending(appWindow->get_ascending()); });
appWindow->set_model(sortModel);
appWindow->run();
return 0;
} The output produced by the
A possible workaround is to add a Could be a good idea to move filtering function invocation at first usage, as is done for |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Discussion resolved, see #4983 Thanks everyone! |
Beta Was this translation helpful? Give feedback.
Discussion resolved, see #4983
Thanks everyone!