Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 更改QLineEdit中占位符文本的颜色_C++_Qt_Qlineedit - Fatal编程技术网

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

C++ 更改QLineEdit中占位符文本的颜色,c++,qt,qlineedit,C++,Qt,Qlineedit,当我使用QLineEdit::setPlaceholderText()设置占位符文本时,它显示为灰色 有没有办法将颜色更改为其他颜色,例如红色?如果要更改QLineEdit的占位符文本颜色,必须自定义组件的qpalete对象 QPalette p = lineEdit->palette(); p.setColor(QPalette::Mid, Qt::red); // assuming Mid is the color you want to change. lineEdit->s

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


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

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

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

我记不清哪一个
qpalete::ColorRole
适合更改
QLineEdit
的占位符文本颜色。

至少在当前的QLineEdit代码中,您无法更改

正如您从源代码中看到的,占位符文本只是使用调色板的前景笔刷并使其部分透明,请参见:


不过,您可以使用upstream找到更通用的解决方案。特别是,我希望将该颜色添加到调色板中,或者通常由当前的
QStyle
(例如a)提供。

您必须将
QLineEdit
子类化,并在
paintEvent()
中绘制您自己的占位符


还有一种有点老套但简单可靠的方法

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类派生出文本。考虑到Qt为占位符提供与文本相同的颜色,该解决方案非常好,只是增加了50%的不透明度。因此,几乎没有选择将占位符颜色设置为与文本不同。但是,即使是这个解决方案也可以通过确保不需要设置Qt提供给您的默认变量以外的其他变量来改进

需要使用默认的
Placeholder Text()
可能是因为您有许多
QLineEdit
控件,这些控件已经升级为覆盖
QLineEdit
行为的某个控件,并且已经通过code或Qt Creator设置了
Placeholder Text()
,也就是说,引入另一个动态属性会有点痛苦。但是,如果您没有提升到某个儿童控制,那么为了使用这种解决方案,有必要这样做

类CustomColorPlaceholderLineEdit:公共QLineEdit
{
公众:
CustomColorPlaceholderLineEdit(QWidget*parent=0):QLineEdit(parent){color=QColor(0,0,0128);}
常量QString&customPlaceholderText()常量{return mText;}
void setCustomPlaceholder颜色(constQColor&color){this->color=color;}
常量QColor&customPlaceholderColor()常量{return color;}
无效paintEvent(QPaintEvent*事件)
{
if(color.isValid()&&text().isEmpty()&&(!placeholder text().isEmpty()| |!mText.isEmpty())
{
如果(!placeholder text().isEmpty())
{
//通过这种方式,占位符文本()被纳入局部变量“mText”的考虑范围。每当占位符文本()发生更改时,它都会在那里得到考虑。
多行文字=占位符文字();
//这将确保Qt不会为我们绘制占位符。
设置占位符文本(“”);
}
//这样,我们就可以确保Qt将正确地绘制QLineEdit默认部分。
QLineEdit::paintEvent(e);
//现在@Meefte代码在这里被重用。
油漆工p(本);
p、 设置笔(彩色);
QFontMetrics fm=fontMetrics();
int minLB=qMax(0,-fm.minLeftBearing());
QRect lineRect=此->rect();
QRect ph=线性校正(最小磅+3,0,0,0);
QString elidedText=fm.elidedText(mText,Qt::elidelight,ph.width());
p、 drawText(ph,Qt::AlignVCenter,elidedText);
return;//无需再次绘制。
}
//QLineEdit的默认Qt绘制行为。
QLineEdit::paintEvent(e);
}
私人:
QString多行文字;
四色;
};

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

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

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

您需要实现自己的占位符绘制功能。您可以查看Qt的源代码,了解它是如何工作的。这很简单。您可以在
QLineEdit
的源代码中看到它是如何处理的。基本上,它只是采用文本颜色并减少alpha。我对问题进行了编辑,使其更为一般和易懂。此代码不起作用-我测试了它,文本颜色保持灰色。我已经开始对这个问题进行悬赏。在Qt5.12中,有一个
Qpalete::Placeholder Text
用于此目的,并避免了0.5不透明度问题。@AlexKritchevsky tnx
Qpalete::Placeholder Text
对我有效。我不建议这样做。这是一个非常脆弱的黑客行为,因为你基本上在现有的占位符文本上画得太多了。如果Qt决定微调样式,并将内容移动一个像素左右,该怎么办?如果检查是否应绘制占位符的条件发生更改怎么办?(特别是,此代码中的错误。)@peppe Qt standart占位符文本不存在。在某些条件下,我正在QLineEdit上绘制自定义文本。您可以将条件更改为您想要的任何颜色。@peppe我有一个主意-您可以在onFocus()事件中更改文本颜色-在焦点离开时将其更改为自定义颜色,然后在焦点进入时将其更改回自定义颜色。我问的是“您想要的任何颜色”。该条件已被破坏为。当然,这可以通过使用私有头来解决。但是,同样,如果Qt决定以某种方式改变行为(例如,就像您正在做的那样,通过修改边距),您将最终导致渲染中断。这是一个黑客。这不是一个解决办法。这不是解决办法。
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
    }
}
setStyleSheet("QLineEdit{"
              "    color: red;" //TEXT COLOR
              "}"
              "QLineEdit[text=\"\"]{"
              "    color: gray;" //TEXTHOLDER COLOR
              "}");
connect(ui->lineEdit, &QLineEdit::textChanged, [=]{ style()->polish(ui->lineEdit); });