在 QTextEdit C++ 中为特定行添加下划线

Underline specific line in QTextEdit C++

本文关键字:添加 下划线 QTextEdit C++      更新时间:2023-10-16

如何为QTextEdit的多个不同行添加下划线/更改颜色?这里找到的先前答案都没有帮助。它们都更改了所有QTextEdit行的颜色。

您可以使用setHtml(( 方法 (https://doc.qt.io/qt-5/qtextedit.html#html-prop( 来实现这样的外观。

对于特定的 QLineEdit,您可以使用QWidget::setStyle(QStyle *style)

要更改QLineEdit中的文本,只需使用样式即可。

看看这个: https://docs.huihoo.com/qt/5.x/stylesheet-examples.html

和这个:https://doc.qt.io/qt-5/stylesheet-reference.html

例如:

QLineEdit 
{
border: 2px solid gray;
border-radius: 10px;
padding: 0 8px;
background: yellow;
border-bottom: 2px solid maroon;
selection-background-color: darkgray;
}

这是一个简单的例子,展示了如何在Qt中使用文本元素:

ui->textEdit->setText("this is a <a style='color:red;'>red</a> word!");
ui->textEdit->append("This is an <u>underlined</u> text.");
ui->textEdit->append("This is a <a style='color:red;'><u>red underlined</u></a>text.");

输出如下所示: 输出