Resizing qt widgets when their children are hidden

How would I go about resizing the widget when the retry child is hidden so that it looks as in the first image? The main layout is a QVBoxLayout, the retry child is a widget with a QVBoxLayout as well.

I've tried the following:

  • update()
  • updateGeometry()
  • setGeometry(childrenRect())
  • layout()->activate()

on the main widget as soon as I've set the retry widget to hidden. Do I need to intercept some event to do this?

Answers


The adjustSize function may do what you want.


Here is a basic example which does auto-resize upon widget hide/show.

dialog.h file:

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QtGui>

class dialog : public QDialog
{
    Q_OBJECT


public:
    explicit dialog(QWidget *parent = 0)
    {

        vlayout.addWidget(&checkbox);
        vlayout.addWidget(&label);
        label.setText("Label");

        setLayout(&vlayout);
        this->layout()->setSizeConstraint(QLayout::SetFixedSize); // !!! This is the what makes it auto-resize

        checkbox.setChecked(true);
        connect(&checkbox,SIGNAL(toggled(bool)),&label,SLOT(setVisible(bool)));
    }

private:
    QVBoxLayout vlayout;
    QCheckBox checkbox;
    QLabel label;

};

#endif // DIALOG_H

and main.c

#include <QApplication>
#include "dialog.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    dialog d;
    d.show();
    return a.exec();
}

This is one I have just developed an answer for!!! ( Note: this will be given in PyQt4 form )

I have needed the ability to re-size my app window when showing and hiding extra panels. However in the past I ended up just reconfiguring the UI and/or work flow so that re-sizing the window wasn't necessary. However now it is a needed must.

The window can be re-sized during its creation. The window can be re-sized directly (e.g. self.resize( xSize, ySize ) However if you create some lines of code that capture the needed sizes, hides the desired section, recalculates what the size should be, the re-size seems to be not take at all.

(example)

curHgt = self.size().height()
curWth = self.size().width()
myFrmHgt = self.ui.reviewFrm.size().height()
self.ui.reviewFrm.hide()
self.resize( curWth, (curHgt - myFrmHgt ) )

Nothing. Doesn't work. Why not?

It today's hunt for an answer still did not come up with a viable answer. There are several offerings for options. Hunt up the widget hierarchy, invalidate, adjustSize. A rather exhaustive layout shaking that just will not work if you are attempting to re-size the the main window. There was also an overwhelming response of saying to set your constraint to FixedSize. But that rather defeats the entire purpose, kinda like saying to cut your hand off at the wrist if you have a hang nail.

However I did stumble across someone that had some success with digging in and discovering that the reason the re-size isn't taking hold is because of the queue of Posted Events that will reset the resize.

His solution was to run the QApplication.sendPostedEvents() just before doing your application re-size, so that the queued events don't reset your window. Yes, and No. Yes, this is the solution, and I am sure there is a prettier way of doing it, but you need to really shake the the queue out like a washed paint roller first.

Here is my simple snippet that I set up that so far seems to be solid.

result = [ self.size().width(), self.size().height() ]
if self.ui.reviewFrm.isVisible():
    result[1] -= self.ui.reviewFrm.size().height()
    self.ui.reviewFrm.hide()

    while self.size().height() > result[1]:
        self.appy.sendPostedEvents()
        self.resize( result[0], result[1] )

Nope. It ain't pretty. However at most I have only seen it shake the paint roller twice before the Posted Events allowed for the window to resize. No need to lock constraints or shake the parent widgets or climb through the hierarchy looking for size hints.


My problem was a little different in that I had a top-level QWidget (instead of a QDialog or a QMainWindow) that had children within a layout. When those children were resizing to larger, the QWidget would resize to accommodate, but not when they became SMALLER. Much like above, the area just had empty space. I finally found a solution:

The layout used in my QWidget had a property called setSizeConstraint(). This is normally set to "QLayout::SetDefaultConstraint", which simply affects the "minimumSize" of the QWidget that owns it. Instead, I chose "QLayout::SetFixedSize" which made the layout adjust the fixed size of the owning QWidget, effectively making it SMALLER when it needed to be.


If you want your widget to be resizable after setting QLayout::SetFixedSize you will also need to override QWidget::resizeEvent().

void resizeEvent(QResizeEvent* event) override {
    QWidget::resizeEvent(event);
    layout()->setSizeConstraint(QLayout::SetMinAndMaxSize);
};

Need Your Help

Internet Explorer 8 HTML5 elements not being styled properly

jquery html css internet-explorer-8 compatibility

I have a problem with my website in Internet Explorer 8, it doesn't happen in the version I have personally, but my friend tried it and showed me a screenshot.

Did you enable the php_fileinfo extension in Azure

php azure laravel

How can I enable php_fileinfo in an Azure website, for Laravel to determine whether the file is a PDF?