It is strange to me that the default delegate for QListView displays an empty QLineEdit widget during editing instead of one filled with the text from the model's DisplayRole data. It makes me think I am doing something wrong, or inefficiently. Here is my current solution...
/*
[1] Reimplement "setEditorData" in a subclass of QStyledItemDelegate so that it puts
the text in the editor (selected by default, as expected) instead of leaving it blank.
*/
{
QStyledItemDelegate::setEditorData(editor, index);
if (editor->inherits("QLineEdit"))
{
QLineEdit *e
= static_cast<QLineEdit
*>
(editor
);
e->setText(index.data(Qt::DisplayRole).toString());
}
}
/*
[2] Set MyDelegate as the item delegate for the view before "show()"...
*/
void MyMainWindow::showListView()
{
[...]
MyListModel *model = new MyListModel(this);
model->setScene(this->currentScene());
MyDelegate *delegate = new MyDelegate(this);
view->setModel(model);
view->setItemDelegate(delegate); // <---[2]
view->show();
[...]
}
/*
[1] Reimplement "setEditorData" in a subclass of QStyledItemDelegate so that it puts
the text in the editor (selected by default, as expected) instead of leaving it blank.
*/
void MyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QStyledItemDelegate::setEditorData(editor, index);
if (editor->inherits("QLineEdit"))
{
QLineEdit *e = static_cast<QLineEdit*>(editor);
e->setText(index.data(Qt::DisplayRole).toString());
}
}
/*
[2] Set MyDelegate as the item delegate for the view before "show()"...
*/
void MyMainWindow::showListView()
{
[...]
MyListModel *model = new MyListModel(this);
model->setScene(this->currentScene());
MyDelegate *delegate = new MyDelegate(this);
QListView *view = new QListView;
view->setModel(model);
view->setItemDelegate(delegate); // <---[2]
view->show();
[...]
}
To copy to clipboard, switch view to plain text mode
...is it really necessary to subclass QStyledItemDelegate to get the desired result, or am I missing something?
Cheers,
-andy.f
Bookmarks