Item widgets: clean up treatment of Qt::TextAlignmentRole / CheckStateRole

The item widgets all have an API flaw: getters and setters for the text
alignment deal with int instead of Qt::Alignment.

Deprecate the existing setters and introduce others taking
Qt::Alignment. Store the alignment directly into the item widget (now
that views know how to handle it).

We can't change the getters without cluttering the API, so make that a
Qt 7 change. Users can prepare by forcibly casting the return value to
Qt::Alignment; this is going to work in Qt 6 and 7.

While at it: streamline the handling of Qt::CheckStateRole as well,
avoiding to rely on a pointless Qt::CheckState to int conversion
through QVariant (the setter stores a Qt::CheckState, but the getter
retrieves an int and converts it to a Qt::CheckState).

Task-number: QTBUG-75172
Change-Id: I9f29e818e93cb2dc1d8e042bc320162c2f692112
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
bb10
Giuseppe D'Angelo 2022-03-21 17:28:55 +01:00
parent 5d8f815e10
commit 53ee4c8b1f
6 changed files with 112 additions and 8 deletions

View File

@ -934,11 +934,24 @@ QDataStream &operator>>(QDataStream &in, QListWidgetItem &item)
*/
/*!
\if defined(qt7)
\fn Qt::Alignment QListWidgetItem::textAlignment() const
Returns the text alignment for the list item.
\else
\fn int QListWidgetItem::textAlignment() const
Returns the text alignment for the list item.
\sa Qt::AlignmentFlag
\note This function returns an int for historical reasons. It will
be corrected to return Qt::Alignment in Qt 7.
\sa Qt::Alignment
\endif
*/
/*!
@ -1092,11 +1105,21 @@ void QListWidgetItem::setFlags(Qt::ItemFlags aflags)
*/
/*!
\obsolete [6.4] Use the overload that takes a Qt::Alignment argument.
\fn void QListWidgetItem::setTextAlignment(int alignment)
Sets the list item's text alignment to \a alignment.
\sa Qt::AlignmentFlag
\sa Qt::Alignment
*/
/*!
\since 6.4
\fn void QListWidgetItem::setTextAlignment(Qt::Alignment alignment)
Sets the list item's text alignment to \a alignment.
*/
/*!

View File

@ -109,10 +109,20 @@ public:
{ return qvariant_cast<QFont>(data(Qt::FontRole)); }
inline void setFont(const QFont &font);
#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
inline int textAlignment() const
{ return data(Qt::TextAlignmentRole).toInt(); }
#else
inline Qt::Alignment textAlignment() const
{ return qvariant_cast<Qt::Alignment>(data(Qt::TextAlignmentRole)); }
#endif
#if QT_DEPRECATED_SINCE(6, 4)
QT_DEPRECATED_VERSION_X_6_4("Use the overload taking Qt::Alignment")
inline void setTextAlignment(int alignment)
{ setData(Qt::TextAlignmentRole, alignment); }
#endif
inline void setTextAlignment(Qt::Alignment alignment)
{ setData(Qt::TextAlignmentRole, QVariant::fromValue(alignment)); }
inline QBrush background() const
{ return qvariant_cast<QBrush>(data(Qt::BackgroundRole)); }
@ -125,7 +135,7 @@ public:
{ setData(Qt::ForegroundRole, brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); }
inline Qt::CheckState checkState() const
{ return static_cast<Qt::CheckState>(data(Qt::CheckStateRole).toInt()); }
{ return qvariant_cast<Qt::CheckState>(data(Qt::CheckStateRole)); }
inline void setCheckState(Qt::CheckState state)
{ setData(Qt::CheckStateRole, static_cast<int>(state)); }

View File

@ -1289,14 +1289,29 @@ void QTableWidgetItem::setFlags(Qt::ItemFlags aflags)
*/
/*!
\if defined(qt7)
\fn Qt::Alignment QTableWidgetItem::textAlignment() const
Returns the text alignment for the list item.
\else
\fn int QTableWidgetItem::textAlignment() const
Returns the text alignment for the item's text.
\note This function returns an int for historical reasons. It will
be corrected to return Qt::Alignment in Qt 7.
\sa Qt::Alignment
\endif
*/
/*!
\obsolete [6.4] Use the overload that takes a Qt::Alignment argument.
\fn void QTableWidgetItem::setTextAlignment(int alignment)
Sets the text alignment for the item's text to the \a alignment
@ -1305,6 +1320,15 @@ void QTableWidgetItem::setFlags(Qt::ItemFlags aflags)
\sa Qt::Alignment
*/
/*!
\since 6.4
\fn void QTableWidgetItem::setTextAlignment(Qt::Alignment alignment)
Sets the text alignment for the item's text to the \a alignment
specified.
*/
/*!
Constructs a table item of the specified \a type that does not belong
to any table.

View File

@ -133,10 +133,20 @@ public:
{ return qvariant_cast<QFont>(data(Qt::FontRole)); }
inline void setFont(const QFont &font);
#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
inline int textAlignment() const
{ return data(Qt::TextAlignmentRole).toInt(); }
#else
inline Qt::Alignment textAlignment() const
{ return qvariant_cast<Qt::Alignment>(data(Qt::TextAlignmentRole)); }
#endif
#if QT_DEPRECATED_SINCE(6, 4)
QT_DEPRECATED_VERSION_X_6_4("Use the overload taking Qt::Alignment")
inline void setTextAlignment(int alignment)
{ setData(Qt::TextAlignmentRole, alignment); }
#endif
inline void setTextAlignment(Qt::Alignment alignment)
{ setData(Qt::TextAlignmentRole, QVariant::fromValue(alignment)); }
inline QBrush background() const
{ return qvariant_cast<QBrush>(data(Qt::BackgroundRole)); }
@ -149,7 +159,7 @@ public:
{ setData(Qt::ForegroundRole, brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); }
inline Qt::CheckState checkState() const
{ return static_cast<Qt::CheckState>(data(Qt::CheckStateRole).toInt()); }
{ return qvariant_cast<Qt::CheckState>(data(Qt::CheckStateRole)); }
inline void setCheckState(Qt::CheckState state)
{ setData(Qt::CheckStateRole, state); }

View File

@ -1362,17 +1362,44 @@ bool QTreeWidgetItem::isFirstColumnSpanned() const
*/
/*!
\if defined(qt7)
\fn Qt::Alignment QTreeWidgetItem::textAlignment(int column) const
Returns the text alignment for the label in the given \a column.
\else
\fn int QTreeWidgetItem::textAlignment(int column) const
Returns the text alignment for the label in the given \a column
(see \l{Qt::AlignmentFlag}).
Returns the text alignment for the label in the given \a column.
\note This function returns an int for historical reasons. It will
be corrected to return Qt::Alignment in Qt 7.
\sa Qt::Alignment
\endif
*/
/*!
\obsolete [6.4] Use the overload that takes a Qt::Alignment argument.
\fn void QTreeWidgetItem::setTextAlignment(int column, int alignment)
Sets the text alignment for the label in the given \a column to
the \a alignment specified (see \l{Qt::AlignmentFlag}).
the \a alignment specified.
\sa Qt::Alignment
*/
/*!
\since 6.4
\fn void QTreeWidgetItem::setTextAlignment(int column, Qt::Alignment alignment)
Sets the text alignment for the label in the given \a column to
the \a alignment specified.
*/
/*!

View File

@ -129,10 +129,20 @@ public:
{ return qvariant_cast<QFont>(data(column, Qt::FontRole)); }
inline void setFont(int column, const QFont &font);
#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
inline int textAlignment(int column) const
{ return data(column, Qt::TextAlignmentRole).toInt(); }
#else
inline Qt::Alignment textAlignment(int column) const
{ return qvariant_cast<Qt::Alignment>(data(column, Qt::TextAlignmentRole)); }
#endif
#if QT_DEPRECATED_SINCE(6, 4)
QT_DEPRECATED_VERSION_X_6_4("Use the overload taking Qt::Alignment")
inline void setTextAlignment(int column, int alignment)
{ setData(column, Qt::TextAlignmentRole, alignment); }
#endif
inline void setTextAlignment(int column, Qt::Alignment alignment)
{ setData(column, Qt::TextAlignmentRole, QVariant::fromValue(alignment)); }
inline QBrush background(int column) const
{ return qvariant_cast<QBrush>(data(column, Qt::BackgroundRole)); }
@ -145,7 +155,7 @@ public:
{ setData(column, Qt::ForegroundRole, brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); }
inline Qt::CheckState checkState(int column) const
{ return static_cast<Qt::CheckState>(data(column, Qt::CheckStateRole).toInt()); }
{ return qvariant_cast<Qt::CheckState>(data(column, Qt::CheckStateRole)); }
inline void setCheckState(int column, Qt::CheckState state)
{ setData(column, Qt::CheckStateRole, state); }