Qt实现爱奇艺轮播图效果

cpp文件 

/////////////////////////////////////////////////////////////////////////
PhotoWidget::PhotoWidget(QWidget *parent)
	:QWidget(parent)
{

	this->resize(800 - 28 * 2, 190);//0,72
	totalWidget = new QWidget(this);
	timer = new QTimer(this);
	connect(timer, &QTimer::timeout, [this]()
	{
		if (wideget_move)
		{
			int newIndex = current_index + 1;
			setPhoto(newIndex);
		}
	});
}

PhotoWidget::~PhotoWidget()
{

}

bool PhotoWidget::eventFilter(QObject *target, QEvent *e)
{
	if (e->type() == QEvent::MouseButtonRelease)
	{
		for (int i = 0; i < labelListPhoto.size(); i++)
		{
			QLabel *label = labelListPhoto.at(i);
			if (label == target)
			{
				if (current_index != i)
				{
					if (wideget_move)
					{
						setPhoto(i);
					}
				}
				else
				{
					if (urlList.size() >= i)
					{
						QString url = urlList.at(i);
						QString fileName = photoList.at(i);
						QFileInfo filaInfo(fileName);
						BigDataInterface::GetInstance()->socketPost(QString("%1,%2,%3,%4").arg("M00").arg("OP001").arg("AFP002").arg(filaInfo.fileName()));
						if (url.startsWith("http"))//cate_id = 1096, app_id = 1000
						{
							QDesktopServices::openUrl(QUrl::fromUserInput(url));
						}
						else
						{
							QStringList urlList = url.split(",");
							QString cate_id = "";
							QString app_id = "";
							if (urlList.size() == 2)
							{
								foreach(QString str, urlList)
								{
									//做一些点击操作
								}
							}
							if (!cate_id.isEmpty() && !app_id.isEmpty())
							{
								//做一些点击操作
							}
						}	
					}
				}
				break;
			}
		}
	}
	return QWidget::eventFilter(target, e);
}

void PhotoWidget::keyPressEvent(QKeyEvent *event)
{
	if (wideget_move) {
		switch (event->key()) {
		case Qt::Key_Left:
		case Qt::Key_Up:
		{
			//if (current_index > 1) {
			int newIndex = current_index - 1;
			setPhoto(newIndex);
			//	}
			break;
		}
		case Qt::Key_Right:
		case Qt::Key_Down:
		{
			//	if (current_index < labelListPhoto.size() - 2) {
			int newIndex = current_index + 1;
			setPhoto(newIndex);
			//	}
			break;
		}
		default:
			break;
		}
	}
}

void PhotoWidget::setPhoto(int &newLabelIndex)
{
	setLabelMove(false);
	if (newLabelIndex < labelListPhoto.size())
	{
		//if (newLabelIndex == 0)
		//{
		//	newLabelIndex = labelListPhoto.size() - 2;
		//}
		//else if (newLabelIndex == labelListPhoto.size() - 1)
		//{
		//	newLabelIndex = 1;
		//}

		if (newLabelIndex == 0 || newLabelIndex == labelListPhoto.size() - 1)
		{
			newLabelIndex = newLabelIndex == 2 ? 1 : labelListPhoto.size() - 3;
			setLabelMove(true);
			return;
		}
		
		//之前的 lable 复原
		QLabel *oldLabel = labelListPhoto.at(current_index);
		if (oldLabel->y() == 22)
		{
			oldLabel->setGeometry(oldLabel->x() + 71, oldLabel->y() + 10, 308, 126);
			oldLabel->setPixmap(QPixmap(photoList.at(current_index)).scaled(308, 126));
		}

		//新的label  突出
		QLabel *chooseLabel = labelListPhoto.at(newLabelIndex);
		chooseLabel->setGeometry(chooseLabel->x() - 71, chooseLabel->y() - 10, 450, 146);
		chooseLabel->setPixmap(QPixmap(photoList.at(newLabelIndex)));
		labelListPhoto.at(newLabelIndex - 1)->raise();
		labelListPhoto.at(newLabelIndex + 1)->raise();
		chooseLabel->raise();

		int oleLabelIndex = current_index;
		current_index = newLabelIndex;
		moveCurrentPage(oleLabelIndex < current_index);
		emit chooseLabelSignal(current_index);
	}
	setLabelMove(true);
}

void PhotoWidget::addPhoto(QString photo, QString url)
{
	photoList.append(photo);
	urlList.append(url);
	QLabel *label = new QLabel(totalWidget);
	label->setAlignment(Qt::AlignHCenter);
	label->installEventFilter(this);
	QPixmap pixmap = QPixmap(photo);

	//QPixmap temp(pixmap.size());
	//temp.fill(Qt::transparent);
	//QPainter p1(&temp);
	//p1.setCompositionMode(QPainter::CompositionMode_Source);
	//p1.drawPixmap(0, 0, pixmap);
	//p1.setCompositionMode(QPainter::CompositionMode_DestinationIn);
	//p1.fillRect(temp.rect(), QColor(255, 255, 255, 60));
	//p1.end();
	//pixmap = temp;

	label->setPixmap(pixmap);
	labelListPhoto.append(label);
	resetInterface();
}

