如何在QTextEdit中输出文本上方的单词

问题描述 投票:2回答:1

我有一个有两个成员的结构:word:string和pos:int(词性)。

我想在words输出pos和他们的QTextEdit像这样

enter image description here

这是偶数可能还是用SyntaxHighlighter制作彩色字体和背景更容易?

c++ qt part-of-speech
1个回答
2
投票

您可以将文本设置为html:

QString pos = "Hello. p .v.";
QString word = "word";

ui->textEdit->setHtml("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">"
                     "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">"
                     "p, li { white-space: pre-wrap; }"
                     "</style></head><body style=\" font-family:'Ubuntu'; font-size:11pt; font-weight:400; font-style:normal;\">"
                     "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-style:italic; color:#888a85;\">" + pos + ". </span></p>"
                     "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:14pt; font-weight:600; color:#2e3436;\">" + word + "</span></p></body></html>");

输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.