#include "CustomGraphicsView.h"
#include <QMutexLocker>
#include <QLayout>
#include <QWheelEvent>
#include "CustomImageItem.h"
#include <QRandomGenerator>
#define ZOOMMAX 50 //最大放大倍数
#define ZOOMMIN 0.02 //最小缩小倍数
CustomGraphicsView::CustomGraphicsView(QWidget *parent)
: QGraphicsView(parent)
, m_pScene(Q_NULLPTR)
, m_pImageItem(Q_NULLPTR)
, m_pPosInfoWidget(Q_NULLPTR)
, m_pPosInfoLabel(Q_NULLPTR)
{
this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->setRenderHint(QPainter::Antialiasing);
this->setTransformationAnchor(QGraphicsView::AnchorViewCenter);
this->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
this->setSceneRect(INT_MIN/2, INT_MIN/2, INT_MAX, INT_MAX);
this->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
this->setResizeAnchor(QGraphicsView::AnchorUnderMouse);
this->setAttribute(Qt::WA_TranslucentBackground);
//this->setStyleSheet(QString::fromUtf8("#widget_2{border:3px solid red}"));
//this->setStyleSheet(QString::fromUtf8("border:0px solid black"));
setBackground(true,false);
centerOn(0, 0);
if(false == InitWidget())
{
throw std::bad_alloc();
}
}
CustomGraphicsView::~CustomGraphicsView()
{
}
bool CustomGraphicsView::InitWidget()
{
//创建变量对象
m_pScene = new QGraphicsScene(this);
m_pImageItem = new CustomImageItem(this);
m_pImageItem->setAcceptHoverEvents(true);
m_pImageItem->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);
this->setScene(m_pScene);
m_pScene->addItem(m_pImageItem);
m_pPosInfoLabel = new QLabel(this);
m_pPosInfoWidget = new QWidget(this);
//在视觉窗口下方显示鼠标坐标以及图像的灰度值
m_pPosInfoLabel->setStyleSheet("color:rgb(200,255,200); "
"background-color:rgba(50,50,50,50); "
"font: Microsoft YaHei;"
"font-size: 12px;");
m_pPosInfoLabel->setText(" W:0,H:0 | X:0,Y:0 | R:0,G:0,B:0");
//显示区域窗口
m_pPosInfoWidget->setFixedHeight(20);
m_pPosInfoWidget->setGeometry(0, this->height() - 20, this->width(), 20);
m_pPosInfoWidget->setStyleSheet("background-color:rgba(0,0,0,0);");
QHBoxLayout* pInfoLayout = new QHBoxLayout();
pInfoLayout->setSpacing(0);
pInfoLayout->setContentsMargins(1,1,1,1);
pInfoLayout->addWidget(m_pPosInfoLabel);
// pInfoLayout->addStretch();
m_pPosInfoWidget->setLayout(pInfoLayout);
m_pPosInfoWidget->setAttribute(Qt::WA_TransparentForMouseEvents);
//初始化信号槽
connect(m_pImageItem, &CustomImageItem::RGBValue, this, [&](QString InfoVal) {
m_pPosInfoLabel->setText(InfoVal);
});
return true;
}
//为视觉窗口设置图像,是一个公共对外接口
void CustomGraphicsView::SetImage(const QImage &image)
{
static QMutex mutex;
QMutexLocker locker(&mutex);
m_qImage = image.copy();
m_Image = QPixmap::fromImage(image);
m_pImageItem->w = m_Image.width();
m_pImageItem->h = m_Image.height();
m_pImageItem->setPixmap(m_Image);
fitFrame();
onCenter();
show();
}
//重写鼠标滚轮滚动的事件函数
//主要依赖于Zoom()方法
void CustomGraphicsView::wheelEvent(QWheelEvent *event)
{
//滚轮的滚动量
QPoint scrollAmount = event->angleDelta();
if ((scrollAmount.y() > 0) && (m_dZoomValue >= ZOOMMAX)) //最大放大到原始图像的50倍
{
return;
}
else if ((scrollAmount.y() < 0) && (m_dZoomValue <= ZOOMMIN))//最小缩小到原始图像的50倍
{
return;
}
// 正值表示滚轮远离使用者,为放大;负值表示朝向使用者,为缩小
scrollAmount.y() > 0 ? onZoom(1.2f) : onZoom(0.8f);
}
// //在视觉窗口上双击鼠标左键,会有图像居中效果,主要依赖于onCenter()方法。
// void CustomGraphicsView::mouseDoubleClickEvent(QMouseEvent *event)
// {
// if(event->button() == Qt::LeftButton)
// {
// //自适应图像大小至视觉窗口的大小
// fitFrame();
// //居中显示
// onCenter();
// }
// QGraphicsView::mouseDoubleClickEvent(event);
// }
//绘制函数,用于视觉窗口背景绘制
void CustomGraphicsView::paintEvent(QPaintEvent* event)
{
QPainter paint(this->viewport());
//绘制背景
paint.drawTiledPixmap(QRect(QPoint(0, 0), QPoint(this->width(), this->height())), m_tilePixmap);
QGraphicsView::paintEvent(event);
//qDebug()<<QRandomGenerator::global()->bounded(100.0); //生成一个0和10.123之间的浮点数
}
//当窗口尺寸发生变化时,实时更新视觉窗口位置
void CustomGraphicsView::resizeEvent(QResizeEvent *event)
{
fitFrame();
onCenter();
m_pPosInfoWidget->setGeometry(0, this->height() - 20, this->width(), 20);
QGraphicsView::resizeEvent(event);
}
//视图居中
void CustomGraphicsView::onCenter()
{
//调用QGraphicsView自带的方法centerOn,使视觉窗口的中心位于图像元素的中心点
//并设置m_pImageItem的坐标,使其也位于中心点
this->centerOn(0,0);
m_pImageItem->setPos(-m_pImageItem->pixmap().width()/2,-m_pImageItem->pixmap().height()/2);
}
void CustomGraphicsView::onZoom(float scaleFactor)
{
//记录下当前相对于图像原图的缩放比例,可以记录下当前图像真实放大缩小了多少倍
//可以借此来限制图像的最大或最小缩放比例
m_dZoomValue *= scaleFactor;
//调用视图类QGraphicsView自带的scale缩放方法,来对视图进行缩放,实现放大缩小的功能
//缩放的同时,视图里的所有元素也会进行缩放,也就达到了视觉窗口放大缩小的效果
this->scale(scaleFactor, scaleFactor);
}
void CustomGraphicsView::FitImage()
{
qDebug()<<"触发右键自适应图像";
//自适应图像大小至视觉窗口的大小
fitFrame();
//居中显示
onCenter();
}
//图片自适应方法,根据图像原始尺寸和当前视觉窗口的大小计算出应缩放的尺寸,再根据已经缩放的比例计算还差的缩放比例,
//补齐应缩放的比例,使得图像和视觉窗口大小相适配
void CustomGraphicsView::fitFrame()
{
if (this->width() < 1 || m_Image.width() < 1)
return;
//计算缩放比例
double winWidth = this->width();
double winHeight = this->height();
double ScaleWidth = (m_Image.width() + 1) / winWidth;
double ScaleHeight = (m_Image.height() + 1) / winHeight;
double s_temp = ScaleWidth >= ScaleHeight ? 1 / ScaleWidth : 1 / ScaleHeight;
double scale = s_temp / m_dZoomValue;
if ((scale >= ZOOMMAX) || (scale <= ZOOMMIN)) //最大放大到原始图像的50倍
{
return;
}
onZoom(scale);
m_dZoomValue = s_temp;
}
//设置视觉窗口背景为棋盘格样式
void CustomGraphicsView::setBackground(bool enabled, bool invertColor)
{
if (enabled)
{
m_tilePixmap.fill(invertColor ? QColor(220, 220, 220) : QColor(35, 35, 35));
QPainter tilePainter(&m_tilePixmap);
constexpr QColor color(50, 50, 50, 255);
constexpr QColor invertedColor(210, 210, 210, 255);
tilePainter.fillRect(0, 0, 18, 18, invertColor ? invertedColor : color);
tilePainter.fillRect(18, 18, 18, 18, invertColor ? invertedColor : color);
tilePainter.end();
//当取消注释时,视觉窗口背景格会跟随图像一起缩放
//setBackgroundBrush(m_tilePixmap);
}
else
{
//setBackgroundBrush(Qt::transparent);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
qt创建的图片显示控件dll,通过其他程序调用的实现
共1476个文件
idx:1388个
obj:18个
h:15个
需积分: 0 0 下载量 194 浏览量
2024-05-15
23:07:16
上传
评论
收藏 10.09MB RAR 举报
温馨提示
qt创建的图片显示控件dll,通过其他程序调用的实现
资源推荐
资源详情
资源评论
收起资源包目录
qt创建的图片显示控件dll,通过其他程序调用的实现 (1476个子文件)
CustomGraphicsView.cpp 8KB
moc_imagewatcher.cpp 6KB
moc_imagewatcher.cpp 5KB
moc_CustomImageItem.cpp 5KB
moc_CustomImageItem.cpp 5KB
moc_CustomGraphicsView.cpp 4KB
moc_CustomGraphicsView.cpp 4KB
moc_mainwindow.cpp 3KB
moc_mainwindow.cpp 3KB
imagewatcher.cpp 3KB
mainwindow.cpp 2KB
CustomImageItem.cpp 1KB
main.cpp 183B
Makefile.Debug 96KB
Makefile.Debug 96KB
Makefile.Debug 64KB
Makefile.Debug 64KB
ImageWatcher.dll 135KB
ImageWatcher.dll 135KB
ImageWatcher.dll 60KB
ImageWatcher.dll 60KB
test_dll.exe 99KB
test_dll.exe 36KB
ImageWatcher.exp 5KB
ImageWatcher.exp 5KB
ui_mainwindow.h 3KB
ui_mainwindow.h 2KB
CustomGraphicsView.h 2KB
CustomGraphicsView.h 2KB
imagewatcher.h 1KB
imagewatcher.h 1KB
CustomImageItem.h 701B
CustomImageItem.h 701B
mainwindow.h 678B
ImageWatcher_global.h 266B
ImageWatcher_global.h 266B
moc_predefs.h 261B
moc_predefs.h 261B
moc_predefs.h 244B
moc_predefs.h 244B
random.48589B0C317EA8F7.idx 278KB
random.48589B0C317EA8F7.idx 266KB
xstring.689CECAA65E37F50.idx 160KB
xstring.689CECAA65E37F50.idx 160KB
qnamespace.h.DD2B864177A0CAB8.idx 151KB
qnamespace.h.DD2B864177A0CAB8.idx 151KB
qnamespace.h.DD2B864177A0CAB8.idx 151KB
qnamespace.h.DD2B864177A0CAB8.idx 151KB
emmintrin.h.8A4F82278D0E6F5D.idx 149KB
emmintrin.h.8A4F82278D0E6F5D.idx 149KB
emmintrin.h.8A4F82278D0E6F5D.idx 149KB
emmintrin.h.8A4F82278D0E6F5D.idx 149KB
algorithm.FCF63B217112925A.idx 146KB
algorithm.FCF63B217112925A.idx 146KB
algorithm.FCF63B217112925A.idx 145KB
algorithm.FCF63B217112925A.idx 145KB
qmetatype.h.F5B7AA88FE1032F2.idx 141KB
qmetatype.h.F5B7AA88FE1032F2.idx 141KB
qmetatype.h.F5B7AA88FE1032F2.idx 141KB
qmetatype.h.F5B7AA88FE1032F2.idx 141KB
xstring.689CECAA65E37F50.idx 139KB
xstring.689CECAA65E37F50.idx 139KB
qhash.h.3DC812ACC5517951.idx 114KB
qhash.h.3DC812ACC5517951.idx 114KB
vector.E16DE3738CBEF331.idx 114KB
vector.E16DE3738CBEF331.idx 114KB
qhash.h.3DC812ACC5517951.idx 113KB
qhash.h.3DC812ACC5517951.idx 113KB
filesystem.1A18B40573940108.idx 98KB
filesystem.1A18B40573940108.idx 98KB
qlocale.h.5264DD9FC2A54B38.idx 90KB
qlocale.h.5264DD9FC2A54B38.idx 90KB
qlocale.h.5264DD9FC2A54B38.idx 90KB
qlocale.h.5264DD9FC2A54B38.idx 90KB
vector.E16DE3738CBEF331.idx 88KB
vector.E16DE3738CBEF331.idx 88KB
qstring.h.2344862353850B00.idx 84KB
qstring.h.2344862353850B00.idx 84KB
qstring.h.2344862353850B00.idx 84KB
xmmintrin.h.51FE2590DEC9AAC4.idx 84KB
xmmintrin.h.51FE2590DEC9AAC4.idx 84KB
qstring.h.2344862353850B00.idx 84KB
xmmintrin.h.51FE2590DEC9AAC4.idx 84KB
xmmintrin.h.51FE2590DEC9AAC4.idx 84KB
qvarlengtharray.h.70C8BB705668D3A6.idx 83KB
qvarlengtharray.h.70C8BB705668D3A6.idx 83KB
qvarlengtharray.h.70C8BB705668D3A6.idx 83KB
qvarlengtharray.h.70C8BB705668D3A6.idx 83KB
qflags.h.25435A037D26E237.idx 79KB
qlist.h.2E2BC49DAF8C58AF.idx 74KB
qlist.h.2E2BC49DAF8C58AF.idx 74KB
qlist.h.2E2BC49DAF8C58AF.idx 73KB
qlist.h.2E2BC49DAF8C58AF.idx 73KB
xlocnum.A27198031873DC1C.idx 73KB
xlocnum.A27198031873DC1C.idx 73KB
cmath.3480AD3F5B8E5F83.idx 70KB
cmath.3480AD3F5B8E5F83.idx 70KB
cmath.3480AD3F5B8E5F83.idx 70KB
cmath.3480AD3F5B8E5F83.idx 70KB
qmap.h.D3477E1DE3D6FF80.idx 69KB
共 1476 条
- 1
- 2
- 3
- 4
- 5
- 6
- 15
资源评论
Blues_vision
- 粉丝: 10
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功