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 0
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
#endif
};
#endif
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 0
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
#endif
bool event(QEvent *event);
};
#endif
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);
}
void myGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsScene::mouseReleaseEvent(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.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.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()
{
}