Alright I have 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
#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
To copy to clipboard, switch view to plain text mode
layoutproblem.cpp
#include "layoutproblem.h"
#include <QVBoxLayout>
#include <QGridLayout>
#include <QMdiArea>
#include <QTimer>
#include <QTabWidget>
#include <QApplication>
#include <qwt_plot.h>
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 != 3; ++i)
{
window
->layout
()->addWidget
(new QwtPlot);
window
->layout
()->addWidget
(new QwtPlot);
tab
->addTab
(window,
"tab " + QString::number(i
));
mdiArea_->addSubWindow(tab);
tab->showMaximized();
}
}
int main(int argc, char *argv[])
{
LayoutProblem w;
w.show();
return a.exec();
}
#include "layoutproblem.h"
#include <QVBoxLayout>
#include <QGridLayout>
#include <QMdiArea>
#include <QTimer>
#include <QTabWidget>
#include <QApplication>
#include <qwt_plot.h>
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 != 3; ++i)
{
QWidget* window = new QWidget;
window->setLayout(new QVBoxLayout);
window->layout()->addWidget(new QwtPlot);
window->layout()->addWidget(new QwtPlot);
QTabWidget* tab = new QTabWidget;
tab->addTab(window, "tab " + QString::number(i));
mdiArea_->addSubWindow(tab);
tab->showMaximized();
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
LayoutProblem w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Run this code and you will notice that you can scale the widget much smaller than the minimum size hint. Next, try commenting the QTimer line and call createUI() directly:
// QTimer::singleShot(500, this, SLOT(createUI()));
createUI();
// QTimer::singleShot(500, this, SLOT(createUI()));
createUI();
To copy to clipboard, switch view to plain text mode
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?
Bookmarks