objects.cpp Example File
activeqt/hierarchy/objects.cpp
#include "objects.h"
#include <QLayout>
#include <QPainter>
QParentWidget::QParentWidget(QWidget *parent)
: QWidget(parent),
m_vbox(new QVBoxLayout(this))
{
}
void QParentWidget::createSubWidget(const QString &name)
{
QSubWidget *sw = new QSubWidget(this, name);
m_vbox->addWidget(sw);
sw->setLabel(name);
sw->show();
}
QSubWidget *QParentWidget::subWidget(const QString &name)
{
return findChild<QSubWidget *>(name);
}
QSize QParentWidget::sizeHint() const
{
return QWidget::sizeHint().expandedTo(QSize(100, 100));
}
QSubWidget::QSubWidget(QWidget *parent, const QString &name)
: QWidget(parent)
{
setObjectName(name);
}
void QSubWidget::setLabel(const QString &text)
{
m_label = text;
setObjectName(text);
update();
}
QString QSubWidget::label() const
{
return m_label;
}
QSize QSubWidget::sizeHint() const
{
QFontMetrics fm(font());
return QSize(fm.width(m_label), fm.height());
}
void QSubWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setPen(palette().text().color());
painter.drawText(rect(), Qt::AlignCenter, m_label);
}