QTextEdit Magic Size

Here is the equivalent extracted code:

#include <QApplication> #include <QWidget> #include <QVBoxLayout> #include <QTextBrowser> #include <QTextEdit> class ChatMessageEdit : public QTextEdit { public: ChatMessageEdit(QWidget* parent) : QTextEdit(parent) { } virtual QSize sizeHint() const { return QSize(0, 25); } }; int main(int argc, char** argv) { QApplication app(argc, argv); QWidget* widget = new QWidget; QVBoxLayout* layout = new QVBoxLayout; QTextBrowser* log = new QTextBrowser(widget); layout->addWidget(log, 1); ChatMessageEdit* editor = new ChatMessageEdit(widget); editor->setMinimumHeight(editor->sizeHint().height()); // empty layout->addWidget(editor); widget->setLayout(layout); widget->show(); return app.exec(); } 

The minimum size for the editor is 25 pixels, and the minimum size. But for some strange reason, it is created with a size of about 100 pixels, which is always preferable to tips of my size. Everything else works as expected: extension (size hint is actually not fixed in my application), compression, etc. I tried to change the size policy, but with absolutely no result.

+4
source share
2 answers

This was the minumumSizeHint() method. I overloaded it to return sizeHint() , and now everything works as expected.

+3
source

You also don't notice how layouts work. Please read here about why your sizes are not respected in the layout.

0
source

Source: https://habr.com/ru/post/1299799/


All Articles