Closed ogoffart closed 5 months ago
Thanks @ogoffart.
Analogous behavior is triggered by custom filter model.
Here the example code:
// 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;
callback only-evens-changed <=> only-evens-switch.toggled;
VerticalBox {
only-evens-switch := Switch {
text: "Only evens";
}
ListView {
for item in root.model: Text {
text: item;
}
}
}
}
// main.cpp
#include <memory>
#include <utility>
#include "appwindow.h"
#include "slint.h"
class MyFilterModel: public slint::FilterModel<int>
{
bool onlyEvens{false};
[[nodiscard]] auto filter(const int& element) const -> bool { 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); }}
{ }
[[nodiscard]] auto getOnlyEvens() const -> bool { return onlyEvens; }
void setOnlyEvens(bool value)
{
onlyEvens = value;
// Perform some kind of magic to force filtering re-evaluation
}
};
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)};
appWindow->set_only_evens(filterModel->getOnlyEvens());
appWindow->set_model(filterModel);
appWindow->on_only_evens_changed([appWindow, filterModel]() {
filterModel->setOnlyEvens(appWindow->get_only_evens());
});
appWindow->run();
return 0;
}
Similar, calling FilterModel::reset()
triggers a call loop between FilterModel::reset()
and FilterModelInner::reset()
, that with my current toolchain ends with an exception in std::vector
implementation, after some iterations.
I don't know if a different issue should be filled for FilterModel
.
Indeed, all the reset function were having a problem. https://github.com/slint-ui/slint/pull/4969 fixes it.
Discussed in https://github.com/slint-ui/slint/discussions/4960