Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Can I draw in QGraphicsView on Qpixmap?



  • Hi!
    I created a drawing class in QGraphicsView .
    But can I draw in QGraphicsView on Qpixmap?


  • Lifetime Qt Champion

    Hi,

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



  • 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);    
    }
    


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


  • Lifetime Qt Champion

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



  • But it does not need to erase the lower pixels


  • Lifetime Qt Champion

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



  • 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.


Log in to reply