PDA

View Full Version : QPlainTextEdit in delegate, how to confirm editing using TAB ?



boo9
16th June 2016, 19:44
I am using QPlainTextEdit editor in QTableView text column delegate.

QWidget * myDelegate_MLine::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const
{
return new QPlainTextEdit(parent);
}
THe only way I can complete/confirm QPlainTextEdit text changes is to click other column cell.
I would like to override the TAB key press to confirm QPlainTextEdit editing and move to next column of the row (or using Ctrl+Enter to commit row and move to next row)

I have other column delegate, using qlineedit, and when I press Enter when editing the cell, the row commits and focus stays on that field.
I would like to achieve same behaviour, when editing cell using QPlainTextEdit, pressing Ctrl+Enter should commit the cell changes to the database and focus should be left on the QPlainTextEdit widget. Then pressing the TAB should move to next cell in the same row.

anda_skoa
16th June 2016, 20:19
You could install the delegate as an event fiilter on the editor and then react to tab.

Cheers,
_

boo9
16th June 2016, 22:22
> then react to tab.
I considered that, but I dont know what the reaction should be :(
should I act on QPlainTextEdit->call() or else ?

anda_skoa
17th June 2016, 10:23
I considered that, but I dont know what the reaction should be :(

I am afraid I don't understand. You said you wanted to commit the data and move to the next cell.



should I act on QPlainTextEdit->call() or else ?
You act on the tab mouse press event that the event filter gets before it reaches the text edit.

Cheers,
_

boo9
17th June 2016, 13:15
> I am afraid I don't understand

Editing cell with default delegate:
When I insert a new row in TableView (the new row gets "*" mark) and the cell is varchar(), I get standard delegate behaving like QLineEdit. After inputing the text I can
- press TAB to move to next cell in the row (the "*" stays unchagned), or
- press Enter/Return after which the row commits (the "*" changes to numeric row sequence) and the focus stays on current cell, the cell is selected.

Editing cell with default my custom delegate (using QPlainTextEdit).
When I insert a new row, and start editing a cell with my custom delegate, the row is marked "*", I want similar behaviour as above:
1) pressing TAB should move focus to next cell (the row mark should stay "*")
2) pressing Control+Enter should move focus commit row ("*" row mark should change to numeric) and focus should stay on the current cell and cell should be selected.

I manage to figure out 1) by calling QPlainTextEdit->setTabChangesFocus(true)
I dont know how to achive 2)

I did subclass QPlainTextEdit with keyPressEvent() member so I can see all keys being sent.
I am intercepting Key_Tab, or (Ctrl+Enter), but I dont know what code should I call to achive 2) behaviour
I dont know what the code of QPlainTextEdit does to achive 1) when I call QPlainTextEdit->setTabChangesFocus(true); I would like to do similar for other key sequences.

THis is what I have now

class myMultiLineEdit : public QPlainTextEdit
{
public:
myMultiLineEdit (QWidget * parent) : QPlainTextEdit(parent) {}
virtual void keyPressEvent(QKeyEvent *ee)
{
if(ee->key() == Qt::Key_Tab)
{
qDebug() << "tab pressed";
focusNextChild() || focusPreviousChild();
}
else
QPlainTextEdit::keyPressEvent(ee);
}
};

but focusNextChild() || focusPreviousChild() does nothing, the editor control does not close and does not move to next cell.

cheers

anda_skoa
17th June 2016, 13:45
I think if your editor wants to indicate that it is finished editing, then the delegate has to emit the commitData() signal.

Cheers,
_

boo9
17th June 2016, 18:38
TAB works, thank you very much, exits editor and moves to editing next cell

However, when the record is inserted ("*" record mark)
this does not commit the row, the "*" mark stays and does not change to numeric row id.
What do I need to do to commit the row to db (to change "*" to numeric), as default delegate does for varchar() columns ?

EDIT:
to commit the row one must call QAbstractItemModel::submit()



void myDelegate_MLine::commitAndCloseEditor()
{
myMultiLineEdit *editor = qobject_cast<myMultiLineEdit *>(sender());
emit commitData(editor);
emit closeEditor(editor, QAbstractItemDelegate::EditNextItem);
model->submit();
}