티스토리 뷰

C | C++

QGraphicsView 클래스(QT C++)

야라바 2024. 3. 13. 12:24
728x90

QGraphicsView 클래스는 QGraphicsScene 기반으로 만들어진 2D 그래픽을 스크롤 가능한 뷰로 출력하는 역할을 수행한다. QWidget> QFrame> QAbstractScrollArea의 계층 구조로 상속받는다. 뷰 영역의 크기에 따라서 자동적으로 스크롤바를 표시한다.

 

참조 링크 : https://doc.qt.io/qt-5/qgraphicsview.html

                  https://doc.qt.io/qt-5/qtwidgets-graphicsview-diagramscene-example.html

 

QGraphicsScene *scene = new QGraphicsScene();
scene->addText("Hello, world!");

QGraphicsView *view = new QGraphicsView(scene);
view->show();

 

QGraphicsView 클래스 인스턴스의 생성은 위의 예제처럼 QGraphicsScene 오브젝트의 포인터로 할 수도 있고 인스턴스 생성 이후에 setScene()으로 설정해도 된다.

 

 

위의 그림은 QT Designer의 QGraphicsView에 대한 속성들로 이를 다루는 주요 메서드들은 다음과 같다.

 

QBrush backgroundBrush()
void setBackgroundBrush(const QBrush &brush) 

QBrush foregroundBrush() 
void setForegroundBrush(const QBrush &brush) : 전경색 및 배경색 조회 및 설정. drawForeground()와 연관된다.

bool isInteractive() 
void setInteractive(bool allowed)
 : 읽기 전용인지 마우스와 키보드 이벤트를 받을지 조회 및 설정. 기본은 true

Qt::Alignment alignment() 
void setAlignment(Qt::Alignment alignment)
 : 전체 씬이 보일 때 뷰 내에서 어떻게 정렬할지 조회 및 설정.

QPainter::RenderHints renderHints() 
void setRenderHints(QPainter::RenderHints hints)
 : 항목 렌더링 옵션 조회 및 설정. 
    QPainter::Antialiasing, TextAntialiasing, SmoothPixmapTransform, LosslessImageRendering 등을 설정할 수 있다.

QGraphicsView::CacheMode cacheMode() 
void setCacheMode(QGraphicsView::CacheMode mode)
 : 기본은 캐시 하지 않는 것이고, 캐시로 성능 확보 가능.
    QGraphicsView::CacheNone, CacheBackground 설정 가능

QGraphicsView::ViewportAnchor transformationAnchor() 
void setTransformationAnchor(QGraphicsView::ViewportAnchor anchor)
 
    뷰 변형시 기준점 조회 및 설정. QGraphicsView::NoAnchor, AnchorViewCenter, AnchorUnderMouse 설정 가능

QGraphicsView::ViewportAnchor resizeAnchor() 
void setResizeAnchor(QGraphicsView::ViewportAnchor anchor)
 :
    뷰 크기 변형 시 기준점 조회 및 설정. QGraphicsView::NoAnchor, AnchorViewCenter, AnchorUnderMouse 설정 가능

Qt::ItemSelectionMode rubberBandSelectionMode() 
void setRubberBandSelectionMode(Qt::ItemSelectionMode mode)
 : 고무줄 선택 모드 조회 및 설정.
    Qt::ContainsItemShape, IntersectsItemShape, ContainsItemBoundingRect, IntersectsItemBoundingRect 

QGraphicsView::OptimizationFlags optimizationFlags() 
void setOptimizationFlags(QGraphicsView::OptimizationFlags flags)
 : 최적화 옵션 조회 및 설정. 
    기본값은 설정이 없다. QGraphicsView::DontSavePainterState, DontAdjustForAntialiasing, IndirectPainting

 

 

기타 유용한 함수와 슬롯은 다음과 같다.

 

QList<QGraphicsItem *> items() : 씬에 속한 모든 요소들을 리턴한다. 가장 위에 있는 요소가 리스트 선두에 담긴다.

QList<QGraphicsItem *> items(const QPoint &pos) 
QList<QGraphicsItem *> items(int x, int y) 
QList<QGraphicsItem *> items(const QRect &rect, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) 
QList<QGraphicsItem *> items(int x, int y, int w, int h, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape)
:
    지정 위치나 범위에 해당하는 요소들의 목록을 리턴.

QGraphicsItem * itemAt(const QPoint &pos) 
QGraphicsItem * itemAt(int x, int y)
: 지정 위치의 요소 추출. 여러 개가 있으면 가장 위에 있는 것 추출.

void centerOn(const QPointF &pos)
void centerOn(qreal x, qreal y)
void centerOn(const QGraphicsItem *item)
 : 지정한 위치나 요소가 뷰 중앙에 오도록 스크롤.

void ensureVisible(const QRectF &rect, int xmargin = 50, int ymargin = 50)
void ensureVisible(qreal x, qreal y, qreal w, qreal h, int xmargin = 50, int ymargin = 50)
void ensureVisible(const QGraphicsItem *item, int xmargin = 50, int ymargin = 50)
 :
    지정한 위치나 요소가 마진을 감안하여 보이도록 스크롤.

void fitInView(const QRectF &rect, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio)
void fitInView(qreal x, qreal y, qreal w, qreal h, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio)
void fitInView(const QGraphicsItem *item, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio)
 :
    지정한 위치나 요소가 뷰 내에 적절하게 보이도록 배율을 조정하고 스크롤.

void rotate(qreal angle) : 지정한 각도로 뷰 회전.

void scale(qreal sx, qreal sy) : 지정한 배율로 뷰 확대/축소.

void shear(qreal sh, qreal sv) : 지정한 값으로 뷰 깎아내기..

QTransform transform()

void setTransform(const QTransform &matrix, bool combine = false)

void resetTransform() : 현재 변형 작업 조회 및 설정/초기화.

QGraphicsScene * scene()

void setScene(QGraphicsScene *scene) : 씬 조회 및 설정.

QRectF sceneRect() const
void setSceneRect(const QRectF &rect)
void setSceneRect(qreal x, qreal y, qreal w, qreal h)
 : 씬 크기 조회 및 설정

void invalidateScene(const QRectF &rect = QRectF(), QGraphicsScene::SceneLayers layers) :
  씬 레이어를 지정하여 다시 그리도록 지시. 레이어를 지정하지 않으면 전체 레이어를 대상으로 함.

void updateScene(const QList<QRectF> &rects)
void updateSceneRect(const QRectF &rect)
 : 지정 영역을 다시 그리도록 지시.

 

 

728x90

'C | C++' 카테고리의 다른 글

QGraphicsItem 클래스(QT C++)  (0) 2024.03.14
QGraphicsScene 클래스(QT C++)  (0) 2024.03.13
QTableView 클래스(QT C++)  (0) 2024.03.08
QTableWidget 클래스(QT C++)  (0) 2024.03.08
QTextEdit 클래스(QT C++)  (0) 2024.03.07