From 4c6579eacded086ad014b0fd2432095362c1d131 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Fri, 19 Feb 2021 08:14:31 +0100 Subject: [PATCH] Unbreak binary compatibility in QStyleOptionHeader 4d943846128118e1b9932a17ce6f977a0f4127a5 introduced a new data member to QStyleOptionHeader, and reduced the size of the orientation member. This changed the binary layout of class instances, and breaks ABI. 180c662b0790c6eceffdcb4661681d7df1541a2d added another member within the new bitfield. Introduce a new QStyleOptionHeaderV2 class instead with the new members, and use that in QHeaderView, and the styles using the new members. Fixes: QTBUG-91224 Pick-to: 6.1 Change-Id: I47e6841e6652e4b67f247b7b4514e90be5609156 Reviewed-by: David Faure --- src/plugins/styles/mac/qmacstyle_mac.mm | 6 ++++-- src/widgets/itemviews/qheaderview.cpp | 11 +++++++---- src/widgets/styles/qcommonstyle.cpp | 10 +++++----- src/widgets/styles/qfusionstyle.cpp | 6 ++++-- src/widgets/styles/qstyleoption.cpp | 26 +++++++++++++++++++++++-- src/widgets/styles/qstyleoption.h | 23 ++++++++++++++++++---- 6 files changed, 63 insertions(+), 19 deletions(-) diff --git a/src/plugins/styles/mac/qmacstyle_mac.mm b/src/plugins/styles/mac/qmacstyle_mac.mm index 7b3709459d..dad81dc8a1 100644 --- a/src/plugins/styles/mac/qmacstyle_mac.mm +++ b/src/plugins/styles/mac/qmacstyle_mac.mm @@ -3565,8 +3565,10 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter textr.translate(pixmap.width() / pixmap.devicePixelRatio() + 2, 0); } QString text = header->text; - if (header->textElideMode != Qt::ElideNone) - text = header->fontMetrics.elidedText(text, header->textElideMode, textr.width()); + if (const QStyleOptionHeaderV2 *headerV2 = qstyleoption_cast(header)) { + if (headerV2->textElideMode != Qt::ElideNone) + text = header->fontMetrics.elidedText(text, headerV2->textElideMode, textr.width()); + } proxy()->drawItemText(p, textr, header->textAlignment | Qt::AlignVCenter, header->palette, header->state.testFlag(State_Enabled), text, QPalette::ButtonText); diff --git a/src/widgets/itemviews/qheaderview.cpp b/src/widgets/itemviews/qheaderview.cpp index cf74db49fb..27866ecbb0 100644 --- a/src/widgets/itemviews/qheaderview.cpp +++ b/src/widgets/itemviews/qheaderview.cpp @@ -2910,6 +2910,7 @@ void QHeaderView::initStyleOptionForIndex(QStyleOptionHeader *option, int logica if (!option) return; QStyleOptionHeader &opt = *option; + QStyleOptionHeaderV2 *optV2 = qstyleoption_cast(option); QStyle::State state = QStyle::State_None; if (window()->isActiveWindow()) @@ -2953,7 +2954,8 @@ void QHeaderView::initStyleOptionForIndex(QStyleOptionHeader *option, int logica Qt::FontRole); if (var.isValid() && var.canConvert()) opt.fontMetrics = QFontMetrics(qvariant_cast(var)); - opt.textElideMode = d->textElideMode; + if (optV2) + optV2->textElideMode = d->textElideMode; QVariant foregroundBrush = d->model->headerData(logicalIndex, d->orientation, Qt::ForegroundRole); @@ -2992,7 +2994,8 @@ void QHeaderView::initStyleOptionForIndex(QStyleOptionHeader *option, int logica opt.selectedPosition = QStyleOptionHeader::NextIsSelected; else opt.selectedPosition = QStyleOptionHeader::NotAdjacent; - opt.isSectionDragTarget = d->target == logicalIndex; + if (optV2) + optV2->isSectionDragTarget = d->target == logicalIndex; } /*! @@ -3007,7 +3010,7 @@ void QHeaderView::paintSection(QPainter *painter, const QRect &rect, int logical if (!rect.isValid()) return; - QStyleOptionHeader opt; + QStyleOptionHeaderV2 opt; QPointF oldBO = painter->brushOrigin(); initStyleOption(&opt); @@ -3052,7 +3055,7 @@ QSize QHeaderView::sectionSizeFromContents(int logicalIndex) const return qvariant_cast(variant); // otherwise use the contents - QStyleOptionHeader opt; + QStyleOptionHeaderV2 opt; initStyleOption(&opt); opt.section = logicalIndex; QVariant var = d->model->headerData(logicalIndex, d->orientation, diff --git a/src/widgets/styles/qcommonstyle.cpp b/src/widgets/styles/qcommonstyle.cpp index ff115596bf..a2f52562c8 100644 --- a/src/widgets/styles/qcommonstyle.cpp +++ b/src/widgets/styles/qcommonstyle.cpp @@ -1682,11 +1682,11 @@ void QCommonStyle::drawControl(ControlElement element, const QStyleOption *opt, p->setFont(fnt); fm = QFontMetrics((p->font())); } - QString text; - if (header->textElideMode != Qt::ElideNone) - text = fm.elidedText(header->text, header->textElideMode, rect.width()); - else - text = header->text; + QString text = header->text; + if (const QStyleOptionHeaderV2 *headerV2 = qstyleoption_cast(header)) { + if (headerV2->textElideMode != Qt::ElideNone) + text = fm.elidedText(header->text, headerV2->textElideMode, rect.width()); + } proxy()->drawItemText(p, rect, header->textAlignment, header->palette, header->state.testFlag(State_Enabled), text, QPalette::ButtonText); } diff --git a/src/widgets/styles/qfusionstyle.cpp b/src/widgets/styles/qfusionstyle.cpp index d2eb5e25cc..5772c7d4c4 100644 --- a/src/widgets/styles/qfusionstyle.cpp +++ b/src/widgets/styles/qfusionstyle.cpp @@ -1280,10 +1280,12 @@ void QFusionStyle::drawControl(ControlElement element, const QStyleOption *optio painter->save(); // Draws the header in tables. if (const QStyleOptionHeader *header = qstyleoption_cast(option)) { + const QStyleOptionHeaderV2 *headerV2 = qstyleoption_cast(option); QString pixmapName = QStyleHelper::uniqueName(QLatin1String("headersection"), option, option->rect.size()); pixmapName += QString::number(- int(header->position)); pixmapName += QString::number(- int(header->orientation)); - pixmapName += QString::number(- int(header->isSectionDragTarget)); + if (headerV2) + pixmapName += QString::number(- int(headerV2->isSectionDragTarget)); QPixmap cache; if (!QPixmapCache::find(pixmapName, &cache)) { @@ -1294,7 +1296,7 @@ void QFusionStyle::drawControl(ControlElement element, const QStyleOption *optio QColor buttonColor = d->buttonColor(option->palette); QColor gradientStartColor = buttonColor.lighter(104); QColor gradientStopColor = buttonColor.darker(102); - if (header->isSectionDragTarget) { + if (headerV2 && headerV2->isSectionDragTarget) { gradientStopColor = gradientStartColor.darker(130); gradientStartColor = gradientStartColor.darker(130); } diff --git a/src/widgets/styles/qstyleoption.cpp b/src/widgets/styles/qstyleoption.cpp index 933603dd7e..33071b0724 100644 --- a/src/widgets/styles/qstyleoption.cpp +++ b/src/widgets/styles/qstyleoption.cpp @@ -748,10 +748,32 @@ QStyleOptionHeader::QStyleOptionHeader(int version) section(0), textAlignment(Qt::AlignLeft), iconAlignment(Qt::AlignLeft), position(QStyleOptionHeader::Beginning), selectedPosition(QStyleOptionHeader::NotAdjacent), sortIndicator(None), - orientation(Qt::Horizontal), textElideMode(Qt::ElideNone), isSectionDragTarget(false), unused(0) + orientation(Qt::Horizontal) { } +/*! + \class QStyleOptionHeaderV2 + \brief The QStyleOptionHeaderV2 class is used to describe the + parameters for drawing a header. +*/ + +/*! + Constructs a QStyleOptionHeaderV2, initializing the members + variables to their default values. +*/ +QStyleOptionHeaderV2::QStyleOptionHeaderV2() + : QStyleOptionHeaderV2(QStyleOptionHeader::Version) +{ +} + +/*! + \internal +*/ +QStyleOptionHeaderV2::QStyleOptionHeaderV2(int version) +: QStyleOptionHeader(version), textElideMode(Qt::ElideNone), isSectionDragTarget(false), unused(0) +{} + /*! \variable QStyleOptionHeader::orientation \brief the header's orientation (horizontal or vertical) @@ -892,7 +914,7 @@ QStyleOptionHeader::QStyleOptionHeader(int version) */ /*! - \variable QStyleOptionHeader::textElideMode + \variable QStyleOptionHeaderV2::textElideMode \brief where ellipsis should be added for text that is too long to fit into an item diff --git a/src/widgets/styles/qstyleoption.h b/src/widgets/styles/qstyleoption.h index 677a04c04b..0f41211521 100644 --- a/src/widgets/styles/qstyleoption.h +++ b/src/widgets/styles/qstyleoption.h @@ -219,10 +219,7 @@ public: SectionPosition position; SelectedPosition selectedPosition; SortIndicator sortIndicator; - Qt::Orientation orientation:2; - Qt::TextElideMode textElideMode:2; - bool isSectionDragTarget:1; - int unused:27; + Qt::Orientation orientation; QStyleOptionHeader(); QStyleOptionHeader(const QStyleOptionHeader &other) : QStyleOption(Version, Type) { *this = other; } @@ -232,6 +229,24 @@ protected: QStyleOptionHeader(int version); }; +class Q_WIDGETS_EXPORT QStyleOptionHeaderV2 : public QStyleOptionHeader +{ +public: + enum StyleOptionType { Type = SO_Header }; + enum StyleOptionVersion { Version = 2 }; + + QStyleOptionHeaderV2(); + QStyleOptionHeaderV2(const QStyleOptionHeaderV2 &other) : QStyleOptionHeader(Version) { *this = other; } + QStyleOptionHeaderV2 &operator=(const QStyleOptionHeaderV2 &) = default; + + Qt::TextElideMode textElideMode:2; + bool isSectionDragTarget:1; + int unused:29; + +protected: + QStyleOptionHeaderV2(int version); +}; + class Q_WIDGETS_EXPORT QStyleOptionButton : public QStyleOption { public: