基本信息
源码名称:圆角widget实现方法(roundedwidgets.cpp)
源码大小:3.29KB
文件格式:.cpp
开发语言:C/C++
更新时间:2020-10-17
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

用qt很简单地实现圆角widget,并且不需要使用qss


#include "roundedwidgets.h"
#include <QDebug>

DGUI_USE_NAMESPACE
RoundedWidget::RoundedWidget(QWidget *parent, int r, int t, QPoint o)
: QWidget(parent), radius(r), margin(0), type(t), offset(o), painter(nullptr) {
	setPalette(DGuiApplicationHelper::instance()->applicationPalette());
	connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, [this]() {
		setPalette(DGuiApplicationHelper::instance()->applicationPalette());
		repaint();
	});
	if (!offset.isNull())
		connect(this, &RoundedWidget::widgetMoving, [=](QPoint pos) {
			this->blockSignals(true);
			this->move(pos   this->offset);
			this->blockSignals(false);
		});
}
void RoundedWidget::paintEvent(QPaintEvent *event) {
	if (!painter) {
		painter = new QPainter(this);
		painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
	}
	painter->begin(this);
	painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
	QPainterPath path;
	auto rect = event->rect();
	if (margin)
		rect.adjust(margin, margin, -margin, -margin);
	path.addRoundedRect(QRectF(rect), radius, radius);
	if (type) {
		auto tmpRect = rect;
		switch (type) {
			case TopTwo:
				tmpRect.adjust(0, rect.height() - radius, 0, 0);
				break;
			case BottomTwo:
				tmpRect.adjust(0, 0, 0, -(rect.height() - radius));
				break;
			case LeftTwo:
				tmpRect.adjust(0, 0, -(rect.width() - radius), 0);
				break;
			case RightTwo:
				tmpRect.adjust(rect.width() - radius, 0, 0, 0);
				break;
		}
		path.addRect(QRectF(tmpRect));
	}
	path.setFillRule(Qt::WindingFill);
	painter->fillPath(path, palette().window());
	painter->end();
}
void RoundedWidget::moveEvent(QMoveEvent *event) {
	emit widgetMoving(event->pos());
	return QWidget::moveEvent(event);
}
void RoundedWidget::setRadius(int r) {
	radius = r;
}
void RoundedWidget::setMargin(int d) // set self margins
{
	margin = d;
	setContentsMargins(margin, margin, margin, margin);
}
void RoundedWidget::setShadow(int d) {
	if (!d)
		return;
	QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect(this);
	effect->setOffset(0, 0);
	effect->setColor(QColor(0, 0, 0, 50));
	effect->setBlurRadius(d);
	setGraphicsEffect(effect);
	setMargin(d);
}
CircleButton::CircleButton(QIcon icon, QWidget *parent) : QPushButton(icon, "", parent), painter(nullptr) {
	setPalette(DGuiApplicationHelper::instance()->applicationPalette());
	connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, [this]() {
		setPalette(DGuiApplicationHelper::instance()->applicationPalette());
		repaint();
	});
}
void CircleButton::paintEvent(QPaintEvent *event) {
	if (!painter) {
		painter = new QPainter(this);
		painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
	}
	//	qDebug() << "renderHints:" << (painter->renderHints() == (QPainter::Antialiasing | QPainter::SmoothPixmapTransform));
	//	painter->save();
	painter->begin(this);
	painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
	QPainterPath path;
	path.addEllipse(event->rect());
	painter->fillPath(path, isChecked() ? palette().highlight() : palette().button());
	auto iconRect = event->rect();
	int d = int(iconRect.width() * 0.3);
	iconRect.adjust(d, d, -d, -d);
	painter->drawPixmap(iconRect, this->icon().pixmap(iconRect.width(), iconRect.height()));
	painter->end();
}