Guest User

Combobox Delegate

a guest
Jan 11th, 2013
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #include <Wt/WGlobal>
  2. #include <Wt/WItemDelegate>
  3.  
  4. #include <boost/cast.hpp>
  5.  
  6. class ItemDelegate : public WItemDelegate {
  7.    void OnCloseEditor (Wt::WWidget* editor, bool save) const
  8.    {
  9.       closeEditor().emit(editor, save);
  10.    }
  11.  
  12.    public:
  13.       ItemDelegate (WObject* parent = nullptr)
  14.          :WItemDelegate(parent)
  15.       {
  16.       }
  17.  
  18.       WWidget* createEditor (const WModelIndex& index, WFlags<ViewItemRenderFlag>) const override
  19.       {
  20.          auto container = new Wt::WContainerWidget;
  21.  
  22.          auto cb = new WComboBox(container);
  23.          cb->addItem(WString("foo"));
  24.          cb->addItem(WString("bar"));
  25.  
  26.          cb->setCurrentIndex(cb->findText(asString(index.data(EditRole))));
  27.  
  28.          cb->activated().connect(std::bind(&ItemDelegate::OnCloseEditor, this, container, true));
  29.          cb->setFocus();
  30.  
  31.          return container;
  32.       }
  33.  
  34.       void setEditState (WWidget* editor, const boost::any& value) const override
  35.       {
  36.          auto container = boost::polymorphic_cast<WContainerWidget*>(editor);
  37.          auto cb = boost::polymorphic_cast<WComboBox*>(container->widget(0));
  38.  
  39.          cb->setCurrentIndex(cb->findText(asString(value)));
  40.       }
  41.  
  42.       boost::any editState (WWidget* editor) const override
  43.       {
  44.          auto container = boost::polymorphic_cast<WContainerWidget*>(editor);
  45.          auto cb = boost::polymorphic_cast<WComboBox*>(container->widget(0));
  46.  
  47.          return cb->currentText();
  48.       }
  49. };
Advertisement
Add Comment
Please, Sign In to add comment