I have such a text editor. I need to make sure that when the size of the main window is changed while the application is running, the size of the text field changes, and the sidebar remains the same.

Does QTextEdit have a property that allows you to change its size while stretching the main application window? SetMinimumSize () does not work correctly, possibly due to QGridLayout, but I'm not sure.

#include "mainclass.h" #include <QMainWindow> #include <QTextEdit> #include <QWidget> #include <QGridLayout> #include <QGroupBox> #include <QLabel> #include <QSpinBox> #include <QFontComboBox> #include <QCheckBox> #include <QTextCodec> #include <QPushButton> MainClass::MainClass(QWidget* parent) : QMainWindow(parent) { QTextCodec::setCodecForCStrings(QTextCodec::codecForName("cp1251")); QTextCodec::setCodecForTr(QTextCodec::codecForName("cp1251")); QTextCodec::setCodecForLocale(QTextCodec::codecForName("cp1251")); QGridLayout *QMLayout = new QGridLayout; QTextEdit *MainTextEdit = new QTextEdit(this); MainTextEdit->setMinimumSize(300, 300); QGroupBox *SidePanel = new QGroupBox (this); SidePanel->setGeometry(300,0, 220,300); QVBoxLayout *vbox = new QVBoxLayout; QGroupBox *groupFontBox = new QGroupBox(tr("Параметры")); QLabel *PLabel = new QLabel (this); PLabel->setText("Параметры"); QSpinBox *FontsizeSpin = new QSpinBox (this); FontsizeSpin->setRange(8, 20); FontsizeSpin->setSuffix(" пт"); FontsizeSpin->setSingleStep(2); QLabel *FLabel = new QLabel (this); FLabel->setText("Шрифт"); QFontComboBox *FStyleList = new QFontComboBox(this); vbox->addWidget(PLabel); vbox->addWidget(FLabel); vbox->addWidget(FStyleList); QLabel *SLabel = new QLabel(this); SLabel->setText("Размер"); vbox->addWidget(SLabel); vbox->addWidget(FontsizeSpin); QLabel *FSLabel = new QLabel (this); FSLabel->setText("Начертание"); vbox->addWidget(FSLabel); QCheckBox *ItalyStyle = new QCheckBox; ItalyStyle->setText("Курсив"); QCheckBox *BoldStyle = new QCheckBox; BoldStyle->setText("Полужирный"); QCheckBox *UnderLineStyle = new QCheckBox; UnderLineStyle->setText("Подчеркнутый"); vbox->addWidget(ItalyStyle); vbox->addWidget(BoldStyle); vbox->addWidget(UnderLineStyle); QHBoxLayout *BLayout = new QHBoxLayout; QGroupBox *ButtonGroup = new QGroupBox; ButtonGroup->setLayout(BLayout); vbox->addWidget(ButtonGroup); QPushButton *QuitButton = new QPushButton("Выход"); BLayout->addWidget(QuitButton); QPushButton *SaveButton = new QPushButton("Сохранить"); BLayout->addWidget(SaveButton); groupFontBox->setLayout(vbox); SidePanel->setLayout(vbox); QMLayout->addWidget(MainTextEdit, 0, 0, 3, 3); QMLayout->addWidget(SidePanel, 1, 0,1,1); this->setLayout(QMLayout); } MainClass::~MainClass() { } 

    1 answer 1

    There is such a function for all widgets:

     void QWidget::setSizePolicy(QSizePolicy::Policy horizontal, QSizePolicy::Policy vertical) 

    To solve your problem, use it instead of setMinimumSize:

     QTextEdit *MainTextEdit = new QTextEdit(this); MainTextEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); 

    This will expand the MainTextEdit to the entire cell in QGridLayout