Unsolved Fine-tune initial editor state after QTableWidget::edit?
-
Suppose I programmatically set the text in a QTableWidget cell with a particular QModelIndex, then call edit() on this index, making the cell editable. If I understand correctly, QTableWidget creates a temporary QLineEdit for the editing but there is no direct way to access this widget (I don't want to bother with delegates or QTableView). I don't want to substantially change the behavior of the QLineEdit but I'd like to tweak its default state: it seems that the editor always starts with its contents selected and the cursor at the end. I'd like to manually position the cursor (say, just before the programmatically added text) and unselect everything. Is this doable? And is there a good reason why edit() defaults to selecting the whole cell contents? I can see scenarios where this is desired as default behavior but there are others when a cell may be edited repeatedly with text added more often than entirely replacing the previous text, so I'm hoping there's some flag to "turn off" selecting in this case.
-
Hi,
AFAIK, the simplest way to do that is with a minimal
QStyledItemDelegate
. There's not much code to implement for what you want so it will likely be cleaner and quicker to do. -
Hi
For a quick test i did#include <QStyledItemDelegate> class LineEditDelegate : public QStyledItemDelegate { Q_OBJECT public: LineEditDelegate(QObject *parent) : QStyledItemDelegate(parent) {} QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const { QLineEdit *editor = new QLineEdit(parent); return editor; } void setEditorData(QWidget *editor, const QModelIndex &index) const { QVariant value = index.data(Qt::EditRole); QLineEdit *lineEdit= static_cast<QLineEdit *>(editor); lineEdit->setText(value.toString()); lineEdit->deselect(); } };
But it still select the text so i am wondering what did i miss ?
-
You can replace the deselect call by
QTimer::singleShot(0, lineEdit, &QLineEdit::deselect);