Auto text select in QTableWidgetItem
Does anybody know how to have all of the text "selected" in a QTableWidgetItem after a double mouse click?
Currently, the local editor is invoked, the cursor shows up, but you still have to use the mouse to select the existing text.
The starting point for my code is...
void SpreadSheet::mouseDoubleClickEvent(QMouseEvent* pevent)
{
QTableWidget::mouseDoubleClickEvent(pevent);
} // SpreadSheet::mouseDoubleClickEvent(
Re: Auto text select in QTableWidgetItem
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.
Code:
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();
}
Edit: Oh, and you set the extended item delegate for example like this:
Code:
tableWidget->setItemDelegate(new MyItemDelegate(tableWidget));
Re: Auto text select in QTableWidgetItem
Thanks much!
That worked fine.