Some questions regarding the star delegate example.
-
Hi,
as I'd like to accomplish something similar but with some simple lines instead of diamonds/stars, I studied the star delegate example, but still have some questions.
Is it correct that Q_DECLARE_METATYPE(StarRating) enables StarRating class instances to be displayed in a table cell, i.e., using the model/view approach it would be used by the data method?
The constructor of the StarRating class confuses me a little:
StarRating::StarRating(int starCount, int maxStarCount) : myStarCount(starCount), myMaxStarCount(maxStarCount) {....}
Do myStarCount and myMaxStarCount - so to say - receive the values of starCount and maxStarCount or how should I read the syntax?
Last but not least, at the end of the description there's the hint that the same would be achievable with QAbstractItemDelegate::editorEvent(), i.e., without having to write a separate subclass of QWidget. Would this more complicated or easier than creating a subclass?
Kind regards,
Andreas
PS: I am referring to the current documentation: Star delegate example
-
Hi,
-
No, Q_DECLARE_METATYPE has nothing to do with displaying something in a cell. It's part of the mechanism that allows to use custom types in for example signals and slots.
-
Yes, the values are passed to the member variables in the initialisation list.
-
From the top of my head, it shouldn't be that much more complicated.
-
-
To extend @SGaist 's answers a lil bit:
Is it correct that Q_DECLARE_METATYPE(StarRating) enables StarRating class instances to be displayed in a table cell, i.e., using the model/view approach it would be used by the data method?
This is not the purpose of
Q_DECLARE_METATYPE
but in this case it comes in handy to use it like this. It's even noted in the example documentation itself:The Q_DECLARE_METATYPE() macro makes the type StarRating known to QVariant, making it possible to store StarRating values in QVariant.
(taken from here)
So it's possible to do this:
QTableWidgetItem *item3 = new QTableWidgetItem; item3->setData(0, QVariant::fromValue(StarRating(staticData[row].rating)));
Notice the call to QVariant::fromValue to convert a StarRating to a QVariant.
because
QTableWidgetItem::setData
is defined as:void QTableWidgetItem::setData(int role, const QVariant &value)
Do myStarCount and myMaxStarCount - so to say - receive the values of starCount and maxStarCount or how should I read the syntax?
This is basic C++. It's the fast and short form of
StarRating::StarRating(int starCount, int maxStarCount) { myStarCount = starCount; myMaxStarCount = maxStarCount; }
Would this more complicated or easier than creating a subclass?
It's less code and on the same level of complexity, I would say.
Basically everything you do in yourStarEditor
goes in theeditorEvent
function.
There you tell your Item View in which way you want to edit the data of typeStarRating
(treated asQVariant
) represented byStarDelegate
. -