April 4, 2011

wjlrkjjv wjlrkjjv
Lab Rat
18 posts

[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?

9 replies

April 5, 2011

Gerolf Gerolf
Robot Herder
3235 posts

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.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

April 5, 2011

Andre Andre
Robot Herder
6295 posts

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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 5, 2011

wjlrkjjv wjlrkjjv
Lab Rat
18 posts

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?

April 5, 2011

Volker Volker
Ant Farmer
5428 posts

No. The delegates work with standard line edits without any additional signal, so it must be something different.

April 5, 2011

wjlrkjjv wjlrkjjv
Lab Rat
18 posts

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().

April 5, 2011

Gerolf Gerolf
Robot Herder
3235 posts

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.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

April 5, 2011

wjlrkjjv wjlrkjjv
Lab Rat
18 posts

Ok, thanks again!
I think this thread is done.

April 5, 2011

Volker Volker
Ant Farmer
5428 posts

It’s generally recommended to use QStyledItemDelegate instead of QItemDelegate.

See the note in the docs [doc.qt.nokia.com]:

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.

April 6, 2011

Andre Andre
Robot Herder
6295 posts

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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

 
  ‹‹ How to enable a submit button after multiple form fields have been verified.      Non Native Gui ››

You must log in to post a reply. Not a member yet? Register here!