parent
cbc2c688c4
commit
4264098e1e
@ -0,0 +1,74 @@
|
||||
#include <QResizeEvent>
|
||||
|
||||
#include "keepradiowidget.h"
|
||||
|
||||
KeepRadioWidget::KeepRadioWidget(QWidget *parent) :
|
||||
QWidget(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
KeepRadioWidget::~KeepRadioWidget()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void KeepRadioWidget::setWidget(QWidget *w)
|
||||
{
|
||||
if (!w) {
|
||||
return;
|
||||
}
|
||||
w->setParent(this);
|
||||
m_subWidget = w;
|
||||
}
|
||||
|
||||
void KeepRadioWidget::setWidthHeightRadio(float widthHeightRadio)
|
||||
{
|
||||
if (fabs(m_widthHeightRadio - widthHeightRadio) < 0.000001f) {
|
||||
return;
|
||||
}
|
||||
m_widthHeightRadio = widthHeightRadio;
|
||||
adjustSubWidget();
|
||||
}
|
||||
|
||||
const QSize KeepRadioWidget::goodSize()
|
||||
{
|
||||
if (!m_subWidget || m_widthHeightRadio < 0.0f) {
|
||||
return QSize();
|
||||
}
|
||||
return m_subWidget->size();
|
||||
}
|
||||
|
||||
void KeepRadioWidget::resizeEvent(QResizeEvent *event)
|
||||
{
|
||||
Q_UNUSED(event)
|
||||
adjustSubWidget();
|
||||
}
|
||||
|
||||
void KeepRadioWidget::adjustSubWidget()
|
||||
{
|
||||
if (!m_subWidget) {
|
||||
return;
|
||||
}
|
||||
|
||||
QSize curSize = size();
|
||||
QPoint pos(0, 0);
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
if (m_widthHeightRadio > 1.0f) {
|
||||
// base width
|
||||
width = curSize.width();
|
||||
height = curSize.width() / m_widthHeightRadio;
|
||||
pos.setY((curSize.height() - height) / 2);
|
||||
} else if (m_widthHeightRadio > 0.0f) {
|
||||
// base height
|
||||
height = curSize.height();
|
||||
width = curSize.height() * m_widthHeightRadio;
|
||||
pos.setX((curSize.width() - width) / 2);
|
||||
} else {
|
||||
// full widget
|
||||
height = curSize.height();
|
||||
width = curSize.width();
|
||||
}
|
||||
m_subWidget->setGeometry(pos.x(), pos.y(), width, height);
|
||||
}
|
||||
@ -0,0 +1,29 @@
|
||||
#ifndef KEEPRADIOWIDGET_H
|
||||
#define KEEPRADIOWIDGET_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QPointer>
|
||||
|
||||
class KeepRadioWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit KeepRadioWidget(QWidget *parent = nullptr);
|
||||
~KeepRadioWidget();
|
||||
|
||||
void setWidget(QWidget* w);
|
||||
void setWidthHeightRadio(float widthHeightRadio);
|
||||
const QSize goodSize();
|
||||
|
||||
protected:
|
||||
void resizeEvent(QResizeEvent *event);
|
||||
void adjustSubWidget();
|
||||
|
||||
private:
|
||||
float m_widthHeightRadio = -1.0f;
|
||||
QPointer<QWidget> m_subWidget;
|
||||
QSize m_goodSize;
|
||||
|
||||
};
|
||||
|
||||
#endif // KEEPRADIOWIDGET_H
|
||||
@ -1,19 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>MagneticWidget</class>
|
||||
<widget class="QWidget" name="MagneticWidget">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>400</width>
|
||||
<height>300</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string/>
|
||||
</property>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
@ -1,8 +1,9 @@
|
||||
FORMS += \
|
||||
$$PWD/magneticwidget.ui
|
||||
FORMS +=
|
||||
|
||||
HEADERS += \
|
||||
$$PWD/keepradiowidget.h \
|
||||
$$PWD/magneticwidget.h
|
||||
|
||||
SOURCES += \
|
||||
$$PWD/keepradiowidget.cpp \
|
||||
$$PWD/magneticwidget.cpp
|
||||
|
||||
Loading…
Reference in new issue