Project

General

Profile

Lazy loading failing with WTreeView ยป main.cpp

sample code for WTreeView issue - John Stark, 12/23/2022 08:52 AM

 
#include <Wt/WApplication.h>
#include <Wt/WLocale.h>
#include <Wt/WStandardItem.h>
#include <Wt/WContainerWidget.h>
#include <Wt/WPushButton.h>
#include <Wt/WStandardItemModel.h>
#include <Wt/WString.h>
#include <Wt/WTreeView.h>

namespace {
std::unique_ptr<Wt::WStandardItem> createItem( const Wt::WString& text )
{
auto item(std::make_unique<Wt::WStandardItem>());
item->setText(text);
return item;
}
}


class HelloApplication : public Wt::WApplication
{
public:
HelloApplication(const Wt::WEnvironment& env): Wt::WApplication(env)
{ }

void initialize() override
{
Wt::WApplication::initialize();

_model = std::make_shared<Wt::WStandardItemModel>(0, 1);
_btnRebuild = root()->addWidget(std::make_unique<Wt::WPushButton>("Rebuild Model"));
_btnRebuild->clicked().connect(this, &HelloApplication::populateModel);

_treeView = root()->addWidget(std::unique_ptr<Wt::WTreeView>(new Wt::WTreeView()));
_treeView->setModel(_model);
_treeView->setHeight(800);
populateModel();
}

private:
void populateModel()
{
// Wt::WApplication *app = Wt::WApplication::instance();
const Wt::WLocale& locale(this->locale());

// _model->clear(); // Works
_model->removeRows(0, _model->rowCount()); // Doesn't work

const int N = 1000;
for(int row = 0; row < N; ++row) {
_model->setItem(row, 0, createItem(locale.toString(row) + Wt::WString("-Item")));
}
}

Wt::WPushButton* _btnRebuild = nullptr;
Wt::WTreeView* _treeView = nullptr;
std::shared_ptr<Wt::WStandardItemModel> _model;
};

std::unique_ptr<Wt::WApplication> createApplication(const Wt::WEnvironment& env) {
return std::make_unique<HelloApplication>(env);
}

int main(int argc, char** argv) {
return Wt::WRun(argc, argv, &createApplication);
}
    (1-1/1)