[Qt-interest] QTreeWidget->setItemWidget() works contrary to Qt documentation
J-P Nurmi
jpnurmi at gmail.com
Sun Oct 9 14:32:53 CEST 2011
On Sat, Oct 8, 2011 at 3:16 PM, Roland Tollenaar <rwatollenaar at gmail.com> wrote:
> Hi,
>
> The documentation of Qt4.5.2 on QTreeWidgetItem::setItemWidget(..) reads:
>
> "Sets the given widget to be displayed in the cell specified by the
> given item and column.
>
> This function should only be used to display static content in the place
> of a tree widget item. If you want to display custom dynamic content or
> implement a custom editor widget, use QTreeView and subclass
> QItemDelegate instead."
>
> We have been adding Qlineedits and QtextEdits for editing to
> QtreeWidgetItems for years without problems.
Even if something "works", it doesn't necessarily mean that it's a
reasonable or good way of doing things, right? I would go as far as
saying that using item widgets as editors is wrong. What's the point
to have an item view full of editor instances whilst only one can be
active/used at a time? Use editable items instead. The model/view
framework supports editable items out of the box, QLineEdit being the
default editor for textual data.
As you might have noticed, item widgets have no connection to the
underlying model. They are merely something glued on top of the
corresponding view items. In fact, item views end up doing a whole lot
of extra geometry calculation to be able to keep item widgets in
correct places. Qt's model/view framework is based on the idea of
representing model contents in a single widget (aka view) using
delegates to render each model item, and overusing heavyish item
widgets effectively loses the benefits of the model/view framework.
> Spotting this now there is a concern that future versions of Qt (we
> might have to upgrade at some point) will break our code.
>
> Can anyone comment on this?
As explained above, there is a reason why the documentation has been
made more clear about this. No matter Qt version, it's not a good way
to use item widgets.
--
J-P Nurmi
More information about the Qt-interest-old
mailing list