File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments