日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(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ò)繪圖求其比例,再按比例縮小。
首先提供主要的代碼:

connect(view->verticalScrollBar(),&QScrollBar::valueChanged,this,&chunzhongForm::slot_VvalueChanged); connect(view->horizontalScrollBar(),&QScrollBar::valueChanged,this,&chunzhongForm::slot_HvalueChanged);void chunzhongForm::slot_VvalueChanged(int value) {if(value != 0 && dlg != NULL){m_y = value;if(dlg->isVisible()){emit signalSetDrawRectSize();}} }void chunzhongForm::slot_HvalueChanged(int value) {if(value != 0 && dlg != NULL){m_x = value;if(dlg->isVisible()){emit signalSetDrawRectSize();}} }connect(this,&chunzhongForm::signalSetDrawRectSize,this,&chunzhongForm::slot_setViewRect);QSize GraphicsView::viewportSizeHint() {return viewport()->size(); }void chunzhongForm::slot_setViewRect() {m_viewSize = view->viewportSizeHint();//獲取視口大小int x = m_x / (m_viewSize.width() * m_scale)* SMALL_W;int y = m_y / (m_viewSize.height() * m_scale)* SMALL_H;int wid = SMALL_W / m_scale;int hei = SMALL_H / m_scale;outPut<<"小矩形坐標(biāo)及大小:"<<"("<<m_x<<" ,"<<m_y<<" ,"<<wid<<" ,"<<hei<<")";//換成qDebug()輸出QRect rect(x,y,wid,hei);emit signalDrawRect(rect); } connect(this,&chunzhongForm::signalDrawRect,dlg,&BreviaryDlg::slot_setRectSize);//縮略圖窗口類BreviaryDlg void BreviaryDlg::slot_setRectSize(QRect &rect) {m_rect = rect;scene->onSetPreviewRect(rect); }//縮略圖中的自定義場(chǎng)景 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);//設(shè)置圖形項(xiàng)矩形 }

下面貼出自定義場(chǎng)景類
MyGraphicsScene.h

#pragma once//#include <vld.h> #include <QGraphicsScene>class MyGraphicsScene : public QGraphicsScene {Q_OBJECTpublic:MyGraphicsScene(QObject *parent = nullptr);virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);virtual void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);Q_SIGNALS:void previewRectMoved(QRect rect);public Q_SLOTS:void onSetPreviewRect(QRect rect);private:QGraphicsRectItem* m_pRectItem;QRect m_rectSaved;bool m_bRectClicked;QPoint m_ptRectRelated; // 鼠標(biāo)點(diǎn)擊時(shí),相對(duì)于紅色矩形框的位置 };

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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。