Qt Manual/Fixed Layout Manager ("QManualLayout")

I still question whether I missed something very obvious, but it seems difficult to manually layout widgets in the Qt Framework, and there isn't any provided QManualLayout layout manager. Below you will find my implementation of one. And yes, there are plenty of special cases where you might need one. Using my layout manager you can place widgets anywhere on the layout using the common widget methods such as move(), resize(), and setGeometry().

/*******************************************************************
**
** Qt Manual/Fixed Layout Manager ("QManualLayout")
** ManualLayout.h
** v1.0
**
** This layout manager lets you easily programmatically place
** widgets in a fixed/manual method. Widgets can be placed
** in a fixed method and dynamically moved on the layout manager's
** widget resizeEvent(QResizeEvent*) event.
**
** Usage:
**
** this->setLayout(new ManualLayout());
** QWidget *widget = new QWidget(this);
** widget->setGeometry(10,10,200,100);
**
** Copyright 2012 Dan Krusi, Nerves Design & Engineering
** dan.krusi@nerves.ch | www.dankrusi.com | www.nerves.ch
**
*******************************************************************/

#ifndef MANUALLAYOUT_H
#define MANUALLAYOUT_H

#include <QtGui>
#include <QList>

class ManualLayout : public QLayout {

private:
QList<QLayoutItem*> list;

public:
ManualLayout(QWidget *parent): QLayout(parent) {}
ManualLayout(): QLayout() {}
~ManualLayout();

public:
int count() const;
void addItem(QLayoutItem *item);
QSize sizeHint() const;
QSize minimumSize() const;
QLayoutItem *itemAt(int) const;
QLayoutItem *takeAt(int);
void setGeometry(const QRect &rect);
};

#endif // MANUALLAYOUT_H

/*******************************************************************
**
** Qt Manual/Fixed Layout Manager ("QManualLayout")
** ManualLayout.cpp
** v1.0
**
** This layout manager lets you easily programmatically place
** widgets in a fixed/manual method. Widgets can be placed
** in a fixed method and dynamically moved on the layout manager's
** widget resizeEvent(QResizeEvent*) event.
**
** Usage:
**
** this->setLayout(new ManualLayout());
** QWidget *widget = new QWidget(this);
** widget->setGeometry(10,10,200,100);
**
** Copyright 2012 Dan Krusi, Nerves Design & Engineering
** dan.krusi@nerves.ch | www.dankrusi.com | www.nerves.ch
**
*******************************************************************/

#include "ManualLayout.h"

int ManualLayout::count() const {
return list.size();
}

QLayoutItem *ManualLayout::itemAt(int idx) const {
return list.value(idx);
}

QLayoutItem *ManualLayout::takeAt(int idx) {
return idx >= 0 && idx < list.size() ? list.takeAt(idx) : 0;
}

void ManualLayout::addItem(QLayoutItem *item) {
list.append(item);
}

ManualLayout::~ManualLayout() {
QLayoutItem *item;
while ((item = takeAt(0))) delete item;
}

void ManualLayout::setGeometry(const QRect &r) {
QLayout::setGeometry(r);
}

QSize ManualLayout::sizeHint() const {
QSize s(0,0);
int n = list.count();
int i = 0;
while (i < n) {
s = s.expandedTo(list.at(i)->sizeHint());
i++;
}
return s;
}

QSize ManualLayout::minimumSize() const {
QSize s(0,0);
int n = list.count();
int i = 0;
while (i < n) {
s = s.expandedTo(list.at(i)->minimumSize());
i++;
}
return s;
}

You can download a demo project including the source here:

Source Download: ManualLayout_src.zip

Tags: code

 
Back to Blag...