當(dāng)前位置:
首頁(yè) >
Qt中视图的缩放对应缩略图中矩形框的缩放
發(fā)布時(shí)間:2024/9/27
43
豆豆
生活随笔
收集整理的這篇文章主要介紹了
Qt中视图的缩放对应缩略图中矩形框的缩放
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
本文實(shí)現(xiàn)的目的是:視圖縮放時(shí),縮略圖中的矩形框也進(jìn)行縮放,而縮略圖中的矩形區(qū)域?yàn)橐晥D中的可見(jiàn)區(qū)。
獲取視圖中滾動(dòng)條的值,將其值與縮略圖所在的小窗口對(duì)比,可通過(guò)繪圖求其比例,再按比例縮小。
首先提供主要的代碼:
下面貼出自定義場(chǎng)景類
MyGraphicsScene.h
MyGraphicsScene.cpp
#include "MyGraphicsScene.h" #include <QGraphicsSceneMouseEvent> #include <QGraphicsRectItem> #include <QDebug>MyGraphicsScene::MyGraphicsScene(QObject *parent): QGraphicsScene(parent), m_bRectClicked(false) {m_pRectItem = new QGraphicsRectItem(0, 0, 0, 0);QPen penRectItem = QPen(QColor(255, 0, 0));penRectItem.setWidth(2);m_pRectItem->setPen(penRectItem);m_pRectItem->setZValue(1);addItem(m_pRectItem); }void MyGraphicsScene::onSetPreviewRect(QRect rect) {m_rectSaved = rect;// 內(nèi)縮幾個(gè)像素,用矩形外邊框來(lái)標(biāo)示viewport顯示區(qū)域m_pRectItem->setRect(rect.x() - 2/*+ 5*/, rect.y() - 2/*+ 5*/, rect.width() - 4, rect.height() - 4); }void MyGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent) {QGraphicsScene::mouseMoveEvent(mouseEvent);if (m_bRectClicked) {QPoint ptTopLeft = mouseEvent->scenePos().toPoint() - m_ptRectRelated;m_rectSaved.setTopLeft(ptTopLeft); // qDebug()<<"mouseMoveEvent";emit previewRectMoved(m_rectSaved);} }void MyGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) {QGraphicsScene::mousePressEvent(mouseEvent);if (m_rectSaved.contains(mouseEvent->scenePos().x(), mouseEvent->scenePos().y())) {m_bRectClicked = true;m_ptRectRelated = mouseEvent->scenePos().toPoint() - m_rectSaved.topLeft();} }void MyGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent) {QGraphicsScene::mouseReleaseEvent(mouseEvent);m_bRectClicked = false; }總結(jié)
以上是生活随笔為你收集整理的Qt中视图的缩放对应缩略图中矩形框的缩放的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: C++中常用字符串相关的编程题
- 下一篇: java+多线程菜鸟_java多线程