View Full Version : Validating entry in QTreeView

Thuan Seah Tan
7th September 2011, 05:38
Hi everyone,

I am trying to figure out a way to do validation of entries in a QTreeView. In the tree view, I have text for each tree item which by default, the QStyledItemDelegate uses a derived class of QLineEdit as the editor. I want to be able to reject certain strings as valid inputs so I what I end up doing was derive from QStyleItemDelegate and override the createEditor method. I first call the QStyleItemDelegate createEditor method to create a new QWidget and then I try to cast it into a QLineEdit. If it is a QLineEdit object, I attach an instance of my QValidator.

Wondering if there is a better way of doing this (besides handling it at the commitData level)? The only issue I found with doing this is that the default eventFilter in QStyleItemDelegate will trigger a commitData first before QLineEdit handles the focusOut event which is where the validation is done. I got around that by overriding the eventFilter as well.

Any suggestions?