QT5笔记: 35. QGraphicsView 视图

![image-20220505144510057](QT5 使用.assets/image-20220505144510057.png)

三者关系:View中可以有多个Scene,Scene放在View中,Scene可以装多个Item图形

若要实现鼠标事件,则需要重写QGraphicsView
MyGraphicsView.h

#ifndef MYGRAPHICSVIEW_H
#define MYGRAPHICSVIEW_H

#include <QGraphicsView>
#include <QMouseEvent>

class MyGraphicsView : public QGraphicsView
{
    Q_OBJECT
public:
    explicit MyGraphicsView(QWidget *parent = nullptr);

signals:
    void mouseMoveEvent(QPoint point);//发送鼠标事件
    void mouseClickEvent(QPoint point);

    // QWidget interface
protected:
    void mouseMoveEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;

};

#endif // MYGRAPHICSVIEW_H

MyGraphicsView.cpp

窗口#include "mygraphicsview.h"

MyGraphicsView::MyGraphicsView(QWidget *parent) : QGraphicsView(parent)
{

}


void MyGraphicsView::mouseMoveEvent(QMouseEvent *event)
{
    emit(mouseMoveEvent(event->pos()));
    QGraphicsView::mouseMoveEvent(event);
}

void MyGraphicsView::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        emit(mouseClickEvent(event->pos()));
    }
    QGraphicsView::mouseReleaseEvent(event);
}

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QGraphicsScene>
#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

    QGraphicsScene *graphicsScene;//场景

    void initGraphicsView();

private slots:
    void onMouseMove(QPoint point);
    void onMouseClick(QPoint point);

    // QWidget interface
protected:
    void resizeEvent(QResizeEvent *event) override;
};
#endif // MAINWINDOW_H

MainWindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QGraphicsRectItem>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->graphicsView->setCursor(Qt::CrossCursor);
    ui->graphicsView->setMouseTracking(true);

    initGraphicsView();

    connect(ui->graphicsView, SIGNAL(mouseMoveEvent(QPoint)), this, SLOT(onMouseMove(QPoint)));
    connect(ui->graphicsView, SIGNAL(mouseClickEvent(QPoint)), this, SLOT(onMouseClick(QPoint)));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initGraphicsView()
{
    QRectF rect = QRectF(-200, -100, 400, 200);
    graphicsScene = new QGraphicsScene(rect);
    ui->graphicsView->setScene(graphicsScene);//设置场景

    QPen pen;
    pen.setWidth(2);
    pen.setStyle(Qt::SolidLine);

    QBrush brush;
    brush.setColor(QColor(100, 200, 150, 150));
    brush.setStyle(Qt::CrossPattern);

    QGraphicsRectItem *item = new QGraphicsRectItem(rect);//画矩形
    item->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable);
    item->setPen(pen);
    graphicsScene->addItem(item);

    QGraphicsEllipseItem *ellipseItem = new QGraphicsEllipseItem(-100, -50, 200, 100);//椭圆
    ellipseItem->setPen(pen);
    ellipseItem->setBrush(brush);
    ellipseItem->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable);
    graphicsScene->addItem(ellipseItem);

    QGraphicsEllipseItem *circleItem = new QGraphicsEllipseItem(-50, -50, 100, 100);//圆
    circleItem->setPos(200, 100);
    circleItem->setPen(pen);
    brush.setStyle(Qt::SolidPattern);
    circleItem->setBrush(brush);
    circleItem->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemIsMovable);//可选 | 可聚焦 | 可点击后移动(拖拽)
    graphicsScene->addItem(circleItem);
}

void MainWindow::onMouseMove(QPoint point)
{
    QPointF scenePoint = ui->graphicsView->mapToScene(point);//graphicsView映射到scene的坐标
    QString info = QString::asprintf("View坐标:(%d , %d); Scene坐标:(%.0f , %.0f)", point.x(), point.y(), scenePoint.x(), scenePoint.y());
    ui->statusbar->showMessage(info);
}

void MainWindow::onMouseClick(QPoint point)
{
    QPointF scenePoint = ui->graphicsView->mapToScene(point);
    QGraphicsItem *item = nullptr;
    item = graphicsScene->itemAt(scenePoint, ui->graphicsView->transform());
    if (item != nullptr) {
        QPointF itemPoint = item->mapFromScene(scenePoint);//求当前点击位置相对于当前图元的坐标,图元中心为原点
        QString message = ui->statusbar->currentMessage() + QString::asprintf("; Item 坐标:(%.0f , %.0f)", itemPoint.x(), itemPoint.y());
        ui->statusbar->showMessage(message);
    }
}

void MainWindow::resizeEvent(QResizeEvent *event)
{
    QString graphicsStr = ui->label->text();
    QString sceneStr = ui->label_2->text();

    int w = ui->graphicsView->width();
    int h = ui->graphicsView->height();
    ui->label->setText(QString::asprintf("Graphics View坐标,左上角总是(0,0),宽度= %d,长度= %d", w, h));

    QRectF rect = this->graphicsScene->sceneRect();
    ui->label_2->setText(QString::asprintf("QGraphicsView::sceneRect=(%.0f, %.0f, %.0f, %.0f)", rect.x(), rect.y(), rect.width(), rect.height()));
}

界面:
image

posted @ 2023-03-16 13:48  echo_lovely  阅读(369)  评论(0编辑  收藏  举报