Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Drawing in a QGraphicsScene, how to add a mouse button is released?

Drawing in a QGraphicsScene, how to add a mouse button is released?

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 325 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mikeeeeee
    wrote on last edited by
    #1

    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();
    }
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      Mikeeeeee
      wrote on last edited by
      #2

      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;
      }
      
      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved