image vb6 图片自适应_Qt炫酷3D图片预览

该博客介绍了如何在VB6中使用Qt库创建一个3D图片预览功能,包括自定义时间间隔滚动图片、鼠标点击切换图片以及窗口大小自适应的实现。核心代码部分展示了关键的编程逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

0f5b03e7cfabf204dcd0a3c3612111b4.gif

功能

  1. 自定义时间设置滚动图片
  2. 支持鼠标点击左右切换图片
  3. 自适应窗口大小

效果图

7e6c04007f29068f9e320caafc2ef1fa.gif

ff6697051abba00f3f3f9ce6804c0fea.gif

ad06a959e475c95b095b92e225dd3a4b.gif

核心代码

void ImageViewWindow::initControl()
{
	//场景
	m_scene = new QGraphicsScene(QRect(0, 0, 876, 368), this);	
	//图片信息
	m_imgMapInfolst << QMap<QString, QString>{
		{ "zIndex"  , "1"   }, 
		{ "width"   , "120" }, 
		{ "height"  , "150" }, 
		{ "top"     , "71"  }, 
		{ "left"    , "134" }, 
		{ "opacity" , "0.6" }
	};
	m_imgMapInfolst << QMap<QString, QString>{
		{ "zIndex", "2"   },
		{ "width", "130" },
		{ "height", "170" },
		{ "top", "61" },
		{ "left", "0" },
		{ "opacity", "0.7" }
	};
	m_imgMapInfolst << QMap<QString, QString>{
		{ "zIndex", "3"   },
		{ "width", "170" },
		{ "height", "218" },
		{ "top", "37" },
		{ "left", "110" },
		{ "opacity", "0.8" }
	};
	m_imgMapInfolst << QMap<QString, QString>{
		{ "zIndex", "4"   },
		{ "width", "224" },
		{ "height", "288" },
		{ "top", "0" },
		{ "left", "262" },
		{ "opacity", "1" }
	};
	m_imgMapInfolst << QMap<QString, QString>{
		{ "zIndex", "3"   },
		{ "width", "170" },
		{ "height", "218" },
		{ "top", "37" },
		{ "left", "468" },
		{ "opacity", "0.8" }
	};
	m_imgMapInfolst << QMap<QString, QString>{
		{ "zIndex", "2"   },
		{ "width", "130" },
		{ "height", "170" },
		{ "top", "61" },
		{ "left", "620" },
		{ "opacity", "0.7" }
	};
	m_imgMapInfolst << QMap<QString, QString>{
		{ "zIndex", "1"   },
		{ "width", "120" },
		{ "height", "150" },
		{ "top", "71" },
		{ "left", "496" },
		{ "opacity", "0.6" }
	};

	//场景中添加图片元素
	for (int index = 0; index < m_imgMapInfolst.size(); index++)
	{
		const auto imageInfoMap = m_imgMapInfolst[index];
		const QString&& centerImg = QString(":/ImageViewWindow/Resources/%1.jpg").arg(index + 1);
		const QPixmap&& pixmap = QPixmap(centerImg);
		GraphicsPixmap *item = new GraphicsPixmap();
		item->setPixmap(pixmap);
		item->setPixmapSize(QSize(imageInfoMap["width"].toInt(), imageInfoMap["height"].toInt()));
		item->setItemOffset(QPointF(imageInfoMap["left"].toInt() + image_xoffset, imageInfoMap["top"].toInt() + image_yoffset));
		item->setZValue(imageInfoMap["zIndex"].toInt());
		item->setOpacity(imageInfoMap["opacity"].toFloat());
		m_items << item;
		m_scene->addItem(item);
	}

	//left button
	GraphicsPixmap *leftBtn = new GraphicsPixmap();
	leftBtn->setCursor(QCursor(Qt::PointingHandCursor));
	leftBtn->setPixmap(QPixmap(":/ImageViewWindow/Resources/Wblog_left.png"));
	leftBtn->setItemOffset(QPointF(12, image_yoffset + 124));
	leftBtn->setZValue(5);
	m_scene->addItem(leftBtn);
	connect(leftBtn, SIGNAL(clicked()), this, SLOT(onLeftBtnClicked()));
	//right button
	GraphicsPixmap *rightBtn = new GraphicsPixmap();
	rightBtn->setCursor(QCursor(Qt::PointingHandCursor));
	rightBtn->setPixmap(QPixmap(":/ImageViewWindow/Resources/Wblog_right.png"));
	rightBtn->setItemOffset(QPointF(836, image_yoffset + 124));
	rightBtn->setZValue(5);
	m_scene->addItem(rightBtn);
	connect(rightBtn, SIGNAL(clicked()), this, SLOT(onRightBtnClicked()));

	//视图
	GraphicsView *view = new GraphicsView(m_scene);
	view->setFrameShape(QFrame::NoFrame);
	view->setParent(this);
	view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
	view->setBackgroundBrush(QColor(46, 46, 46));
	view->setCacheMode(QGraphicsView::CacheBackground);
	view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
	ui.viewlayout->addWidget(view);

	//动画: 大小,位置
	m_group = new QParallelAnimationGroup;
	for (int i = 0; i < m_items.count(); ++i) {
		QPropertyAnimation *anim = new QPropertyAnimation(m_items[i], "itemoffset");
		QPropertyAnimation *anims = new QPropertyAnimation(m_items[i], "itemsize");
		m_animationMap.insert(m_items[i], anim);
		m_animationsMap.insert(m_items[i], anims);
		anim->setDuration(1000);
		anims->setDuration(1000);
		anim->setEasingCurve(QEasingCurve::OutQuad);
		anims->setEasingCurve(QEasingCurve::OutQuad);
		m_group->addAnimation(anim);
		m_group->addAnimation(anims);
	}
	//定时切换图片
	m_timer = new QTimer(this);
	m_timer->setInterval(2000);
	connect(m_timer, &QTimer::timeout, [this](){
		nextPlay();
	});
	connect(m_group, &QParallelAnimationGroup::finished, [this](){
		m_isStart = false;
		m_timer->start();
	});
	m_timer->start();
}

