在QLineEdit中更改占位符文本的颜色

Pro*_*ing 3 c++ qt qlineedit

当我使用占位符文本设置时QLineEdit::setPlaceholderText(),它显示为灰色.

在此输入图像描述

有没有办法将颜色更改为其他颜色,例如红色?

Mee*_*fte 6

你必须在子类中QLineEdit绘制自己的占位符paintEvent().

class CustomColorPlaceholderLineEdit : public QLineEdit
{
public:
    CustomColorPlaceholderLineEdit(QWidget * parent = 0) : QLineEdit(parent) { color = QColor(0,0,0,128); }
    void setCustomPlaceholderText(const QString &text) { this->mText = text; }
    const QString &customPlaceholderText() const { return mText; }
    void setCustomPlaceholderColor(const QColor &color) { this->color = color; }
    const QColor &customPlaceholderColor() const { return color; }
    void paintEvent(QPaintEvent *event) {
        QLineEdit::paintEvent(event);
        if (!hasFocus() && text().isEmpty() && !mText.isEmpty()) {
            // QLineEdit's own placeholder clashes with ours.
            Q_ASSERT(placeholderText().isEmpty());
            QPainter p(this);
            p.setPen(color);
            QFontMetrics fm = fontMetrics();
            int minLB = qMax(0, -fm.minLeftBearing());
            QRect lineRect = this->rect();
            QRect ph = lineRect.adjusted(minLB + 3, 0, 0, 0);
            QString elidedText = fm.elidedText(mText, Qt::ElideRight, ph.width());
            p.drawText(ph, Qt::AlignVCenter, elidedText);
        }
    }
private:
    QString mText;
    QColor color;
};
Run Code Online (Sandbox Code Playgroud)


Ver*_*mov 5

还有另一种有点hacky但简单可靠的方法。

connect(lineEdit, &QLineEdit::textChanged, this, &YourClass::updateLineEditStyleSheet);

void YourLineEdit::updateLineEditStyleSheet()
{
    if (lineEdit->text().isEmpty()) {
        lineEdit->setStyleSheet("#lineEdit { color: lightGray;"); // Set your color but remember that Qt will reduce alpha
    } else {
        lineEdit->setStyleSheet("#lineEdit { color: black;"); // usual color
    }
}
Run Code Online (Sandbox Code Playgroud)

您也可以使用这种方式从 QLineEdit 类派生


lau*_*ons 5

如果您想使用 QSS 而不是 QPalette,请尝试以下操作:

setStyleSheet("QLineEdit{"
              "    color: red;" //TEXT COLOR
              "}"
              "QLineEdit[text=\"\"]{"
              "    color: gray;" //TEXTHOLDER COLOR
              "}");
connect(ui->lineEdit, &QLineEdit::textChanged, [=]{ style()->polish(ui->lineEdit); });
Run Code Online (Sandbox Code Playgroud)

您可以更改颜色,但请记住,在源代码中的占位符中设置了一个无法删除的 alpha 因子(如另一条评论中所述)。因此,您总是会看到占位符较暗(此选项不可能出现白色)。


pep*_*ppe 3

至少对于当前的 QLineEdit 代码,您不能这样做。

从源代码中可以看到,占位符文本只是采用调色板的前景画笔并使其部分透明,请参见QLineEdit::paintEvent

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

不过,您可以与上游合作形成更通用的解决方案。特别是,我希望将颜色添加到调色板中,或者通常由当前颜色提供QStyle(例如作为样式提示)。