Когда я устанавливаю текст-заполнитель с помощью QLineEdit::setPlaceholderText()
, он становится серым.
Есть ли способ изменить цвет на что-то другое, например красный?
Когда я устанавливаю текст-заполнитель с помощью QLineEdit::setPlaceholderText()
, он становится серым.
Есть ли способ изменить цвет на что-то другое, например красный?
Вы не можете, по крайней мере, с текущим кодом 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
(например, как подсказка по стилю).
Вам нужно создать подкласс 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;
};
Есть еще один немного хакерский, но простой и надежный способ.
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
Если вы хотите использовать QSS вместо QPalette, попробуйте следующее:
setStyleSheet("QLineEdit{"
" color: red;" //TEXT COLOR
"}"
"QLineEdit[text=\"\"]{"
" color: gray;" //TEXTHOLDER COLOR
"}");
connect(ui->lineEdit, &QLineEdit::textChanged, [=]{ style()->polish(ui->lineEdit); });
Вы можете изменить цвет, но не забывайте, что в заполнителе из исходного кода установлен альфа-фактор (как упоминалось в другом комментарии), который нельзя удалить. Поэтому вы всегда будете видеть заполнитель более темным (с этой опцией белый цвет невозможен).
Если вы хотите изменить цвет текста-заполнителя для 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
.
QPalette::PlaceholderText
, который работает для этой цели и позволяет избежать проблемы непрозрачности 0,5.
- person Alex Kritchevsky; 24.07.2019
QPalette::PlaceholderText
у меня работает.
- person DRPK; 22.01.2021
Решение @Meefte неплохое, учитывая ситуацию, когда Qt придает заполнителю тот же цвет, что и для текста, за исключением того, что он добавляет 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;
};
QLineEdit
. По сути, он просто берет цвет текста и уменьшает альфа-канал. - person thuga   schedule 01.12.2014