-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSquareWidget.cpp
More file actions
48 lines (42 loc) · 1.21 KB
/
SquareWidget.cpp
File metadata and controls
48 lines (42 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "SquareWidget.h"
#include "qevent.h"
SquareWidget::SquareWidget(QWidget *parent) : QWidget(parent)
{
m_layout = new QBoxLayout(QBoxLayout::LeftToRight, this);
m_aspect = 1;
}
void SquareWidget::setChildWidget(QWidget* widget, qreal aspect)
{
m_layout->addItem(new QSpacerItem(0, 0));
m_layout->addWidget(widget);
m_layout->addItem(new QSpacerItem(0, 0));
setAspect(aspect);
}
void SquareWidget::setAspect(qreal aspect)
{
m_aspect = aspect;
}
void SquareWidget::resizeEvent(QResizeEvent *event)
{
adjustChildWidget();
}
void SquareWidget::adjustChildWidget()
{
float thisAspectRatio = (float)width() / height();
int widgetStretch, outerStretch;
if (thisAspectRatio > m_aspect) // too wide
{
m_layout->setDirection(QBoxLayout::LeftToRight);
widgetStretch = height() * m_aspect; // i.e., my width
outerStretch = (width() - widgetStretch) / 2 + 0.5;
}
else // too tall
{
m_layout->setDirection(QBoxLayout::TopToBottom);
widgetStretch = width() / m_aspect; // i.e., my height
outerStretch = (height() - widgetStretch) / 2 + 0.5;
}
m_layout->setStretch(0, outerStretch);
m_layout->setStretch(1, widgetStretch);
m_layout->setStretch(2, outerStretch);
}