本文主要是介绍QGraphicsView移动,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
- mygraphicsview.h
- mygraphicsview.cpp
- mygraphicsscene.h
- mygraphicsscene.cpp
- mygraphicsitem.h
- mygraphicsitem.cpp
- mainwindow.h
- mainwindow.cpp
mygraphicsview.h
#ifndef MYGRAPHICSVIEW_H
#define MYGRAPHICSVIEW_H#include <QGraphicsView>
class myGraphicsView : public QGraphicsView
{Q_OBJECT
public:myGraphicsView(QWidget *parent = nullptr);myGraphicsView(QGraphicsScene *scene, QWidget *parent = nullptr);
protected:bool event(QEvent *event);
#if 0void mousePressEvent(QMouseEvent *event);void mouseMoveEvent(QMouseEvent *event);void mouseReleaseEvent(QMouseEvent *event);
#endif
};
#endif // MYGRAPHICSVIEW_H
mygraphicsview.cpp
#include "mygraphicsview.h"
#include <QDebug>
myGraphicsView::myGraphicsView(QWidget *parent) : QGraphicsView (parent)
{}myGraphicsView::myGraphicsView(QGraphicsScene *scene, QWidget *parent): QGraphicsView(scene, parent)
{}bool myGraphicsView::event(QEvent *event)
{QGraphicsView::event(event);
}#if 0
void myGraphicsView::mousePressEvent(QMouseEvent *event)
{setMouseTracking(true);QGraphicsView::mousePressEvent(event);
}void myGraphicsView::mouseMoveEvent(QMouseEvent *event)
{QGraphicsView::mouseMoveEvent(event);
}void myGraphicsView::mouseReleaseEvent(QMouseEvent *event)
{QGraphicsView::mouseReleaseEvent(event);
}
#endif
mygraphicsscene.h
#ifndef MYGRAPHICSSCENE_H
#define MYGRAPHICSSCENE_H
#include <QDebug>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsScene>
#include <QGraphicsLineItem>
class myGraphicsScene : public QGraphicsScene
{Q_OBJECT
public:myGraphicsScene(QObject *parent = nullptr);
protected:
#if 0void mousePressEvent(QGraphicsSceneMouseEvent *event);void mouseMoveEvent(QGraphicsSceneMouseEvent *event);void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
#endifbool event(QEvent *event);
};#endif // MYGRAPHICSSCENE_H
mygraphicsscene.cpp
#include "mygraphicsscene.h"myGraphicsScene::myGraphicsScene(QObject *parent): QGraphicsScene (parent)
{}bool myGraphicsScene::event(QEvent *event)
{return QGraphicsScene::event(event);
}#if 0
void myGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{QGraphicsScene::mousePressEvent(event);
}void myGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{QGraphicsScene::mouseMoveEvent(event);// 有BUG//if (mouseGrabberItem())// sendEvent(mouseGrabberItem(), event);
}void myGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{QGraphicsScene::mouseReleaseEvent(event);// 有BUG//if (mouseGrabberItem())// sendEvent(mouseGrabberItem(), event);
}
#endif
mygraphicsitem.h
#ifndef MYGRAPHICSITEM_H
#define MYGRAPHICSITEM_H#include <QGraphicsSceneMouseEvent>
#include <QGraphicsLineItem>
#include <QObject>
class myGraphicsItem :public QObject, public QGraphicsLineItem
{Q_OBJECT
public:myGraphicsItem(QObject *parent = nullptr, QGraphicsLineItem *parent2 = nullptr);
protected:void mousePressEvent(QGraphicsSceneMouseEvent *event);void mouseMoveEvent(QGraphicsSceneMouseEvent *event);void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
private:QPointF m_StartPos;QPointF m_EventStartPos;QPointF m_EventEndPos;int m_State;
};#endif // MYGRAPHICSITEM_H
mygraphicsitem.cpp
#include "mygraphicsitem.h"
#include <QDebug>
myGraphicsItem::myGraphicsItem(QObject *parent, QGraphicsLineItem *parent2) :QObject(parent), QGraphicsLineItem (parent2)
{m_State = 0;
}void myGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{if (m_State == 0) {m_State = 1;m_StartPos = pos();m_EventEndPos = m_EventStartPos = event->scenePos();}
}void myGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{if (m_State == 1) {m_EventEndPos = event->scenePos();setPos(m_StartPos + m_EventEndPos - m_EventStartPos);}
}void myGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{if (1 == m_State) {m_State = 0;}
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsItem>
#include <mygraphicsitem.h>
#include <mygraphicsscene.h>
#include <mygraphicsview.h>
class MainWindow : public QMainWindow
{Q_OBJECT
public:MainWindow(QWidget *parent = nullptr);~MainWindow();
private:myGraphicsView *gv;myGraphicsScene *scene;myGraphicsItem *mgi[10];
};#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent)
{QPen pen;pen.setWidth(5);pen.setColor(QColor(0, 255, 255));scene = new myGraphicsScene(this);scene->addLine(0, 0, 100, 100, QPen(Qt::green));for (int i = 0; i < 10; i++) {mgi[i] = new myGraphicsItem();mgi[i]->setLine(100, 100, 400, 300);mgi[i]->setPen(pen);scene->addItem(mgi[i]);}gv = new myGraphicsView(scene, this);gv->setBackgroundBrush(Qt::black);setCentralWidget(gv);
}MainWindow::~MainWindow()
{}
这篇关于QGraphicsView移动的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!