PDA

View Full Version : what is type?



gfunk
10th August 2006, 21:51
What is the type used for in QListWidgetItem, QTreeWidgetItem, QTableWidgetItem? I see that it is used as an argument in all their constructors, with an option for UserType's, but don't know what this does or what is useful for. Seems to have something to do with rtti?



QListWidgetItem(QListWidget *view = 0, int type = Type);

Chicken Blood Machine
10th August 2006, 22:06
Yes, I think it's used for rtti. You can use this if your compiler does not support dynamic_cast or you don't want the overhead of rtti in the build*. You define a unique integer id for each subclass of QListWidgetItem that you derive, then you can use this id for comparisons when you retrieve an item in order to derive what type to cast it to.

You can also use it to tell different items apart even when you are not using subclassing.

* This may be important on embedded systems.

3dch
12th August 2006, 15:45
The Qt docs are not very elaborate about the type provided in the XXItem constructors.
I'd say that the type identifies the data type contained in the item which gets interpreted by the related XXView classes for operations such as sorting or filtering. Since the View-Classes work mostly with QVariant types the controls need a way to figure out what
type the data is it they have to display and process.

Here an excerpt from the Qt doc for QVariant:



QVariant::QVariant ( Type type )
Constructs a null variant of type type.


where type is:



enum QVariant::Type
This enum type defines the types of variable that a QVariant can contain.

Constant Value Description
QVariant::Invalid 0 no type
QVariant::BitArray 13 a QBitArray
QVariant::Bitmap 73 a QBitmap
QVariant::Bool 1 a bool

...


Regards
Ernst

jacek
12th August 2006, 16:23
I'd say that the type identifies the data type contained in the item which gets interpreted by the related XXView classes for operations such as sorting or filtering.
Unfortunately it isn't true. If it had something to do with QVariant::Type, the Trolls would use QVariant::Type instead of defining new types. Note also that QVariant object knows exactly what is the type of the value it holds and there is no need to duplicate that information. Furthermore xxxView classes use models, not xxxItems.


class Q_GUI_EXPORT QListWidgetItem
{
friend class QListModel;
public:
enum { Type = 0, UserType = 1000 }; // <- note that there are only two values defined
...

As Chcken Blood Machine said, it's used for RTTI:

QListWidgetItem::QListWidgetItem(QListWidget *view, int type)
: rtti(type), ... // <--
...

3dch
12th August 2006, 17:40
You're absolutely right. Mea culpa!