编程语言:C++
可视化平台:Qt
主要组件:QScrollArea,QLable
(1)用QScrollArea添加图片的代码如下:
QScrollArea scrollArea;
QLabel imageLabel;
QPixmap pixMap;
imageLabel.setPixmap(pixMap);
scrollArea.setWidget(imageLable);
(2)滚动区坐标与实际图片中坐标的转换
坐标转换公式为 QImage(X,Y)=QScroll Value(h,v)+QReal(x,y)
PImage(X,Y) 为图像的实际坐标, PScroll Value(h,v) 为水平、垂直滚动条的value()值构成的坐标, PReal(x,y) 为scrollArea的相对坐标。
这里有一个简单的例子可以解释ScrollBar的value()值和步长pageStep()之间的关系(摘自Qt Helper):
If we have a document with 100 lines, and we can only show 20 lines in a widget, we may wish to construct a scroll bar with a page step of 20, a minimum value of 0, and a maximum value of 80. This would give us a scroll bar with five “pages”.
获取QLable的相对坐标的方法可参考云鹤起舞的Qt获取鼠标位置(绝对位置、相对位置)一文
QPoint *qrealPoint;
qrealPoint= event->globalPos(); //获取全局位置,event为鼠标单击事件
qrealPoint= ui->leftImgLabel>mapFromGlobal(qrealPoint);
获取滚动条value()值的方法如下:
QScrollBar *scrollBar_X = scrollArea->horizontalScrollBar();
float x_offset = scrollBar_X->value();
此处用于实现不同QLable的单击选点事件处理,事件过滤器的主要使用方法如下:
.h文件中声明
bool eventFilter(QObject *obj, QEvent *eve);
.cpp构造函数中注册ui.sa_geoImage->installEventFilter(this);
.cpp中重写eventFilter()
方法
/**
* @BRIEF Reimplement eventFilter()
* @param *obj: Which object we should control
* @param *eve: Which event has happened
*/
bool MatchWindowSingle::eventFilter(QObject *obj, QEvent *eve)
{
if (obj == ui.sa_geoImage){ //Check whether it's a geoImage Scroll Area
if (selectPolicy == GEOIMAGE_SELECTING){ //Check whether geoImage can be selected
if (eve->type() == QEvent::MouseButtonRelease){ //Check mouse event
QMouseEvent *mouseEvent = static_cast(eve);
if (mouseEvent->button() == Qt::LeftButton){ // Left button click
//Get Image Position of the Select Point.
Point2f imagePoint = getImageCoordinate(mouseEvent, ui.sa_geoImage, &scaleFactor_g);
//Draw point in the screen.
int pointSize = (int)(2/scaleFactor_MIN_g);
int pix_x = (int)imagePoint.x;
int pix_y = (int)imagePoint.y;
QPen pen(QColor(250,112,154)); //Set pen
QBrush brush(QColor(250,112,154)); //Set brush
QPainter painter(&geoPic);
painter.setPen(pen);
painter.setBrush(brush);
painter.drawEllipse(QPoint(pix_x,pix_y), pointSize, pointSize);
painter.setRenderHint(QPainter::Antialiasing);//Make it more smooth
//Update the scrollArea.
updateScrollArea(&geoPic, ui.sa_geoImage, &geoImageLable, &scaleFactor_g);
selectPolicy = VIDEOIMAGE_SELECTING;
return true;
}else{return false;}
}else{return false;}
}else{return false;}
}else if (obj == ui.sa_videoImage){ //Check whether it's a videoImage Scroll Area
if (selectPolicy == VIDEOIMAGE_SELECTING){ //Check whether videoImage can be selected
if (eve->type() == QEvent::MouseButtonRelease){ //Check mouse event
QMouseEvent *mouseEvent = static_cast(eve);
if (mouseEvent->button() == Qt::LeftButton){ // Left button click
//Get Image Position of the Select Point.
Point2f imagePoint = getImageCoordinate(mouseEvent, ui.sa_videoImage, &scaleFactor_v);
//Draw point in the screen.
int pointSize = (int)(2/scaleFactor_MIN_v);
int pix_x = (int)imagePoint.x;
int pix_y = (int)imagePoint.y;
QPen pen(QColor(254,225,64)); //Set pen
QBrush brush(QColor(254,225,64)); //Set brush
QPainter painter(&videoPic);
painter.setPen(pen);
painter.setBrush(brush);
painter.drawEllipse(QPoint(pix_x,pix_y), pointSize, pointSize);
painter.setRenderHint(QPainter::Antialiasing);//Make it more smooth
//Update the scrollArea.
updateScrollArea(&videoPic, ui.sa_videoImage, &videoImageLable, &scaleFactor_v);
selectPolicy = GEOIMAGE_SELECTING;
return true;
}else{return false;}
}else{return false;}
}else{return false;}
}else{return false;}
}