【问题标题】:Underline specific line in QTextEdit C++下划线 QTextEdit C++ 中的特定行
【发布时间】:2020-05-22 16:20:12
【问题描述】:

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

【问题讨论】:

    标签: c++ qt


    【解决方案1】:

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

    【讨论】:

    • 我希望文本只是改变颜色或加下划线,而不是改变为 html 外观
    • 在任何情况下,您都必须使用某种标记语言来指定该单词或该行应该是黄色/红色/蓝色或下划线/粗体/斜体等等。因此,HTML 格式为您提供了这种可能性。如果这对您的情况不起作用,请尝试使用不同的 GUI 控件或创建您自己的
    【解决方案2】:

    对于特定的 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;
     }
    

    【讨论】:

      【解决方案3】:

      这是一个简单的示例,向您展示如何在 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.");
      

      输出如下: output

      【讨论】:

        猜你喜欢
        • 2012-04-04
        • 2020-01-12
        • 2011-03-09
        • 1970-01-01
        • 1970-01-01
        • 2014-08-08
        • 1970-01-01
        • 2021-06-01
        • 1970-01-01
        相关资源
        最近更新 更多