WItemDelegate customization
Added by Hans-Michael Muller about 11 years ago
Hello,
I am trying to implement a specialized WItemDelegate. In particular, I'd like to replace the WLineEdit that comes with the default with a WTextArea for each item. So I wrote
my own delegate (see attached files), and simply added the setItemDelegate method:
model_ = new Wt::WStandardItemModel(0, 0);
table_ = new Wt::WTableView();
myowndelegate_ = new PgTableEditorItemDelegate(); // added
table*->setItemDelegate(myowndelegate*); // added
SetTable(); // some irrelevant formatting
SizeColumns(); //more irrelevant formatting
table*->setModel(model*);
containers*.table->addWidget(table*);
When implementing, I can see the text area in each cell, however, I cannot edit them, even though I have set all items Wt::ItemIsEditable. In fact, the new setEditState and editState are never called. Before adding the new class and the two line above, I could edit every cell.
Thanks for your help,
Michael.
Replies (2)
RE: WItemDelegate customization - Added by Hans-Michael Muller about 11 years ago
I think the problem is that the text area doesn't stay focussed after I clicked it once. If I keep my mouse button down, it gets the focus and I can edit the textarea. Strange.
RE: WItemDelegate customization - Added by Koen Deforche about 11 years ago
Hey Hans,
For the focusing issue I do not have an explain ready.
But I can show you an item delegate implementation that does about the same (in Java), but it's been designed for a table which always has all editors open (and thus there is no processing of events that close the editor). Perhaps it provides a hint of what to do to make it work.
tableView.setItemDelegate(new WItemDelegate() {
@Override
protected WWidget createEditor(final WModelIndex index,
EnumSet<ViewItemRenderFlag> flags) {
final WContainerWidget result = new WContainerWidget();
result.setSelectable(true);
final WTextArea textArea = new WTextArea();
textArea.setText(StringUtils.asString(index.getData(ItemDataRole.EditRole)).toString());
textArea.changed().addListener(this, new Signal.Listener() {
@Override
public void trigger() {
changed.trigger();
}
});
result.setLayout(new WHBoxLayout());
result.getLayout().setContentsMargins(1, 1, 1, 1);
result.getLayout().addWidget(textArea);
return result;
}
@Override
public Object getEditState(WWidget editor) {
WContainerWidget w = (WContainerWidget) (editor);
WTextArea textArea = (WTextArea) w.getWidget(0);
return textArea.getText();
}
@Override
public void setEditState(WWidget editor, Object value) {
WContainerWidget w = (WContainerWidget) (editor);
WTextArea textArea = (WTextArea) w.getWidget(0);
textArea.setText((String) value);
}
@Override
public WWidget update(WWidget widget, WModelIndex index,
EnumSet<ViewItemRenderFlag> flags) {
WWidget w = super.update(widget, index, flags);
WInteractWidget wi = (WInteractWidget) w;
wi.mouseWentDown().preventPropagation(false);
wi.clicked().preventPropagation(false);
return w;
}
Regards,
koen