日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Qt中视图的缩放对应缩略图中矩形框的缩放

發布時間:2024/9/27 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt中视图的缩放对应缩略图中矩形框的缩放 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文實現的目的是:視圖縮放時,縮略圖中的矩形框也進行縮放,而縮略圖中的矩形區域為視圖中的可見區。
獲取視圖中滾動條的值,將其值與縮略圖所在的小窗口對比,可通過繪圖求其比例,再按比例縮小。
首先提供主要的代碼:

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<<"小矩形坐標及大小:"<<"("<<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); }//縮略圖中的自定義場景 void MyGraphicsScene::onSetPreviewRect(QRect rect) {m_rectSaved = rect;// 內縮幾個像素,用矩形外邊框來標示viewport顯示區域m_pRectItem->setRect(rect.x() - 2/*+ 5*/, rect.y() - 2/*+ 5*/, rect.width() - 4, rect.height() - 4);//設置圖形項矩形 }

下面貼出自定義場景類
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; // 鼠標點擊時,相對于紅色矩形框的位置 };

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;// 內縮幾個像素,用矩形外邊框來標示viewport顯示區域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; }

總結

以上是生活随笔為你收集整理的Qt中视图的缩放对应缩略图中矩形框的缩放的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。