Minimum size hint ignored when adding widgets dynamically
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
Code:
#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
Code:
#include "layoutproblem.h"
#include <QVBoxLayout>
#include <QGridLayout>
#include <QMdiArea>
#include <QTimer>
#include <QTabWidget>
#include <QApplication>
#include <QPushButton>
LayoutProblem
::LayoutProblem(QWidget *parent
) : mdiArea_(new QMdiArea)
{
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)
{
w->setMinimumSize(200, 200);
window->layout()->addWidget(w);
w->setMinimumSize(200, 200);
window->layout()->addWidget(w);
tab
->addTab
(window,
"tab " + QString::number(i
));
mdiArea_->addSubWindow(tab);
tab
->setWindowTitle
("window " + QString::number(i
));
tab->showMaximized();
}
}
int main(int argc, char *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:
Code:
// 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?
Re: Minimum size hint ignored when adding widgets dynamically
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 it as such.
Good luck.
Re: Minimum size hint ignored when adding widgets dynamically
Re: Minimum size hint ignored when adding widgets dynamically
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,
_
Re: Minimum size hint ignored when adding widgets dynamically
@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.