本文主要是介绍Qt 实现 RubberBandDrag 框选,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1 效果
QGraphicsView 框架提供一个默认交互 RubberBandDrag 。可以用了实现 QGraphicsView里的局部调整(放大、图像处理)、局部选中效果。
- 局部调整(根据选中范围内HU值设定整体床位窗宽)
- 局部选中
2 实现
自定义QGraphicsView,重写mousePressEvent、mouseMoveEvent、mouseReleaseEvent。这个框选操作无论是左键右键还是滚轮都可以。所以MouseHandle无论那个按键都可以。
/*** @brief DicomImageView::mousePressEvent* 单机* @param event*/
void DicomImageView::mousePressEvent(QMouseEvent *event) {emit Signal_ViewClicked(this);if (!m_series_) { // 没有 Series InstanceQGraphicsView::mousePressEvent(event);return;}// 按键判断if (event->button() == Qt::RightButton) { // 鼠标右键MousePressHandle(event, mouse_right_state_.state, mouse_right_state_.type);} else if (event->button() == Qt::MiddleButton) { // 鼠标中键MousePressHandle(event, mouse_mid_state_.state, mouse_mid_state_.type);} else if (event->button() == Qt::LeftButton) { // 鼠标左键MousePressHandle(event, mouse_left_state_.state, mouse_left_state_.type);}prev_mouse_pos_ = event->pos();QGraphicsView::mousePressEvent(event);
}/*** @brief DicomImageView::mouseMoveEvent* 移动* @param event*/
void DicomImageView::mouseMoveEvent(QMouseEvent *event) {if (!m_series_) { // 没有 Series InstanceQGraphicsView::mouseMoveEvent(event);return;}// 显示 当前位置和灰度if (pos_value_item_) {PosValueShow(event);}// 按键判断if (event->buttons() & Qt::RightButton) { // 鼠标右键拖动MouseMoveHandle(event, mouse_right_state_.state);} else if (event->buttons() & Qt::MiddleButton) { // 鼠标中键拖动MouseMoveHandle(event, mouse_mid_state_.state);} else if (event->buttons() & Qt::LeftButton) { // 鼠标左键拖动MouseMoveHandle(event, mouse_left_state_.state);} else {QGraphicsView::mouseMoveEvent(event);}prev_mouse_pos_ = event->pos();
}/*** @brief DicomImageView::mouseReleaseEvent* 松开* @param event*/
void DicomImageView::mouseReleaseEvent(QMouseEvent *event) {if (!m_series_) { // 没有 Series InstanceQGraphicsView::mouseReleaseEvent(event);return;}// 按键判断if (event->button() == Qt::RightButton) { // 鼠标右键MouseReleaseHandle(event, mouse_right_state_.state);} else if (event->button() == Qt::MiddleButton) { // 鼠标中键MouseReleaseHandle(event, mouse_mid_state_.state);} else if (event->button() == Qt::LeftButton) { // 鼠标左键MouseReleaseHandle(event, mouse_left_state_.state);}QGraphicsView::mouseReleaseEvent(event);
}
/*** @brief DicomImageView::MousePressHandle* @param event* @param state* @param type*/
void DicomImageView::MousePressHandle(QMouseEvent *event,const CurrentState &state,const DicomImageView::DrawingType &type) {switch (state) {case ROIWindow:setDragMode(QGraphicsView::RubberBandDrag);break;default:break;}
}/*** @brief DicomImageView::MouseMoveHandle* @param event* @param state*/
void DicomImageView::MouseMoveHandle(QMouseEvent *event, const CurrentState &state) {QPointF sp = mapToScene(event->pos());// 鼠标坐标(映射到场景)QPointF ip = pixmap_item_->mapFromScene(sp);// 图片坐标switch (state) { case ROIWindow: {QGraphicsView::mouseMoveEvent(event);break;} }
}/*** @brief DicomImageView::MouseReleaseHandle* @param state*/
void DicomImageView::MouseReleaseHandle(QMouseEvent *, const CurrentState &state) {switch (state) { case ROIWindow: {if (m_scene_->selectedItems().size() == 0) {if (rubberBandRect().isValid()) {// 局部调整m_series_->SetRoiWindow(pixmap_item_->mapFromScene(mapToScene(rubberBandRect())).boundingRect());RefreshPixmap();}// 局部选中foreach (AbstractPathItem *it, item_list_) {if (!it->pixInfoUpdated()) {it->recalPixInfo(m_series_->GetCurrImageInstance(m_vtype)->GetDcmImage());m_scene_->update();}}}break;} }
}
这篇关于Qt 实现 RubberBandDrag 框选的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!