QtWidget实现图片放大器功能

使用重写QtWidget 实现图片放大器功能

  • 话不多说直接上代码
    • 功能描述:该控件可同时使用两张图片进行比较,支持拖动以及放大等功能
    • 插入链接与图片

话不多说直接上代码

功能描述:该控件可同时使用两张图片进行比较,支持拖动以及放大等功能

#pragma once

#include 
#include "CImagePlayer.h"
#include "CGraphicsPixmapItem.h"
#include "CGraphicsScene.h"
#include "CGraphicsView.h"
#include "CPlayerImageControl.h"
#include 
class testWidget : public QWidget
{
	Q_OBJECT

public:
	testWidget(QWidget *parent = Q_NULLPTR);
	~testWidget();
	void initUI();
protected:
	virtual	void resizeEvent(QResizeEvent* event)override;
private:
	//CImagePlayer* m_playerLeft;
	//CImagePlayer* m_playerRight;
	CPlayerImageControl m_leftView;
	CPlayerImageControl m_rightView;
	QPushButton* m_btn;
};

#include "testWidget.h"
#include "CGraphicsPixmapItem.h"
#include "CGraphicsScene.h"
#include "CGraphicsScene.h"
#include 
#include 
testWidget::testWidget(QWidget *parent)
	: QWidget(parent)
{
	initUI();
}

testWidget::~testWidget()
{
}

void testWidget::initUI()
{
	//m_playerLeft = new CImagePlayer(this);
	//m_playerRight = new CImagePlayer(this);
	//m_playerLeft->setLinkFlag(true);
	//m_playerRight->setLinkFlag(true);
	//m_playerLeft->setLinkPlayer(m_playerRight);
	//m_playerRight->setLinkPlayer(m_playerLeft);
	QImage image("C:/Users/admin/Desktop/Painter/Painter_1/x64/Debug/121312.jpg");
	//m_playerLeft->setImage(image);
	//m_playerRight->setImage(image);
	/*CGraphicsScene* m_leftScene = new CGraphicsScene;
	m_leftScene->setSceneRect(0, 0, (this->width() - 10) / 2, this->height());
	CGraphicsScene* m_rightScene = new CGraphicsScene;
	m_rightScene->setSceneRect(0, 0, (this->width() - 10) / 2, this->height());
	CGraphicsPixmapItem* leftItem = new CGraphicsPixmapItem;
	leftItem->setImage(image);
	CGraphicsPixmapItem* rightItem = new CGraphicsPixmapItem;
	rightItem->setImage(image);
	leftItem->setLinkItem(rightItem);
	rightItem->setLinkItem(leftItem);
	m_leftScene->addItem(leftItem);
	m_rightScene->addItem(rightItem);
	m_leftView.setScene(m_leftScene);
	m_rightView.setScene(m_rightScene);*/
	m_leftView.setImage(image);
	m_rightView.setImage(image);
	m_leftView.setLinkCtrl(&m_rightView);
	m_rightView.setLinkCtrl(&m_leftView);
	QHBoxLayout* layout = new QHBoxLayout();
	layout->addWidget(&m_leftView);
	layout->addSpacing(10);
	layout->addWidget(&m_rightView);
	QHBoxLayout* hlayout = new QHBoxLayout;
	m_btn = new QPushButton;
	m_btn->setText(tr("change Image"));
	m_btn->setFixedHeight(40);
	hlayout->addWidget(m_btn);
	QVBoxLayout* Vlayout = new QVBoxLayout;
	Vlayout->addLayout(layout);
	Vlayout->addLayout(hlayout);
	this->setLayout(Vlayout);



	connect(m_btn, &QPushButton::clicked, this, [=]() {
		QImage image("C:/Users/admin/Desktop/Painter/Painter_1/x64/Debug/6539902911616.png");
		m_leftView.setImage(image);
		m_rightView.setImage(image);
		});
}

void testWidget::resizeEvent(QResizeEvent* event)
{
	/*QSize  size = event->size();
	m_leftView.scene()->setSceneRect(0, 0, (size.width() - 10) / 2, size.height());
	m_rightView.scene()->setSceneRect(0, 0, (size.width() - 10) / 2, size.height());
	*/
	QImage image("C:/Users/admin/Desktop/Painter/Painter_1/x64/Debug/121312.jpg");
	m_leftView.setImage(image);
	m_rightView.setImage(image);
	QWidget::resizeEvent(event);
}

#pragma once

#include 

class CPlayerImageControl : public QWidget
{
	Q_OBJECT

public:
	explicit CPlayerImageControl(QWidget *parent=nullptr);
	~CPlayerImageControl();

	void setImage(const QImage image);
	void setLinkCtrl(CPlayerImageControl* ctrl);
	void setLinkFlag(bool flag);

protected:
	virtual void mouseMoveEvent(QMouseEvent* event)override;
	virtual void mousePressEvent(QMouseEvent* event)override;
	virtual void mouseReleaseEvent(QMouseEvent* event)override;
	virtual void wheelEvent(QWheelEvent* event)override;
	virtual void paintEvent(QPaintEvent* event)override;
	virtual void resizeEvent(QResizeEvent* event)override;
private:
	double  m_scaleNum = 1.0;
	bool	m_mouseIsPress=false;
	bool	m_isLinkFlag=false;
	CPlayerImageControl* m_linkCtrl;
	QImage  m_image;
	QPointF m_endPt;
	QPointF m_lastPt;
	QPointF m_pixmapPt=QPointF(0,0);
	QPixmap m_pixmap;

};

