[Solved] QTableView + QAbstractItemDelegate: Custom editor doesnt close on enter or when "clicking away"?
-
Hi!
Can someone tell me how to correctly use delegation? I actually want to use QValidator on QTableView "cells".
Seems that there's no other way than to delegate.So I've implemented QAbstractItemDelegate::createEditor(), but when editing the cell, the custom created QLineEdit doesn't close again as it should normally by pressing enter or clicking into the QTableView-viewport (it closes when clicking anywhere on any row though).
What am I missing?
-
Please show some code (especially the delegate) and tell a bit, which version, compiler and platform you are using.
I'm heavily using custom delegates with line edits, combo boxes etc and it works perfect.
The best would be a simple, working (or not :-) ) example. -
At least, make sure the example is self-contained and compilable. Preferably, post a link to a complete package we can just build here and post the critical code sections you want to discuss in a posting here.
-
Thanks for your responses.
Before I go with an example, another question, because it seems I got it working now.closeEditor(...) signal must be emitted by the custom QLineEdit returned in QAbstractItemDelegate::createEditor().
So I subclassed QLineEdit and let it emit closeEditor(...) on return, escape and lost focus.
And then I connected MyLineEdit::closeEditor(...) with QTableView::closeEditor(...).Now the editor closes properly whenever I click away or press return or escape.
Is this the (a) correct way?
-
No. The delegates work with standard line edits without any additional signal, so it must be something different.
-
Hmm... I had to inherit my delegate from QItemDelegate, not from QAbstractItemDelegate.
But I saw code using QAbstractItemDelegate I'm sure. I guess I still didn't get the basics...
Now it works (behaves like default) by just returning a new widget (without frame) in createEditor(), and setting geometry in updateEditorGeometry().
Sure I won't forget about setEditorData() and setModelData(). -
QItemDelegate implements all the needed stuff fopr you, QAbstractItemDelegate is only the interface with some basics, not all needed stuff.
If you potentially want to use style sheets, I suggest using QStyledItemDelegate instead of QItemDelegate.
-
Ok, thanks again!
I think this thread is done. -
It's generally recommended to use QStyledItemDelegate instead of QItemDelegate.
See the "note in the docs":http://doc.qt.nokia.com/4.7/qitemdelegate.html#qstyleditemdelegate-vs-qitemdelegate:
bq. QStyledItemDelegate vs. QItemDelegate
Since Qt 4.4, there are two delegate classes: QItemDelegate and QStyledItemDelegate. However, the default delegate is QStyledItemDelegate. These two classes are independent alternatives to painting and providing editors for items in views. The difference between them is that QStyledItemDelegate uses the current style to paint its items. We therefore recommend using QStyledItemDelegate as the base class when implementing custom delegates or when working with Qt style sheets. The code required for either class should be equal unless the custom delegate needs to use the style for drawing. -
Problem with that is, that the API of both delegates are completely different. Some things that were easy with QItemDelegate are neigh on inmpossible with QStyledItemDelegate. If you can manage what you need to do with QStyledItemDelegate, then that is obviously the way to go, but otherwise QItemDelegate is still an option, IMHO.