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

问题描述 投票:3回答:6

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

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

c++ qt qlineedit
6个回答
2
投票

你不能,至少使用当前的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);
    }
}

但是,您可以将上游工作转换为更通用的解决方案。特别是我可以期望将颜色添加到调色板中,或者通常由当前的QStyle提供(例如作为style hint)。


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;
};

4
投票

还有一个有点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
    }
}

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


1
投票

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

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

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


1
投票

如果要更改QLineEdit的占位符文本颜色,则必须自定义组件的QPalette对象。

QPalette p = lineEdit->palette();
p.setColor(QPalette::Mid, Qt::red); // assuming Mid is the color you want to change.
lineEdit->setPalette(p);

我不记得究竟哪个QPalette::ColorRole适合改变QLineEdit的占位符文本颜色。


0
投票

考虑到Qt给占位符颜色与文本相同的情况,@ Meefte解决方案相当不错,除了它增加了50%的不透明度。因此,几乎没有选择将占位符颜色设置为与文本不同。但是,即使是这个解决方案也可以通过确保您不需要设置其他变量而不是Qt为您提供的默认变量来改进。

使用默认placeholderText()的需要可能来自于你有许多QLineEdit控件已被提升为某些控制覆盖QLineEdit行为的情况,并且placeholderText()已经通过代码或通过Qt Creator设置,即介绍会有点痛苦另一个动态属性。但是,如果你没有促进某些儿童控制,那么为了使用这种解决方案,这样做是必要的。

class CustomColorPlaceholderLineEdit : public QLineEdit
{
public:
    CustomColorPlaceholderLineEdit(QWidget * parent = 0) : QLineEdit(parent) { color = QColor(0,0,0,128); }

    const QString &customPlaceholderText() const { return mText; }

    void setCustomPlaceholderColor(const QColor &color) { this->color = color; }

    const QColor &customPlaceholderColor() const { return color; }

    void paintEvent(QPaintEvent *event)
    {
        if(color.isValid() && text().isEmpty() && (!placeholderText().isEmpty() || !mText.isEmpty()))
        {
            if(!placeholderText().isEmpty())
            {
                // In this way, placeholderText() is taken into local variable 'mText' care. Whenever placeholderText() will change, there it will be taken care of.
                mText = placeholderText();

                // This will ensure Qt will not draw placeholder for us.
                setPlaceholderText("");
            }

            // By this, we make sure Qt will paint QLineEdit default parts properly.
            QLineEdit::paintEvent(e);

            // And now @Meefte code is reused here.
            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);
            return; // No need to paint again.
        }

        // Default Qt's painting behavior for QLineEdit.
        QLineEdit::paintEvent(e);
    }
private:
    QString mText;
    QColor color;
};
© www.soinside.com 2019 - 2024. All rights reserved.