summaryrefslogtreecommitdiffstats
path: root/src/widgets/itemviews/qitemdelegate.cpp
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2022-03-21 14:13:26 +0100
committerGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2022-03-23 23:06:23 +0100
commit6880b7c39b2c7474be1c9adcabbe0b7d6596822f (patch)
tree2ad5298062452dfdd3d87a07355ea003e46edddf /src/widgets/itemviews/qitemdelegate.cpp
parentaf875e88f46a2947c313bd724ab9b10f3e7268df (diff)
Itemviews: start fixing mixups of int/enum for Qt's item roles
A model is supposed to return a Qt::CheckState for a CheckStateRole, and a Qt::Alignment for a Qt::TextAlignmentRole. This is what the documentation says (and what makes sense), but unfortunately Qt's default delegate expected a plain `int` instead. This sometimes worked (via QVariant conversions, e.g. when using a plain enum) and sometimes didn't (e.g. when using a flag type). This is confusing for end-users (and type unsafe, killing the whole point of using enums and flags in the first place). Adding some automatic flags<->int conversions through QVariant is frowned upon, so I don't want to go there. Instead, add some private convenience functions that extract either the right type from a variant, or try to extract an `int` and convert it to the expected type. Use these from within itemviews code. Change-Id: I44bee98c4a26a1ef6c3b2fa1b8de2edfee7aef32 Pick-to: 6.2 6.3 Fixes: QTBUG-75172 Task-number: QTBUG-74639 Reviewed-by: David Faure <david.faure@kdab.com> Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io> Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/widgets/itemviews/qitemdelegate.cpp')
-rw-r--r--src/widgets/itemviews/qitemdelegate.cpp7
1 files changed, 4 insertions, 3 deletions
diff --git a/src/widgets/itemviews/qitemdelegate.cpp b/src/widgets/itemviews/qitemdelegate.cpp
index 2640d7163d..e481dcae72 100644
--- a/src/widgets/itemviews/qitemdelegate.cpp
+++ b/src/widgets/itemviews/qitemdelegate.cpp
@@ -58,6 +58,7 @@
#include <qmetaobject.h>
#include <qtextlayout.h>
#include <private/qabstractitemdelegate_p.h>
+#include <private/qabstractitemmodel_p.h>
#include <private/qtextengine_p.h>
#include <qdebug.h>
#include <qlocale.h>
@@ -432,7 +433,7 @@ void QItemDelegate::paint(QPainter *painter,
Qt::CheckState checkState = Qt::Unchecked;
value = index.data(Qt::CheckStateRole);
if (value.isValid()) {
- checkState = static_cast<Qt::CheckState>(value.toInt());
+ checkState = QtPrivate::legacyEnumValueFromModelData<Qt::CheckState>(value);
checkRect = doCheck(opt, opt.rect, value);
}
@@ -1182,7 +1183,7 @@ bool QItemDelegate::editorEvent(QEvent *event,
return false;
}
- Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt());
+ Qt::CheckState state = QtPrivate::legacyEnumValueFromModelData<Qt::CheckState>(value);
if (flags & Qt::ItemIsUserTristate)
state = ((Qt::CheckState)((state + 1) % 3));
else
@@ -1209,7 +1210,7 @@ QStyleOptionViewItem QItemDelegate::setOptions(const QModelIndex &index,
// set text alignment
value = index.data(Qt::TextAlignmentRole);
if (value.isValid())
- opt.displayAlignment = Qt::Alignment(value.toInt());
+ opt.displayAlignment = QtPrivate::legacyFlagValueFromModelData<Qt::Alignment>(value);
// set foreground brush
value = index.data(Qt::ForegroundRole);