简体   繁体   English

如何更改 QLineEdit 中部分文本的颜色?

[英]How can I change color of part of the text in QLineEdit?

I want to add some syntax highlighting to text being written in QLineEdit, but it does not support rich text formatting, I can not change QlineEdit to something else, so I should find how to set color of text in this widget.我想为在 QLineEdit 中编写的文本添加一些语法突出显示,但它不支持富文本格式,我无法将 QlineEdit 更改为其他内容,因此我应该找到如何在此小部件中设置文本颜色。

Is there a way to do this?有没有办法做到这一点?

Just found a neat trick for that. 刚刚找到了一个巧妙的技巧。

static void setLineEditTextFormat(QLineEdit* lineEdit, const QList<QTextLayout::FormatRange>& formats)
{
    if(!lineEdit)
        return;

    QList<QInputMethodEvent::Attribute> attributes;
    foreach(const QTextLayout::FormatRange& fr, formats)
    {
        QInputMethodEvent::AttributeType type = QInputMethodEvent::TextFormat;
        int start = fr.start - lineEdit->cursorPosition();
        int length = fr.length;
        QVariant value = fr.format;
        attributes.append(QInputMethodEvent::Attribute(type, start, length, value));
    }
    QInputMethodEvent event(QString(), attributes);
    QCoreApplication::sendEvent(lineEdit, &event);
}

static void clearLineEditTextFormat(QLineEdit* lineEdit)
{
    setLineEditTextFormat(lineEdit, QList<QTextLayout::FormatRange>());
}

// Usage example:
QLineEdit* lineEdit = new QLineEdit;
lineEdit->setText(tr("Task Tracker - Entry"));

QList<QTextLayout::FormatRange> formats;

QTextCharFormat f;

f.setFontWeight(QFont::Bold);
QTextLayout::FormatRange fr_task;
fr_task.start = 0;
fr_task.length = 4;
fr_task.format = f;

f.setFontItalic(true);
f.setBackground(Qt::darkYellow);
f.setForeground(Qt::white);
QTextLayout::FormatRange fr_tracker;
fr_tracker.start = 5;
fr_tracker.length = 7;
fr_tracker.format = f;

formats.append(fr_task);
formats.append(fr_tracker);

setLineEditTextFormat(lineEdit, formats);

You can change the color with the use of style sheets . 您可以使用样式表更改颜色。

 QLineEdit* myLineEdit = new QLineEdit("Whatever");

 //for whatever case you want to change the color
 if(syntax_needs_to_highlighted)
      myLineEdit->setStyleSheet("QLineEdit#myLineEdit{color:blue}"); 

You may want to consider using QTextBrowser for this case. 在这种情况下,您可能需要考虑使用QTextBrowser

You can change color of texts like this: 你可以改变这样的文字颜色:

QLineEdit *line = new QLineEdit();
line->setText("this is a test");
line->setStyleSheet("foreground-color: blue;");

If it won't work, replace the last line with the following: 如果它不起作用,请用以下内容替换最后一行:

line->setStyleSheet("color: blue;");

I was able to accomplish this by overlaying a QLabel on top of a QLineEdit then making the text color of the line edit white.我能够覆盖一个做到这一点QLabel上的顶部QLineEdit ,然后拍行编辑白色的文本颜色。 When the textEdited signal is emitted, use it to update the text of the QLabel .当发出textEdited信号时,使用它来更新QLabel的文本。 The QLabel accepts rich text so you can process the text in the QLineEdit and replace key words with the HTML needed to display the text in the way you want it. QLabel接受富文本,因此您可以处理QLineEdit中的文本,并用所需的 HTML 替换关键字,以按您希望的方式显示文本。 I'm sure you could modify the code to change the text color of the current selection.我确定您可以修改代码以更改当前选择的文本颜色。

class LabelEditPair(QLineEdit):
    """
    QLineEdit that changes the color of the word 'blue' to blue and
    the changes the font weight of the word 'bold' to bold.
    """
    def __init__(self):
        super().__init__()

        self.label = QLabel("starting out")
        self.label.setParent(self)
        self.label.move(3, 0)
        self.label.setAttribute(Qt.WA_TransparentForMouseEvents)
        self.setStyleSheet("QLineEdit{color: white}")

        self.textEdited.connect(self.text_edited)

    def resizeEvent(self, event):
        self.label.setFixedHeight(self.height())
        self.label.setFixedWidth(self.width())
        super().resizeEvent(event)

    def text_edited(self, text):
        text = text.replace("blue", "<span style='color: blue'>blue</span>")
        text = text.replace("bold", "<span style='font-weight: bold'>bold</span>")
        self.label.setText(text)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM