如何在QTextEdit小部件的QString中包含的HTML中编写“ <”

时间:2019-10-30 13:39:28

标签: c++ qt

我是QT新手。我正在为“ C ++类生成器”编写代码。我正在根据在主窗口中引入的数据生成QString,并将其放入第二个窗口的QTextEdit小部件中。我在我的QString“ Generated_code”中使用了一些HTML代码。问题是,当我引入“ <”字符时,它被视为HTML标记,因此不会在结果文本中显示。所以我该如何写这个字符并继续在我的QString中使用HTML(因为还有其他解决方案,即使不使用任何HTML代码编写QString)?

我得到的结果文本 .....一些文字 包括标题1> 包括header2> ...等等 。其他一些文字

void MainWindow::OpenDialogWindow()
{
....some other code

// a part of where i'm actually constructing the QString (MainWindow class methode)
if(!m_headers->itemText(0).isEmpty())
    {
        for(int i = 0 ; i < m_headers->count() ; i++)
        {
            Generated_code += "<br>#include <><<>"  + m_headers->itemText(i) + "<>><></br>";
        }
    }
.
.
.
..... some other code
m_codeGenerated_Window = new CodeGeneratedWindow(Generated_code);
    m_codeGenerated_Window->exec();
}



// the constructor of the second window
CodeGeneratedWindow::CodeGeneratedWindow(QString text)
{
    m_diagLayout = new QVBoxLayout(this);
    m_text = new QTextEdit();
    m_text->setText(text);;

.... some code 
}

1 个答案:

答案 0 :(得分:2)

您可能会做类似的事情:

for(int i = 0 ; i < m_headers->count() ; i++)
{
    const auto cCode = QString("#include <%1>").arg(m_headers->itemText(i));
    Generated_code += QString("<br>%1</br>").arg(cCode.toHtmlEscaped());
}