问题 qtextedit - 调整大小以适应


我有一个 的QTextEdit 充当“显示器”(可编辑为假)。它显示的文本是自动换行的。现在我希望设置此文本框的高度,以使文本完全适合(同时也尊重最大高度)。

基本上布局下面的小部件(在相同的垂直布局中)应该获得尽可能多的空间。

如何才能最轻松地实现这一目标?


8653
2018-02-29 21:04


起源

无法在QT库中找到qtextbox - Tim Kachko
意思是qtextedit,固定(带链接) - paul23
如果你将QTextEdit放在另一个QScrollArea(设置最大高度)中,你可以使用我在那里给出的相同代码: stackoverflow.com/questions/7301785/... - alexisdm
@ paul23你可能想要检查我的回答类似的请求 这里。 - laurasia


答案:


我找到了一个非常稳定,简单的解决方案 QFontMetrics

from PyQt4 import QtGui

text = ("The answer is QFontMetrics\n."
        "\n"
        "The layout system messes with the width that QTextEdit thinks it\n"
        "needs to be.  Instead, let's ignore the GUI entirely by using\n"
        "QFontMetrics.  This can tell us the size of our text\n"
        "given a certain font, regardless of the GUI it which that text will be displayed.")

app = QtGui.QApplication([])

textEdit = QtGui.QPlainTextEdit()
textEdit.setPlainText(text)
textEdit.setLineWrapMode(True)      # not necessary, but proves the example

font = textEdit.document().defaultFont()    # or another font if you change it
fontMetrics = QtGui.QFontMetrics(font)      # a QFontMetrics based on our font
textSize = fontMetrics.size(0, text)

textWidth = textSize.width() + 30       # constant may need to be tweaked
textHeight = textSize.height() + 30     # constant may need to be tweaked

textEdit.setMinimumSize(textWidth, textHeight)  # good if you want to insert this into a layout
textEdit.resize(textWidth, textHeight)          # good if you want this to be standalone

textEdit.show()

app.exec_()

(原谅我,我知道你的问题是关于C ++,我正在使用Python,但是在 Qt 无论如何,他们几乎都是一样的。


8
2018-01-08 17:11





除非有一些特殊的能力 QTextEdit 你需要的,a QLabel 打开单词换行将完全符合您的要求。


2
2018-02-29 22:24



qlabel只在word边界上进行wordwraps - 当数据太大时它也不会显示滚动条。我开始使用标签,但我似乎需要两个功能集...... - paul23
嗯,一个 QLabel 不会工作然后:) - Arnold Spence


可以通过以下方式获得基础文本的当前大小

QTextEdit::document()->size();

而且我相信使用它我们可以相应地调整小部件的大小。

#include <QTextEdit>
#include <QApplication>
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTextEdit te ("blah blah blah blah blah blah blah blah blah blah blah blah");
    te.show();
    cout << te.document()->size().height() << endl;
    cout << te.document()->size().width() << endl;
    cout <<  te.size().height() << endl;
    cout <<  te.size().width() << endl;
// and you can resize then how do you like, e.g. :
    te.resize(te.document()->size().width(), 
              te.document()->size().height() + 10);
    return a.exec();    
}

1
2018-02-29 21:37



转换为“文档”时,包装文字不会丢失吗? - paul23
尝试编译我回答的代码,你会看到窗口小部件的大小和包含文档的大小之间的差异。自动换行不会丢失。但是您需要先显示小部件。 - Tim Kachko
嗯,这并没有真正解决 - 因为使用时没有设置尺寸 setText() 命令。 - paul23
您需要首先显示窗口小部件以形成文档。事实上这是一个糟糕的风格,因为QT中的小部件的大小实际上依赖于小部件的外部布局和大小策略,但无论如何你可以使用这个想法。 - Tim Kachko


说到Python,我实际上发现了 .setFixedWidth( your_width_integer ) 和 .setFixedSize( your_width, your_height ) 非常有用。不确定C是否具有类似的小部件属性。


0
2018-03-03 03:15





就我而言,我将QLabel放在QScrollArea中。如果你很热衷,你可以将两者结合起来制作自己的小部件。


0
2017-10-11 05:45