Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved] Is it possible? I want to paint on QGraphicsScene !!HELP..

[Solved] Is it possible? I want to paint on QGraphicsScene !!HELP..

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 1.4k 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.
  • N Offline
    N Offline
    NandiniN
    wrote on last edited by
    #1

    to my paint application need to insert mousepress,release and move events to draw on graphics scene..and other functionalists i want it same as "Diagram Scene"..how can i approch to this.

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      The best option would be to subclass QGraphicsScene and reimplementing its mousePressEvent, mouseMoveEvent and mouseReleaseEvent.
      For Drawing you can use "QGraphicsPathItem":http://qt-project.org/doc/qt-4.8/qgraphicspathitem.html and then "setPath":http://qt-project.org/doc/qt-4.8/qgraphicspathitem.html#setPath to "QPainterPath":http://qt-project.org/doc/qt-4.8/qpainterpath.html

      Here'a an example..
      @
      #include "paintscene.h"

      PaintScene::PaintScene(QObject *parent) :
      QGraphicsScene(parent)
      {
      pressed = false;
      }

      void PaintScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
      {
      m_item = new QGraphicsPathItem();
      QPainterPath path;
      path.moveTo(event->scenePos());
      m_item->setPath(path);
      addItem(m_item);
      pressed = true;
      }

      void PaintScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
      {
      if(pressed)
      {
      QPainterPath path = m_item->path();
      path.lineTo(event->scenePos());
      m_item->setPath(path);
      }
      }

      void PaintScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
      {
      pressed = false;
      m_item = NULL;
      }
      @
      --- paintscene.h

      @
      #ifndef PAINTSCENE_H
      #define PAINTSCENE_H

      #include <QGraphicsScene>
      #include <QGraphicsPathItem>
      #include <QGraphicsSceneMouseEvent>

      class PaintScene : public QGraphicsScene
      {
      Q_OBJECT
      public:
      explicit PaintScene(QObject *parent = 0);

      protected:
      void mousePressEvent(QGraphicsSceneMouseEvent *event);
      void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
      void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

      signals:

      public slots:

      private:
      QGraphicsPathItem *m_item;
      QPointF previous;
      bool pressed;

      };

      #endif // PAINTSCENE_H
      @

      Hope this helps a bit..

      157

      1 Reply Last reply
      0
      • N Offline
        N Offline
        NandiniN
        wrote on last edited by
        #3

        hey thanku :)

        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