Qt控件拖拽分割线

Qt控件拖拽分割线

功能

  1. 控件拖拽排序
  2. 水平,垂直拖拽位置分割线
  3. 图片透明度设置

效果图

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

部分代码

//设置图片透明度
void setImageTransparency(QImage &image, int alpha)
{
	const int height = image.height();
	for (int l = 0; l < height; l++) {
		QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(l));
		QRgb *lineEnd = line + image.width();
		for (; line < lineEnd; line++) {
			const QRgb rgba = *line;
			*line = qRgba(qRed(rgba), qGreen(rgba), qBlue(rgba), alpha);
		}
	}
}
DragBaseBoxLayout::DragBaseBoxLayout(QWidget *parent) 
	: QWidget(parent)
	, m_isDragout(false)
{
	setAcceptDrops(true);
	this->installEventFilter(this);
}

DragBaseBoxLayout::~DragBaseBoxLayout()
{

}

void DragBaseBoxLayout::addWidget(QWidget* widget)
{
	this->layout()->addWidget(widget);
}

void DragBaseBoxLayout::hideIndicator(Indicator i)
{
	if (m_indicators[i])
		m_indicators[i]->hide();
}

void DragBaseBoxLayout::showIndicator(Indicator i, const QRect &geometry, const QPalette &p)
{
	if (!m_indicators[i])
		m_indicators[i] = new InvisibleWidget(this);
	QWidget *indicator = m_indicators[i];
	indicator->setAutoFillBackground(true);
	indicator->setPalette(p);
	indicator->setGeometry(geometry);
	indicator->show();
	indicator->raise();
}

int DragBaseBoxLayout::findItemAt(const QPoint &pos) const
{
	if (!layout())
		return -1;

	const QLayout *lt = layout();
	const int count = lt->count();

	if (count == 0)
		return -1;

	int best = -1;
	int bestIndex = -1;

	for (int index = 0; index < count; index++) {
		QLayoutItem *item = lt->itemAt(index);
		bool visible = true;
		if (const QWidget *w = item->widget())
			visible = w->isVisible();
		if (visible) {
			const QRect g = item->geometry();

			const int dist = (g.center() - pos).manhattanLength();
			if (best == -1 || dist < best) {
				best = dist;
				bestIndex = index;
			}
		}
	}
	return bestIndex;
}

bool DragBaseBoxLayout::eventFilter(QObject *watched, QEvent *event)
{
	if (watched == this){
		if (event->type() == QEvent::ChildRemoved && m_isDragout){
			QBoxLayout* layout = dynamic_cast<QBoxLayout*>(this->layout());
			layout->insertWidget(m_srcIndex, m_widget);
			m_widget->show();
		}
	}
	return QWidget::eventFilter(watched, event);
}

工程文件

Qt交流大会 853086607 免费群中
在这里插入图片描述

结尾

不定期上传新作品,解答群中作品相关问题。相关外,能解答则解答。欢迎大家一起探索Qt世界!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雨田哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值