Drawing in a QGraphicsScene, how to add a mouse button is released?
Solved
General and Desktop
-
Hi!
I draw like this in QGraphicsScene, but when scaling I get a bug with the continuation of drawing.
How to add a mouse button is released?.h
#ifndef PAINTSCENE_H #define PAINTSCENE_H #include <QGraphicsScene> #include <QGraphicsSceneMouseEvent> #include "QGraphicsView" #include <QTimer> #include <QDebug> //#include "QLabel" class paintScene : public QGraphicsScene { Q_OBJECT public: explicit paintScene(QObject *parent = 0); ~paintScene(); //QGraphicsView *myQGraphicsScene; //paintScene *scene; // Объявляем кастомную графическую сцену private: QPointF previousPoint; // Координаты предыдущей точки private: // Для рисования используем события мыши void mousePressEvent(QGraphicsSceneMouseEvent * event); void mouseMoveEvent(QGraphicsSceneMouseEvent *event); }; #endif // PAINTSCENE_H
.cpp
#include "paintscene.h" paintScene::paintScene(QObject *parent) : QGraphicsScene(parent) { //myQGraphicsScene = new QGraphicsView(); //scene = new paintScene(); // Инициализируем графическую сцену } paintScene::~paintScene() { } void paintScene::mousePressEvent(QGraphicsSceneMouseEvent *event) { // При нажатии кнопки мыши отрисовываем эллипс addEllipse(event->scenePos().x() - 5, event->scenePos().y() - 5, 10, 10, QPen(Qt::NoPen), QBrush(Qt::red)); // Сохраняем координаты точки нажатия previousPoint = event->scenePos(); } void paintScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { // Отрисовываем линии с использованием предыдущей координаты addLine(previousPoint.x(), previousPoint.y(), event->scenePos().x(), event->scenePos().y(), QPen(Qt::red,10,Qt::SolidLine,Qt::RoundCap)); // Обновляем данные о предыдущей координате previousPoint = event->scenePos(); }
-
It is work
#include "paintscene.h" paintScene::paintScene(QObject *parent) : QGraphicsScene(parent) { //myQGraphicsScene = new QGraphicsView(); //scene = new paintScene(); // Инициализируем графическую сцену } paintScene::~paintScene() { } void paintScene::mousePressEvent(QGraphicsSceneMouseEvent *event) { // При нажатии кнопки мыши отрисовываем эллипс addEllipse(event->scenePos().x() - 5, event->scenePos().y() - 5, 10, 10, QPen(Qt::NoPen), QBrush(Qt::red)); // Сохраняем координаты точки нажатия previousPoint = event->scenePos(); buttonIsPressed = true; } void paintScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { if (buttonIsPressed) { // Отрисовываем линии с использованием предыдущей координаты addLine(previousPoint.x(), previousPoint.y(), event->scenePos().x(), event->scenePos().y(), QPen(Qt::red,10,Qt::SolidLine,Qt::RoundCap)); // Обновляем данные о предыдущей координате previousPoint = event->scenePos(); } } void paintScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { buttonIsPressed = false; }