PDA

View Full Version : Minimum size hint ignored when adding widgets dynamically



TheComet
16th August 2016, 14:18
I've noticed that the minimum size hint of widgets in a layout are ignored if you add the widgets later on (i.e. not in the constructor).

Here is an example that reproduces the problem I'm having.

layoutproblem.h

#ifndef LAYOUTPROBLEM_H
#define LAYOUTPROBLEM_H

#include <QWidget>

class QMdiArea;

class LayoutProblem : public QWidget
{
Q_OBJECT

public:
explicit LayoutProblem(QWidget *parent = 0);
virtual ~LayoutProblem();

private slots:
void createUI();

private:
QMdiArea* mdiArea_;
};

#endif // LAYOUTPROBLEM_H


layoutproblem.cpp

#include "layoutproblem.h"

#include <QVBoxLayout>
#include <QGridLayout>
#include <QMdiArea>
#include <QTimer>
#include <QTabWidget>
#include <QApplication>
#include <QPushButton>


LayoutProblem::LayoutProblem(QWidget *parent) :
QWidget(parent),
mdiArea_(new QMdiArea)
{
setLayout(new QGridLayout);
layout()->addWidget(mdiArea_);
mdiArea_->setViewMode(QMdiArea::TabbedView);

resize(200, 200);

QTimer::singleShot(500, this, SLOT(createUI()));
// createUI();
}

LayoutProblem::~LayoutProblem()
{
}

void LayoutProblem::createUI()
{
for(unsigned i = 0; i != 2; ++i)
{
QWidget* window = new QWidget;
window->setLayout(new QVBoxLayout);
QPushButton* w = new QPushButton("test");
w->setMinimumSize(200, 200);
w->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
window->layout()->addWidget(w);

w = new QPushButton("test");
w->setMinimumSize(200, 200);
w->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
window->layout()->addWidget(w);

QTabWidget* tab = new QTabWidget;
tab->addTab(window, "tab " + QString::number(i));

mdiArea_->addSubWindow(tab);
tab->setWindowTitle("window " + QString::number(i));
tab->showMaximized();
}
}

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
LayoutProblem w;
w.show();

return a.exec();
}


Run this code and you will notice that you can scale the window much smaller than the minimum size hint of the QPushButton (which is set to 200x200). Next, try commenting the QTimer line and call createUI() directly:


// QTimer::singleShot(500, this, SLOT(createUI()));
createUI();

Re-run the code and now everything works as expected.

How can I add widgets to my layouts dynamically without Qt ignoring the minimum size hints?

jefftee
17th August 2016, 04:37
I don't have any suggestions to resolve the issue unfortunately, but I can confirm that I experienced the exact same behavior you described on my Mac using Qt 5.7.0. Seems like it should work. Interestingly enough, setMaximumSize seems to be honored, so my best guess is it's a bug. I'd suggest that you report (https://bugreports.qt.io/secure/Dashboard.jspa) it as such.

Good luck.

TheComet
17th August 2016, 14:13
Bug report created: https://bugreports.qt.io/browse/QTBUG-55352

anda_skoa
17th August 2016, 19:36
I am not sure I understand: you are saying that you can manually resize the window to a smaller size than what the minimum sizes would allow?

Cheers,
_

jefftee
18th August 2016, 01:26
@anda_skoa, yes, I used his example code and confirmed that the minimum size is ignored if set via the singleshot timer and honored when set in the constructor. I tried several things to make it work, but could not. I was able to confirm that the maximum size seems to work regardless, so it would seem to be a bug to me.