我可以使用简单的 html 使用单行大小的 QTextEdit 吗? [英] Can I have single line sized QTextEdit with simple html?

查看:105
本文介绍了我可以使用简单的 html 使用单行大小的 QTextEdit 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示包含以下样式的文本的简单状态行:

I need to display simple status row with text which will contain following styles:

  • 颜色
  • 粗体
  • 斜体

QTextEdit 可以渲染简单的 HTML.但它也强行扩展了多行:

QTextEdit can render simple HTML. But it also forcibly expands over multiple lines:

添加了红色背景以强调QTextEdit 的尺寸.所需大小是一行文本的大小.我如何做到这一点?

Red background was added to emphasize the dimensions of the QTextEdit. Desired size is the size of one text line. How do I achieve that?

推荐答案

首先,如果你只是使用了一个 QLabel,你不需要做任何特别的事情:它支持丰富的文本格式,只占用必要的空间:

First of all, if you've simply used a QLabel, you'd not need to do anything special: it supports rich text format, and only takes as much space as necessary:

#include <QtWidgets>
int main(int argc, char ** argv) {
   QApplication app{argc, argv};
   QWidget w;
   QVBoxLayout layout{&w};
   QLineEdit edit;
   QLabel message{"Foo <font color=\"red\">Bar!</font>"};
   message.setTextFormat(Qt::RichText);
   message.setWordWrap(true);
   message.setFrameStyle(QFrame::Box);
   layout.addWidget(&edit);
   layout.addWidget(&message);
   layout.addStretch();
   QObject::connect(&edit, &QLineEdit::textChanged, &message, &QLabel::setText);
   w.show();
   return app.exec();
}

如果你坚持使用 QTextEdit:它包含一个 QTextDocument,由它的 documentLayout() 布局.每次其大小发生变化时,布局都会发出一个信号.您可以根据该信号来更改小部件的高度以适应文档的大小.考虑到 QTextEdit 的结构:它是一个 QAbstractScrollArea 并且内容显示在 viewport() 小部件中.目标是使 viewport() 足够大以适合文本文档.小部件本身可能更大,具体取决于活动样式或样式表.

If you insist to use a QTextEdit: It contains a QTextDocument, laid out by its documentLayout(). The layout emits a signal each time its size changes. You can act on that signal to change the height of the widget to accommodate the size of the document. Take into account the structure of a QTextEdit: it is a QAbstractScrollArea and the contents are shown in the viewport() widget. The goal is for the viewport() to be large enough to fit the text document. The widget itself may be bigger, depending on the active style or style sheet.

以下是您如何实现这一点的示例.行编辑的内容传播到只读message QTextEdit,以便您可以注意到小部件大小是如何随着文本的增加而实时更新的长到适合一行.这会在您更改小部件的宽度时自动更新大小,因为文档大小也会因高度与宽度的权衡而发生变化.

Below is an example of how you might implement this. The contents of the line edit are propagated to the read-only message QTextEdit, so that you can note how the widget size is updated in the real time as the text gets too long to fit in one line. This automatically takes care of updating the size when you change the width of the widget, as the document size changes as well due to height-for-width trade-off.

// https://github.com/KubaO/stackoverflown/tree/master/questions/textedit-height-37945130
#include <QtWidgets>

void updateSize(QTextEdit * edit) {
   auto textHeight = edit->document()->documentLayout()->documentSize().height();
   edit->setFixedHeight(textHeight + edit->height() - edit->viewport()->height());
}

int main(int argc, char ** argv) {
   QApplication app{argc, argv};
   QWidget w;
   QVBoxLayout layout{&w};
   QLineEdit edit;
   QTextEdit message;
   message.setReadOnly(true);
   message.setText("Foo Bar!");
   layout.addWidget(&edit);
   layout.addWidget(&message);
   layout.addStretch();
   QObject::connect(&edit, &QLineEdit::textChanged, &message, &QTextEdit::setPlainText);
   QObject::connect(message.document()->documentLayout(),
                    &QAbstractTextDocumentLayout::documentSizeChanged,
                    &message, [&]{ updateSize(&message); });
   w.show();
   return app.exec();
}

这篇关于我可以使用简单的 html 使用单行大小的 QTextEdit 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