void ImageViewWindow::onLeftBtnClicked()
{
	//鼠标点击的时候,先暂停定时器预览
	m_timer->stop();
	//上一张
	lastPlay();
}

void ImageViewWindow::onRightBtnClicked()
{
	//鼠标点击的时候,先暂停定时器预览
	m_timer->stop();
	//下一张
	nextPlay();
}

void ImageViewWindow::play()
{
	for (int index = 0; index < m_imgMapInfolst.size(); index++)
	{
		const auto item = m_items[index];
		QPropertyAnimation *anim = m_animationMap.value(item);
		QPropertyAnimation *anims = m_animationsMap.value(item);
		const auto imageInfoMap = m_imgMapInfolst[index];
		item->setZValue(imageInfoMap["zIndex"].toInt());
		item->setOpacity(imageInfoMap["opacity"].toFloat());
		QPointF pointf(imageInfoMap["left"].toInt() + image_xoffset, imageInfoMap["top"].toInt() + image_yoffset);
		const QString&& centerImg = QString(":/ImageViewWindow/Resources/%1.jpg").arg(index + 1);
		anim->setStartValue(item->itemoffset());
		anims->setStartValue(item->pixsize());
		anim->setEndValue(pointf);
		anims->setEndValue(QSize(imageInfoMap["width"].toInt(), imageInfoMap["height"].toInt()));
	}
	m_isStart = true;
}


void ImageViewWindow::nextPlay()
{
	m_group->stop();
	auto firstItem = m_items.takeAt(0);
	m_items << firstItem;
	play();
	m_group->start();
}

void ImageViewWindow::lastPlay()
{
	m_group->stop();
	auto lastItem = m_items.takeAt(m_items.size() - 1);
	m_items.prepend(lastItem);
	play();
	m_group->start();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值