PDA

View Full Version : Auto text select in QTableWidgetItem



tstankey
3rd October 2006, 20:58
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(

jpn
3rd October 2006, 21:41
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:

QAbstractItemDelegate::createEditor() is called by the view every time a new editor widget is required
QAbstractItemDelegate::setEditorData() is called by the view every time editing of an item begins (the editor is asked to be populated correctly)
QAbstractItemDelegate::setModelData() is called by the view every time editing of an item ends (the data from the editor should be transferred to the model)


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
QItemDelegate::setEditorData(editor, index);

// 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:


tableWidget->setItemDelegate(new MyItemDelegate(tableWidget));

tstankey
5th October 2006, 21:40
Thanks much!
That worked fine.