There might be "hackish" ways to accomplish this the way you've already started, but I think item delegates are the proper way to go.. 
Items are not edited "in place". A temporary editor widget is provided each time an item is edited. And it's the item delegate who provides this temporary editor widget used for editing items content.
One "hackish" way I was talking about would require somehow indentifying the temporary line edit object. This can most likely be done even quite easily by using QObject::findChild().
So, a better approach is to use item delegates. Delegates work like this:
QItemDelegate is the default implementation for delegates and that's what QTableWidget like any other view uses. You could subclass QItemDelegate and extend the setEditorData() behaviour so that in addition to letting the base class implementation to fill the editor you select the content as well.
void MyItemDelegate
::setEditorData(QWidget* editor,
const QModelIndex
& index
) const {
// let base class fill the editor
// select all if it's a line edit
QLineEdit* lineEdit
= qobject_cast<QLineEdit
*>
(editor
);
if (lineEdit)
lineEdit->selectAll();
}
void MyItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
// let base class fill the editor
QItemDelegate::setEditorData(editor, index);
// select all if it's a line edit
QLineEdit* lineEdit = qobject_cast<QLineEdit*>(editor);
if (lineEdit)
lineEdit->selectAll();
}
To copy to clipboard, switch view to plain text mode
Edit: Oh, and you set the extended item delegate for example like this:
tableWidget->setItemDelegate(new MyItemDelegate(tableWidget));
tableWidget->setItemDelegate(new MyItemDelegate(tableWidget));
To copy to clipboard, switch view to plain text mode
Bookmarks