Skip to content

Commit

Permalink
fix: 修复dslider刻度标签内容过长时,显示不下的问题
Browse files Browse the repository at this point in the history
内容过长时,取省略号替代

Log:
Task: https://pms.uniontech.com/task-view-325247.html
Influence: DSlider刻度标签显示
Change-Id: I47993df919b5fcc238db42b609b6183d5011e14a
(cherry picked from commit 0b89d4cbcc9817b7829dfc5d196284f5c8de9945)
  • Loading branch information
Mars-cb authored and kegechen committed Feb 28, 2024
1 parent cd1f2fd commit c0f0268
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/widgets/dslider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,10 @@ void SliderStrip::paintEvent(QPaintEvent *event)
if (scaleInfo.isEmpty())
return;

auto elidedText = [this](const QString &text, int width) {
return fontMetrics().elidedText(text, Qt::ElideRight, width);
};

if (orient == Qt::Horizontal) {
width -= 2 * offsetSize + margin * 2;
startX += offsetSize + margin;
Expand All @@ -837,15 +841,15 @@ void SliderStrip::paintEvent(QPaintEvent *event)
pa.setPen(penLine);
pa.drawLine(QPointF(startX, startY), QPointF(endX, endY));
pa.setPen(penNumber);
pa.drawText(QRectF(endX, textPos, width, height - tickSize), Qt::AlignLeft, scaleInfo[0]);
pa.drawText(QRectF(endX, textPos, width, height - tickSize), Qt::AlignLeft, elidedText(scaleInfo[0], width / paragraph));

for (int i = 1; i < paragraph - 1; i++) {
startX += average;
endX = startX;
pa.setPen(penLine);
pa.drawLine(QPointF(startX, startY), QPointF(endX, endY));
pa.setPen(penNumber);
pa.drawText(QRectF(endX - width / 2, textPos, width, height - tickSize), Qt::AlignHCenter, scaleInfo[i]);
pa.drawText(QRectF(endX - width / 2, textPos, width, height - tickSize), Qt::AlignHCenter, elidedText(scaleInfo[i], width / paragraph));
}

if (paragraph > 1) {
Expand All @@ -854,7 +858,7 @@ void SliderStrip::paintEvent(QPaintEvent *event)
pa.setPen(penLine);
pa.drawLine(QPointF(startX, startY), QPointF(endX, endY));
pa.setPen(penNumber);
pa.drawText(QRectF(endX - width, textPos, width, height - tickSize), Qt::AlignRight, scaleInfo[paragraph - 1]);
pa.drawText(QRectF(endX - width, textPos, width, height - tickSize), Qt::AlignRight, elidedText(scaleInfo[paragraph - 1], width / paragraph));
}
} else {
startY = offsetSize + margin;
Expand All @@ -877,15 +881,15 @@ void SliderStrip::paintEvent(QPaintEvent *event)
pa.setPen(penLine);
pa.drawLine(QPointF(startX, startY), QPointF(endX, endY));
pa.setPen(penNumber);
pa.drawText(QRectF(textPos, endY - average / 2 + offsetSize / 2, width - tickSize, average), text_flags, scaleInfo[0]);
pa.drawText(QRectF(textPos, endY - average / 2 + offsetSize / 2, width - tickSize, average), text_flags, elidedText(scaleInfo[0], width - tickSize));

for (int i = 1; i < paragraph - 1; i++) {
startY += average;
endY = startY;
pa.setPen(penLine);
pa.drawLine(QPointF(startX, startY), QPointF(endX, endY));
pa.setPen(penNumber);
pa.drawText(QRectF(textPos, endY - average / 2, width - tickSize, average), text_flags, scaleInfo[i]);
pa.drawText(QRectF(textPos, endY - average / 2, width - tickSize, average), text_flags, elidedText(scaleInfo[i], width - tickSize));
}

if (paragraph > 1) {
Expand All @@ -894,7 +898,7 @@ void SliderStrip::paintEvent(QPaintEvent *event)
pa.setPen(penLine);
pa.drawLine(QPointF(startX, startY), QPointF(endX, endY));
pa.setPen(penNumber);
pa.drawText(QRectF(textPos, endY - average / 2 - offsetSize / 2, width - tickSize, average), text_flags, scaleInfo[paragraph - 1]);
pa.drawText(QRectF(textPos, endY - average / 2 - offsetSize / 2, width - tickSize, average), text_flags, elidedText(scaleInfo[paragraph - 1], width - tickSize));
}
}
}
Expand Down

0 comments on commit c0f0268

Please sign in to comment.