#include "CPlayerImageControl.h"
#include 
#include 
#include 
#include 
CPlayerImageControl::CPlayerImageControl(QWidget *parent)
	: QWidget(parent)
{
}

CPlayerImageControl::~CPlayerImageControl()
{
}

void CPlayerImageControl::setImage(const QImage image)
{
	if (image.isNull()) {
		return;
	}
	m_image = image;
	m_pixmap = QPixmap::fromImage(m_image);
	m_pixmap = m_pixmap.scaled(this->width() * m_scaleNum, this->height() * m_scaleNum, Qt::KeepAspectRatio);
	int w = this->width();
	int pw = m_pixmap.width();
	int h = this->height();
	int ph = m_pixmap.height();
	m_pixmapPt = QPointF((this->width() - m_pixmap.width()) / 2, (this->height() - m_pixmap.height()) / 2);
	update();
}

void CPlayerImageControl::setLinkCtrl(CPlayerImageControl* ctrl)
{
	if (ctrl) {
		m_linkCtrl = ctrl;
		m_isLinkFlag = true;
	}
}

void CPlayerImageControl::setLinkFlag(bool flag)
{
	m_isLinkFlag = flag;
}

void CPlayerImageControl::mouseMoveEvent(QMouseEvent* event)
{
	if (m_mouseIsPress) {
		if (event->pos() != m_lastPt) {
			m_endPt = event->pos();
			QPointF temp= QPointF(m_pixmapPt.x() + (m_endPt.x() - m_lastPt.x()), m_pixmapPt.y() + (m_endPt.y() - m_lastPt.y()));
			if (((m_pixmap.rect().width() + temp.x()) > this->width()&&(temp.x()<=0))&&
				((m_pixmap.rect().height() + temp.y()) > this->height()&&  (temp.y() <= 0))) {
				m_pixmapPt = temp;
				m_lastPt = m_endPt;
			}
			update();
		}
	}
	if (m_isLinkFlag) {
		m_linkCtrl->setLinkFlag(false);
		m_linkCtrl->mouseMoveEvent(event);
		m_linkCtrl->setLinkFlag(true);
	}
	QWidget::mouseMoveEvent(event);
}

void CPlayerImageControl::mousePressEvent(QMouseEvent* event)
{
	if (event->button() == Qt::LeftButton) {
		m_mouseIsPress = true;
		if (m_lastPt != event->pos()) {
			m_lastPt = event->pos();
		}
	}
	if (m_isLinkFlag) {
		m_linkCtrl->setLinkFlag(false);
		m_linkCtrl->mousePressEvent(event);
		m_linkCtrl->setLinkFlag(true);
	}
	QWidget::mousePressEvent(event);
}

void CPlayerImageControl::mouseReleaseEvent(QMouseEvent* event)
{
	if (event->button() == Qt::LeftButton) {
		m_mouseIsPress = false;
		m_endPt = event->pos();
		update();
	}
	if (m_isLinkFlag) {
		m_linkCtrl->setLinkFlag(false);
		m_linkCtrl->mouseReleaseEvent(event);
		m_linkCtrl->setLinkFlag(true);
	}
	QWidget::mouseReleaseEvent(event);
}

void CPlayerImageControl::wheelEvent(QWheelEvent* event)
{

		if (event->delta() > 0)
		{
			if (m_scaleNum < 25.0) {
				m_scaleNum *= 1.2;
			}

		}
		else
		{
			if (m_scaleNum / 1.2 >= 1.0) {
				m_scaleNum /= 1.2;
			}
			if (m_scaleNum < 1.2) {
				m_scaleNum = 1.0;
			}
		}

		if (m_isLinkFlag) {
			m_linkCtrl->setLinkFlag(false);
			m_linkCtrl->wheelEvent(event);
			m_linkCtrl->setLinkFlag(true);
		}
		m_pixmap = QPixmap::fromImage(m_image);
		m_pixmap = m_pixmap.scaled(this->width() * m_scaleNum, this->height() * m_scaleNum, Qt::KeepAspectRatio);
		m_pixmapPt = QPointF(-(m_pixmap.rect().width() - this->width()) / 2, -(m_pixmap.rect().height() - this->height()) / 2);
		update();
		QWidget::wheelEvent(event);
}

void CPlayerImageControl::paintEvent(QPaintEvent* event)
{
	QPainter  painter(this);
	QBrush brush("black");
	painter.setBrush(brush);
	painter.drawRect(0, 0, this->width(), this->height());
	painter.drawPixmap(m_pixmapPt, m_pixmap);
}

void CPlayerImageControl::resizeEvent(QResizeEvent* event)
{
	QSize size = event->size();
	m_pixmap = QPixmap::fromImage(m_image);
	m_pixmap = m_pixmap.scaled(size.width() * m_scaleNum, size.height() * m_scaleNum, Qt::KeepAspectRatio);
	QWidget::resizeEvent(event);
}

插入链接与图片

链接: link.

图片:

你可能感兴趣的:(QT学习,qt,cocos2d,c++)