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?
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?