Qt UI设计之《可缩放固定比例窗口》

该窗口有两个窗口控件,内部控件按照自定义比例伸缩在这里插入图片描述
1 主窗口相关代码
#pragma once
#include <QWidget>

class uimainwnd : public QWidget
{
	Q_OBJECT

public:
	uimainwnd(QWidget *parent = Q_NULLPTR);
	~uimainwnd();

protected:
	void resizeEvent(QResizeEvent* event) override;

private:
	void initUI();
	QHBoxLayout *m_hbLayoutMain;	
};
#include "stdafx.h"
#include "uimainwnd.h"
#include "uititlebar.h"
#include "uicontentwnd.h"

static inline void GetScaleAndCenterPos(int baseCX, int baseCY, int windowCX,
	int windowCY, int& x, int& y,
	float& scale)
{
	double windowAspect, baseAspect;
	int newCX, newCY;

	windowAspect = double(windowCX) / double(windowCY);
	baseAspect = double(baseCX) / double(baseCY);

	if (windowAspect > baseAspect) {
		scale = float(windowCY) / float(baseCY);
		newCX = int(double(windowCY) * baseAspect);
		newCY = windowCY;
	}
	else {
		scale = float(windowCX) / float(baseCX);
		newCX = windowCX;
		newCY = int(float(windowCX) / baseAspect);
	}

	x = windowCX / 2 - newCX / 2;
	y = windowCY / 2 - newCY / 2;
}

static inline void GetCenterPosFromFixedScale(int baseCX, int baseCY,
	int windowCX, int windowCY,
	int& x, int& y, float scale)
{
	x = (float(windowCX) - float(baseCX) * scale) / 2.0f;
	y = (float(windowCY) - float(baseCY) * scale) / 2.0f;
}

static inline QSize GetPixelSize(QWidget* widget)
{
#ifdef SUPPORTS_FRACTIONAL_SCALING
	return widget->size() * widget->devicePixelRatioF();
#else
	return widget->size() * widget->devicePixelRatio();
#endif
}

uimainwnd::uimainwnd(QWidget *parent)
	: QWidget(parent)
{
	this->setAutoFillBackground(true); // 自动刷新背景
	QPalette palette(this->palette());
	palette.setColor(QPalette::Background, RGB(76, 76, 76));
	this->setPalette(palette);
	initUI();
}

uimainwnd::~uimainwnd()
{
}

void uimainwnd::initUI()
{
	setWindowTitle("固定比例窗口");
	resize(650, 350);

	m_hbLayoutMain = new QHBoxLayout(this);

	uicontentwnd* _uicontentwnd = new uicontentwnd(this);
	m_hbLayoutMain->addWidget(_uicontentwnd);
	m_hbLayoutMain->setContentsMargins(0, 0, 0, 0);
	setLayout(m_hbLayoutMain);
}

void uimainwnd::resizeEvent(QResizeEvent* event)
{
	QSize  targetSize;
	targetSize = GetPixelSize(this);

	int x;
	int y;
	float scale;

	GetScaleAndCenterPos(
		4, // 宽高比
		3, // 宽高比
		targetSize.width(),
		targetSize.height(),
		x,
		y,
		scale);

	m_hbLayoutMain->setContentsMargins(x, y, x, y);
}
2 内部窗口控件相关代码
#pragma once
#include <QWidget>

class uicontentwnd : public QWidget
{
	Q_OBJECT
public:
	uicontentwnd(QWidget *parent = Q_NULLPTR);
	~uicontentwnd();
};

#include "stdafx.h"
#include "uicontentwnd.h"

uicontentwnd::uicontentwnd(QWidget *parent)
	: QWidget(parent)
{
	this->setAutoFillBackground(true); // 自动刷新背景
	QPalette palette(this->palette());
	palette.setColor(QPalette::Background, RGB(238, 238, 238));
	this->setPalette(palette);
}

uicontentwnd::~uicontentwnd()
{
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值