Qt-interest Archive, January 2008
QTreeWidgetITem and QValidator
Message 1 in thread
Good morning.
I would like to integrate a QValidator in a writable column of a
QTreeWidgetItem.
Is it possible?
I would not like to set a QLineEdit as a widget for that column.
Thank you very much
Giacomo
--
[ signature omitted ]
Message 2 in thread
> I would like to integrate a QValidator in a writable column
> of a QTreeWidgetItem.
You can try deriving from QItemDelegate and overriding the createEditor
function. Within the function, call the base class createEditor
function. Then, if the editor inherits a QLineEdit
(qobject_cast<QLineEdit*>(editor) != NULL), you can attach the desired
validator to the line editor and return it.
Then, simply create an instance of your delegate and set it on the
QTreeWidget. Of course, you are then limiting yourself to the whole
tree, a row, or a column. You should be able to use the QModelIndex
(passed to createEditor) to decide whether or not to apply the validator
or to change it based on the item.
Rob
--
[ signature omitted ]
Message 3 in thread
Thank you robert.
I am looking at the model/view framework..
perhaps you intended that..
anyway, when i create my widget item, i already have to
create it with the editor supporting the validator.
I cannot call setItemWidget on any item in the tree.
Thank you for any other hints.
2008/1/22, Bachrach, Robert L <rob.bachrach@xxxxxxxxxxxxxxxxxxx>:
> > I would like to integrate a QValidator in a writable column
> > of a QTreeWidgetItem.
>
> You can try deriving from QItemDelegate and overriding the createEditor
> function. Within the function, call the base class createEditor
> function. Then, if the editor inherits a QLineEdit
> (qobject_cast<QLineEdit*>(editor) != NULL), you can attach the desired
> validator to the line editor and return it.
>
> Then, simply create an instance of your delegate and set it on the
> QTreeWidget. Of course, you are then limiting yourself to the whole
> tree, a row, or a column. You should be able to use the QModelIndex
> (passed to createEditor) to decide whether or not to apply the validator
> or to change it based on the item.
>
> Rob
>
--
[ signature omitted ]
Message 4 in thread
Giacomo,
I understand that you are using the model/view framework and cannot call
setItemWidget, nor am I recommending that you do so. When you do not
call setItemWidget, the standard view widgets use QItemDelegate to
create a QLineEdit derived editor. I am suggesting that you intercept
the creation of that editor (through the creation of your own delegate)
and apply the validator to the editor that the view widget automatically
created for you. If you need to vary the validator on a per-item basis,
you can add a map to your delegate the stores different validators for
different items and references them via the model index based to
"createEditor".
> From: Giacomo [mailto:delleceste@xxxxxxxxx]
> Thank you robert.
>
> I am looking at the model/view framework..
>
> perhaps you intended that..
>
> anyway, when i create my widget item, i already have to
> create it with the editor supporting the validator.
>
> I cannot call setItemWidget on any item in the tree.
>
> Thank you for any other hints.
>
> 2008/1/22, Bachrach, Robert L <rob.bachrach@xxxxxxxxxxxxxxxxxxx>:
> > > I would like to integrate a QValidator in a writable column of a
> > > QTreeWidgetItem.
> >
> > You can try deriving from QItemDelegate and overriding the
> > createEditor function. Within the function, call the base class
> > createEditor function. Then, if the editor inherits a QLineEdit
> > (qobject_cast<QLineEdit*>(editor) != NULL), you can attach
> the desired
> > validator to the line editor and return it.
> >
> > Then, simply create an instance of your delegate and set it on the
> > QTreeWidget. Of course, you are then limiting yourself to
> the whole
> > tree, a row, or a column. You should be able to use the
> QModelIndex
> > (passed to createEditor) to decide whether or not to apply the
> > validator or to change it based on the item.
> >
> > Rob
> >
>
>
> --
> Giacomo S.
> http://www.giacomos.it
>
> - - - - - - - - - - - - - - - - - - - - - -
>
> IPFIREwall (http://www.giacomos.it/ipfire) viene presentato
> all'Universita` degli Studi di Udine, il 28 ottobre, in
> occasione del Linux Day 2006:
> http://iglu.cc.uniud.it/linuxday
>
> - - - - - - - - - - - - - - - - - - - - - -
>
> . '' `.
> : :' :
> `. ` '
> `- Debian GNU/Linux -- The power of freedom
> http://www.debian.org
>
--
[ signature omitted ]