Skip to content

Commit 2c6d449

Browse files
committed
Fix: Add elision and tooltip for long placeholder text in DLineEditEx
When the font size is large or the placeholder text is too long, the text would previously wrap or display abnormally. This change ensures that: 1. The text is forced to be single line. 2. The text is elided with '...' at the right if it exceeds the widget width. 3. A tooltip with the full text is shown when the text is elided. Log: 修复当字体较大时 placeholder 文本显示异常的问题,增加省略号和 ToolTip 提示。 Pms: BUG-322639
1 parent 9da746c commit 2c6d449

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

src/widgets/dlineeditex.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,19 @@ void DLineEditEx::paintEvent(QPaintEvent *event)
146146
pa.setPen(col);
147147
QTextOption option;
148148
option.setAlignment(Qt::AlignCenter);
149-
pa.drawText(rect(), lineEdit()->placeholderText(), option);
149+
option.setWrapMode(QTextOption::NoWrap);
150+
// 使用 elidedText 确保文本过长时在右侧显示省略号,而不是换行
151+
QFontMetrics fm(pa.font());
152+
const QString &placeholderText = lineEdit()->placeholderText();
153+
QString elidedText = fm.elidedText(placeholderText, Qt::ElideRight, rect().width());
154+
pa.drawText(rect(), Qt::AlignCenter | Qt::TextSingleLine, elidedText);
155+
156+
// 当文本被省略时,设置 tooltip 显示完整文本
157+
if (elidedText != placeholderText) {
158+
setToolTip(placeholderText);
159+
} else {
160+
setToolTip(QString());
161+
}
150162
}
151163
QWidget::paintEvent(event);
152164
}

0 commit comments

Comments
 (0)