void PhotoWidget::addFlish()
{
	if (labelListPhoto.size() >= 3)
	{
		photoList.insert(0, photoList.last());
		photoList.insert(0, photoList.at(photoList.size() - 2));
		urlList.insert(0, urlList.last());
		urlList.insert(0, urlList.at(urlList.size() - 2));

		photoList.append(photoList.at(2));
		photoList.append(photoList.at(3));
		urlList.append(urlList.at(2));
		urlList.append(urlList.at(3));

		//前边添加 倒数第一张 和倒数第二张
		QLabel *label = new QLabel(totalWidget);
		label->setAlignment(Qt::AlignHCenter);
		label->installEventFilter(this);
		label->setPixmap(QPixmap(photoList.at(1)));
		labelListPhoto.insert(0, label);

		QLabel *label2 = new QLabel(totalWidget);
		label2->setAlignment(Qt::AlignHCenter);
		label2->installEventFilter(this);
		label2->setPixmap(QPixmap(photoList.at(0)));
		labelListPhoto.insert(0, label2);

	

		//后半添加 第一张和第二张
		QLabel *labelFilst = new QLabel(totalWidget);
		labelFilst->setAlignment(Qt::AlignHCenter);
		labelFilst->installEventFilter(this);
		labelFilst->setPixmap(QPixmap(photoList.at(photoList.size() - 2)));
		labelListPhoto.append(labelFilst);

		QLabel *labelFilst2 = new QLabel(totalWidget);
		labelFilst2->setAlignment(Qt::AlignHCenter);
		labelFilst2->installEventFilter(this);
		labelFilst2->setPixmap(QPixmap(photoList.last()));
		labelListPhoto.append(labelFilst2);
		resetInterface();
	}
	timer->start(8000);
}

void PhotoWidget::resetInterface()
{
	update();//450*146 
	totalWidget->setGeometry(0, 0, labelListPhoto.size() * 218 + 90, this->height());
	for (int i = 0; i < labelListPhoto.size(); ++i)
	{
		labelListPhoto[i]->setGeometry(QRect(i * 218, 32, 308, 126));
		labelListPhoto[i]->show();
	}
	totalWidget->show();
	//moveCurrentPage(false);
}

void PhotoWidget::setLabelMove(bool enable)
{
	wideget_move = enable;
}

void PhotoWidget::moveCurrentPage(bool direction)
{
	
	int current_pos_x = totalWidget->x();    //label position
	int dest_pos_x = -218 * current_index + 218;
	if (direction) {
		while (current_pos_x > dest_pos_x) {
			totalWidget->move(current_pos_x - 3, 0);
			current_pos_x = totalWidget->x();
			qApp->processEvents(QEventLoop::AllEvents);
		}
	}
	else {
		while (current_pos_x < dest_pos_x) {
			totalWidget->move(current_pos_x + 3, 0);
			current_pos_x = totalWidget->x();
			qApp->processEvents(QEventLoop::AllEvents);
		}
	}
	

	
	if (current_index == 1)
	{
		//选的是第一张 前面的一张
		//突出最后一张
		QLabel *oldLabel = labelListPhoto.at(current_index);
		if (oldLabel->y() == 22)
		{
			oldLabel->setGeometry(oldLabel->x() + 71, oldLabel->y() + 10, 308, 126);
			oldLabel->setPixmap(QPixmap(photoList.at(current_index)).scaled(308, 126));
		}
		current_index = labelListPhoto.size() - 3;
		//新的label  突出
		QLabel *chooseLabel = labelListPhoto.at(current_index);
		chooseLabel->setGeometry(chooseLabel->x() - 71, chooseLabel->y() - 10, 450, 146);
		chooseLabel->setPixmap(QPixmap(photoList.at(current_index)));
		labelListPhoto.at(current_index - 1)->raise();
		labelListPhoto.at(current_index + 1)->raise();
		chooseLabel->raise();
		
	}
	else if (current_index == labelListPhoto.size() - 2)
	{
		//选择的是最后一张的 后边一张
		//突出第一张
		QLabel *oldLabel = labelListPhoto.at(current_index);
		if (oldLabel->y() == 22)
		{
			oldLabel->setGeometry(oldLabel->x() + 71, oldLabel->y() + 10, 308, 126);
			oldLabel->setPixmap(QPixmap(photoList.at(current_index)).scaled(308, 126));
		}

		current_index = 2;
		//新的label  突出
		QLabel *chooseLabel = labelListPhoto.at(current_index);
		chooseLabel->setGeometry(chooseLabel->x() - 71, chooseLabel->y() - 10, 450, 146);
		chooseLabel->setPixmap(QPixmap(photoList.at(current_index )));
		labelListPhoto.at(current_index - 1)->raise();
		labelListPhoto.at(current_index + 1)->raise();
		chooseLabel->raise();
	}
	totalWidget->move(-218 * current_index + 218, 0);
	
	//this->setFocus();
}

void PhotoWidget::clearLabel()
{
	while (!wideget_move)
		Sleep(1);
	setLabelMove(false);
	timer->stop();
	photoList.clear();
	urlList.clear();
	foreach(QLabel *label, labelListPhoto)
	{
		label->deleteLater();
	}
	labelListPhoto.clear();
	current_index = 1;
	setLabelMove(true);
}

 

头文件

class PhotoWidget : public QWidget
{
	Q_OBJECT
public:
	PhotoWidget(QWidget *parent);
	~PhotoWidget();
	void clearLabel();
protected:
	void keyPressEvent(QKeyEvent *event);
	bool eventFilter(QObject *target, QEvent *e);
private:
	void moveCurrentPage(bool direction);
	//开始移动标志位
	void setLabelMove(bool enable);
	void resetInterface();
signals:
	void chooseLabelSignal(int);
	void openAppParticularsSig(QString);
private:
	QWidget *totalWidget;
	QTimer *timer;
	bool wideget_move = true;
	int current_index = 1;
	QList<QString> photoList;
	QList<QString> urlList;
	QList<QLabel* > labelListPhoto;
public:
	void addPhoto(QString photo, QString url);
	void setPhoto(int &newLabel);
	void addFlish();

};

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值