gpt4 book ai didi

c++ - QLineEdit 中的自定义占位符

转载 作者:太空宇宙 更新时间:2023-11-03 10:28:14 25 4
gpt4 key购买 nike

我想要一个具有特定占位符文本格式的 QLineEdit:它需要左对齐和右对齐文本。这是一个例子:

example

有什么想法吗?

最佳答案

不幸的是,this seems to be all hard codedvoid QLineEdit::paintEvent(QPaintEvent *)中如下:

if (d->shouldShowPlaceholderText()) {
if (!d->placeholderText.isEmpty()) {
QColor col = pal.text().color();
col.setAlpha(128);
QPen oldpen = p.pen();
p.setPen(col);
QRect ph = lineRect.adjusted(minLB, 0, 0, 0);
QString elidedText = fm.elidedText(d->placeholderText, Qt::ElideRight, ph.width());
p.drawText(ph, va, elidedText);
p.setPen(oldpen);
}
}

如果您愿意,您可以在子类中自行重新实现它。

当然,您也可以“欺骗”空格和字体大小,但这需要更多的工作,而且最终也会更糟糕,更不用说长期可靠性了。

您也可以为 Qt 项目做出贡献,使此类更加灵活,但他们可能会以不够常见为由拒绝它。这取决于维护者。

关于c++ - QLineEdit 中的自定义占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26504626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com