「表示者」として機能するQTextEditがあります(falseに編集可能)。表示されるテキストはワードラップされます。ここで、このテキストボックスの高さを設定して、テキストが正確に収まるようにします (最大の高さも考慮します)。
基本的に、レイアウトの下の (同じ垂直レイアウト内の) ウィジェットは、できるだけ多くのスペースを確保する必要があります。
これを最も簡単に達成するにはどうすればよいでしょうか?
「表示者」として機能するQTextEditがあります(falseに編集可能)。表示されるテキストはワードラップされます。ここで、このテキストボックスの高さを設定して、テキストが正確に収まるようにします (最大の高さも考慮します)。
基本的に、レイアウトの下の (同じ垂直レイアウト内の) ウィジェットは、できるだけ多くのスペースを確保する必要があります。
これを最も簡単に達成するにはどうすればよいでしょうか?
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
ていますが、とにかくほとんど同じものです)。
QTextEdit
必要な機能に特別なものがない限りQLabel
、ワードラップをオンにすると、希望どおりの結果が得られます。
下にあるテキストの現在のサイズは、
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();
}
私の場合、QLabel を QScrollArea 内に配置しました。興味がある場合は、両方を組み合わせて独自のウィジェットを作成してください。
Pythonといえば、私は実際に見つけ.setFixedWidth( your_width_integer )
て.setFixedSize( your_width, your_height )
非常に便利です。C に同様のウィジェット属性があるかどうかは不明です。