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. Can I draw in QGraphicsView on Qpixmap?

Can I draw in QGraphicsView on Qpixmap?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 444 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 created a drawing class in QGraphicsView .
    But can I draw in QGraphicsView on Qpixmap?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Can you give more details about what you have implemented until now ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mikeeeeee
        wrote on last edited by
        #3

        I implemented drawing on QGraphicsScene.
        .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;    
            bool buttonIsPressed;
        
        private:
            void mousePressEvent(QGraphicsSceneMouseEvent * event);
            void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
            void mouseReleaseEvent(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();
            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;
        }
        
        

        I set 2 images for the scene. I would like to draw on the top Image,

        #include "paint.h"
        #include "ui_paint.h"
        #include <QtWidgets>
        
        Paint::Paint(QWidget *parent) :
            QWidget(parent),
            ui(new Ui::Paint)
        {
            ui->setupUi(this);
        
            QImage mapImage("C:\\Users\\New Owner\\Downloads\\mapMain.png");
          
        
        
           /QImage base("C:\\Users\\New Owner\\Downloads\\mapMain.png"); // set to some file/size
            //верхняя картинка
           /QImage overlay("C:\\Users\\New Owner\\Downloads\\mapTop.png");  // set to some file/size
        
        
            scene = new paintScene();       // Инициализируем графическую сцену
        
        
            scene->addPixmap(QPixmap::fromImage(mapImage));//add image
            scene->addPixmap(QPixmap::fromImage(overlay));//add image
        
        
            newGraphicsView =  new graphicsView();
            newGraphicsView->setScene(scene);
            ui->verticalLayout->addWidget(newGraphicsView);
        
            timer = new QTimer();     
            connect(timer, &QTimer::timeout, this, &Paint::slotTimer);
            timer->start(100);    
        }
        
        1 Reply Last reply
        0
        • M Offline
          M Offline
          Mikeeeeee
          wrote on last edited by Mikeeeeee
          #4

          And I need to draw so that the old points are erased when drawing new ones

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Draw on the top image or add items on top of it ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Mikeeeeee
              wrote on last edited by
              #6

              But it does not need to erase the lower pixels

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Then why not delete associated items and create new ones after that ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Mikeeeeee
                  wrote on last edited by Mikeeeeee
                  #8

                  I draw so:
                  h

                  #ifndef PAINTSCENE_H
                  #define PAINTSCENE_H
                  
                  #include <QGraphicsScene>
                  #include <QGraphicsSceneMouseEvent>
                  #include "QGraphicsView"
                  #include <QTimer>
                  #include <QDebug>
                  
                  
                  
                  
                  class paintScene : public QGraphicsScene
                  {
                  
                      Q_OBJECT
                  
                  public:
                      explicit paintScene(QObject *parent = 0);
                      ~paintScene();
                      QPen myPen;
                      QString paintOrDelete = "paint"; 
                  
                  
                  
                  
                  private:
                      QPointF     previousPoint;     
                      bool buttonIsPressed;
                  
                  private:
                      void mousePressEvent(QGraphicsSceneMouseEvent * event);
                      void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
                      void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
                  
                  };
                  
                  #endif // PAINTSCENE_H
                  

                  cpp

                  void paintScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
                  {
                      if (paintOrDelete == "paint")
                      {
                          addEllipse(event->scenePos().x() -  myPen.width()/2,
                                     event->scenePos().y() -  myPen.width()/2,
                                     myPen.width(),
                                     myPen.width(),
                                     QPen(Qt::NoPen),
                                     QBrush(myPen.color()));
                          previousPoint = event->scenePos();
                          buttonIsPressed = true;
                  
                      }   
                  }
                  

                  I here to build a class for move and delete:
                  h

                  #ifndef MOVEITEM_H
                  #define MOVEITEM_H
                  
                  #include <QObject>
                  #include <QGraphicsItem>
                  #include <QPainter>
                  #include <QGraphicsSceneMouseEvent>
                  #include <QDebug>
                  #include <QCursor>
                   #include <QApplication>
                  
                  class MoveItem : public QObject, public QGraphicsItem
                  {
                      Q_OBJECT
                  public:
                      explicit MoveItem(QObject *parent = 0);
                      ~MoveItem();
                  
                  signals:
                  
                  private:
                      QRectF boundingRect() const;
                      void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
                      void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
                      void mousePressEvent(QGraphicsSceneMouseEvent *event);
                      void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
                  
                  public slots:
                  };
                  
                  #endif // MOVEITEM_H
                  
                  

                  cpp

                  #include "moveitem.h"
                  
                  MoveItem::MoveItem(QObject *parent) :
                      QObject(parent), QGraphicsItem()
                  {
                  
                  }
                  
                  MoveItem::~MoveItem()
                  {
                  
                  }
                  
                  QRectF MoveItem::boundingRect() const
                  {
                      return QRectF (-30,-30,60,60);
                  }
                  
                  void MoveItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
                  {
                      painter->setPen(Qt::black);
                      painter->setBrush(Qt::green);
                      painter->drawRect(-30,-30,60,60);
                      Q_UNUSED(option);
                      Q_UNUSED(widget);
                  }
                  
                  void MoveItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
                  {
                      this->setPos(mapToScene(event->pos()));
                  }
                  
                  void MoveItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
                  {
                      this->setCursor(QCursor(Qt::ClosedHandCursor));
                      Q_UNUSED(event);
                  
                      if (QApplication::mouseButtons() == Qt::RightButton)
                      {
                          qDebug()<<"delete";
                          this->deleteLater();
                      }
                  }
                  
                  void MoveItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
                  {
                      this->setCursor(QCursor(Qt::ArrowCursor));
                      Q_UNUSED(event);
                  }
                  
                  

                  If I add this class to the project, it does not work.

                  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