Как вывести слова над текстом в QTextEdit - PullRequest
2 голосов
/ 17 апреля 2019

У меня есть структура с двумя членами: word: строка и pos: int (часть речи).

И я хочу вывести words с их pos выше в QTextEdit, как это

enter image description here

Это вообще возможно или проще сделать цветной шрифт и фон с помощью SyntaxHighlighter?

1 Ответ

2 голосов
/ 17 апреля 2019

Вы можете установить текст как 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

...