c++qtqlayout

Layout widget resizing


I'm having a problem with my Layouts in QT. Im trying to achieve a List of widgets which can be expanded and contracted in the following manner:

|------------|                                            |------------|
|Title 1     |                                            |Title 1     | Widget1
|------------|                                            |------------|
|QTextEdit1  |   Widget 1                                 |Title 2     |
|            |                                            |------------|                                                  
|------------|Which, by clicking on the title, contract:  |QTextEdit2  | Widget2
|------------|                                            |            |
|Title 2     |                                            |------------|
|------------|                                                       
|QTextEdit1  |
|            |   Widget 2
|------------|

The problem is, so far I've only be able to achieve the following: (After clicking on title 1)

|------------|                                                      
|Title 1     |                                                     
|------------|                                                       
|            |                                                      
|            |                                                                                                   
|------------|     
|------------|                                                       
|Title 2     |                                                 
|------------|                                                       
|QTextEdit1  |
|            |
|------------|

That is, I am able to make the QTextEdit of my first widget disappear, keeping the title height intact (this is important), but the second widget does not replace the space left by the first QTextEdit. It is as if the QTextEdit is still there, and the second widget cannot occupy that space.

I have tried setting a maximum height for the widget1 when I construct it, but that doesn't seem to work. Oh, and each widget contains a QVBoxLayout, where a title and a QTextEdit are inserted, and then the list of widgets is another QVBoxLayout, where all the widgets are inserted.

Constructor:

mainLayout = new QVBoxLayout;
QHBoxLayout *headerLayout = new QHBoxLayout;
title = new QLabel("My title!!");

m_arrowLabel->setPixmap(m_arrowDown);
m_activated = true;
title->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
title->setMaximumHeight(16);
headerLayout->addWidget(title, 0, Qt::AlignLeft);
headerLayout->setContentsMargins(0,0,0,0);

textEdit = new QTextEdit;
textEdit->setReadOnly(true);
textEdit->setText("My text!!!");
textEdit->setMaximumHeight(textEdit->document()->size().height() + 50);

mainLayout->addLayout(headerLayout);
mainLayout->addWidget(textEdit);
mainLayout->setContentsMargins(QMargins(0,0,0,0));
this->setLayout(mainLayout);

Mouse Press event:

event->accept();
if (event->buttons() & Qt::LeftButton)
{
    if (m_activated)
    {
        m_activated = false;
        textEdit->setVisible(false);

    }else
    {
        m_activated = true;
        textEdit->setVisible(true);
    }
}

On my window where I populate it with widgets:

m_mainLayout = QVector<QVBoxLayout *> (10);

for (int i=0; i<m_mainLayout.size(); i++)
{
    myWidget[i] = new myWidget;
    myWidget[i]->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
    m_mainLayout->addWidget(myWidget[i]);
}

Solution

  • You're setting a fixed vertical size policy for your widgets. This is why when you hide the text edit inside them, they won't shrink. Use QWidget::setMaximumHeight if you want to limit the height of the widgets.

    Here is a small working example:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QLayout>
    #include <QLabel>
    #include <QPushButton>
    #include <QTextEdit>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        centralWidget()->setLayout(new QVBoxLayout);
        centralWidget()->layout()->setAlignment(Qt::AlignTop);
    
        for(int i = 0; i < 10; i++)
        {
            QWidget *widget = new QWidget;
            widget->setMaximumHeight(200);
            widget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
            QVBoxLayout *w_layout = new QVBoxLayout;
            widget->setLayout(w_layout);
            QHBoxLayout *top_layout = new QHBoxLayout;
            top_layout->addWidget(new QLabel("Title"));
            QPushButton *toggle_button = new QPushButton("Toggle");
            top_layout->addWidget(toggle_button);
            toggle_button->setCheckable(true);
            QTextEdit *text_edit = new QTextEdit;
            connect(toggle_button, SIGNAL(clicked(bool)), text_edit, SLOT(setHidden(bool)));
            w_layout->addLayout(top_layout);
            w_layout->addWidget(text_edit);
    
            centralWidget()->layout()->addWidget(widget);
        }
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    Clicking the buttons will either hide or show the text edits of the widgets. The widget will shrink if the text edit is hidden and expand if it is shown.