Project

General

Profile

Bug #4433 ยป main.cpp

Mark O'Donovan, 08/28/2015 09:12 AM

 
#include <Wt/WApplication>
#include <Wt/WStandardItem>
#include <Wt/WStandardItemModel>
#include <Wt/WStringListModel>
#include <Wt/WTableView>
#include <Wt/WItemDelegate>
#include <Wt/WTimer>

/*
* Our example application which extends WApplication
*/
class DelegateExample: public Wt::WApplication
{
public:
/*
Constructor
*/
DelegateExample(const Wt::WEnvironment &env): Wt::WApplication(env)
{
Wt::WTableView *table = new Wt::WTableView();

//create model
std::vector<Wt::WString> options;
options.push_back("apples");
options.push_back("pears");
options.push_back("bananas");
options.push_back("cherries");

m_model = new Wt::WStandardItemModel(table);
for (unsigned i=0; i < 2; i++) {
for (unsigned j=0; j < 2; j++) {
Wt::WStandardItem *item = new Wt::WStandardItem();
item->setData(0, Wt::UserRole);
item->setData(options[0], Wt::DisplayRole);
item->setFlags(Wt::ItemIsEditable);
m_model->setItem(i, j, item);
}
}

//create table
table->setModel(m_model);
table->setEditTriggers(Wt::WAbstractItemView::SingleClicked);
Wt::WStringListModel* slModel = new Wt::WStringListModel(table);
slModel->setStringList(options);

//set a delegate
table->setItemDelegate(new Wt::WItemDelegate(this));

//add widgets to root container
Wt::WString heading( "Wt version: " + Wt::WString(WT_VERSION_STR) );
root()->addWidget( new Wt::WText(heading) );
root()->addWidget(table);

//setup timer to call updateData() every second
Wt::WTimer *update_timer = new Wt::WTimer(this);
update_timer->setInterval(1000);
update_timer->timeout().connect( std::bind( &DelegateExample::updateData, this ) );
update_timer->start();
}

private:
void updateData()
{
//emit dataChanged() for index 0,0 periodically to simulate data changes
m_model->dataChanged().emit( m_model->index(0,0), m_model->index(0,0) );
}

Wt::WStandardItemModel *m_model;
};


/*
Return a new WApplication
*/
Wt::WApplication* createApplication(const Wt::WEnvironment &env)
{
return new DelegateExample(env);
}


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