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

How to draw a Rectangle in Qgraphicsview dynamically (runtime), using Qt4



  • I'm just a starter on QT platform and trying to draw a Rectangle Dynamically. The program uses 3 Mouse events namely

    mousePressEvent
    mouseMoveEvent
    mouseReleaseEvent
    Problems is All mouse Events are called but the rectangle is not drawn.

    Here is the code snippet

    main.cpp

    #include <QtGui/QApplication>
    #include "dialog.h"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Dialog w;
        w.show();
        
        return a.exec();
    }
    
    

    dialog.cpp

    #include "dialog.h"
    #include "ui_dialog.h"
    #include "mysquare.h"
    
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        ui->setupUi(this);
        setMouseTracking(true);
        scene = new QGraphicsScene(this);
        ui->graphicsView->setScene(scene);
        scene->setBackgroundBrush(Qt::black);
        square = new mySquare;
        scene->addItem(square);
        square->mPix = QPixmap(400,400);
        square->mPix.fill(Qt::white);
        //scene->addPixmap(mPix);
        scene->addPixmap(square->mPix);
    
    
    }
    
    Dialog::~Dialog()
    {
        delete ui;
    }
    
    void Dialog::on_pushButton_clicked()
    {
       square->selectedTool = 1;
    }
    
    

    dialog.h

    #ifndef DIALOG_H
    #define DIALOG_H
    
    #include <QDialog>
    #include <QtCore>
    #include <QtGui>
    #include "mysquare.h"
    
    namespace Ui {
    class Dialog;
    }
    
    class Dialog : public QDialog
    {
        Q_OBJECT
        
    public:
        explicit Dialog(QWidget *parent = 0);
        ~Dialog();
        
    private slots:
        void on_pushButton_clicked();
    
    private:
        Ui::Dialog *ui;
        QGraphicsScene *scene;
        mySquare *square;
    
    };
    
    #endif // DIALOG_H
    
    

    mysquare.cpp

    #include "mysquare.h"
    
    using namespace std;
    
    
    mySquare::mySquare()
    {
        pressed  = false;
        //setFlag(ItemIsMovable);
        selectedTool = 1;
        mPix = QPixmap(400,400);
        mPix.fill(Qt::white);
    }
    
    QRectF mySquare::boundingRect() const
    {
        return QRectF(0,0,200,200);
    }
    
    
    
    void mySquare::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        //painter->begin(this);
        QPen pen(Qt::red);
        painter->setPen(pen);
        //painter->setPen(Qt::blue);
        //QRectF rec = boundingRect();
        if(pressed)
        {
           // painter->drawPixmap();
            painter->drawPixmap(0,0,mPix);
    
            painter->drawRect(mRect);
            drawStarted = true;
        }
        else if (drawStarted)
        {
            QPainter tempPainter(&mPix);
            tempPainter.drawRect(mRect);
            painter->drawPixmap(0,0,mPix);
        }
    
        //painter->end();
    //    QRectF rec = boundingRect();
    //    QBrush brush(Qt::blue);
    
    //    if(pressed)
    //    {
    //        brush.setColor(Qt::red);
    //    }
    //    else
    //    {
    //        brush.setColor(Qt::black);
    //    }
    //    painter->fillRect(rec,brush);
    //    painter->drawRect(rec);
    }
    
    void mySquare::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
        pressed = true;
    
        y = event->pos();
        cout<<"Pos X : "<<y.x() <<endl;
        cout<<"Pos Y : "<<y.y() <<endl;
    
        if(selectedTool == 1)
        {
            mRect.setTopLeft(event->pos());
            mRect.setBottomRight(event->pos());
    
            X_1 = mRect.x();
            //X_1 = 400 - X_1;
            Y_1 = mRect.y();
            //Y_1 = 600 - Y_1;
            Y_2 = Y_1;
            X_3 = X_1;
            cout << "Value of x_start axis " << X_1 <<endl;
            cout << "Value of y_start axis " << Y_1 <<endl;
        }
        else if (selectedTool == 2)
        {
    
        }
        update();
        //cout<<"Inside mousePressEvent\n";
        //QGraphicsItem::mousePressEvent(event);
    }
    
    void mySquare::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    {
        cout<<"Inside mouseMoveEvent \n";
        if(event->type() == QEvent::MouseMove)
        {
            if(selectedTool == 1){
                mRect.setBottomRight(event->pos());
            }
            else if (selectedTool == 2){
                //mLine.setP2(event->pos());
            }
        }
        update();
    }
    
    void mySquare::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    {
    //    X_4 = event->x();
    //    X_4 = 400 - X_4;
    //    Y_4 = event->y();
    //    Y_4 = 600 - Y_4;
    //    Y_3 = Y_4;
    //    X_2 = X_4;
    //    cout << "Value of x_end axis " << event->x() <<endl;
    //    cout << "Value of y_end axis " << event->y() <<endl;
    
    //    cout << "Value of all mapped coordinates \n" << "X_1 = " << X_1 << "\t\t Y_1 = " << Y_1 << "\n X_2 = " << X_2 << "\t Y_2 = " << Y_2 << "\n X_3 = " << X_3 << "\t Y_3 = " << Y_3 << "\n X_4 = " << X_4 << "\t Y_4 = "  << Y_4 <<endl;
    
        pressed = false;
        update();
        cout<<"Inside mouseReleaseEvent \n";
        //QGraphicsItem::mouseReleaseEvent(event);
    }
    
    

    mysquare.h

    #ifndef MYSQUARE_H
    #define MYSQUARE_H
    #include <QPainter>
    #include <QGraphicsItem>
    #include <QDebug>
    #include <QPainter>
    #include <iostream>
    #include <QPixmap>
    #include <QPaintEvent>
    #include <QGraphicsSceneMouseEvent>
    
    class mySquare: public QGraphicsItem
    {
    public:
        mySquare();
        int selectedTool;
        QPixmap mPix;
        QRectF boundingRect() const;
        void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
       //void paintEvent(QPaintEvent *event);
        bool pressed;
        bool drawStarted;
        int X_1, Y_1,X_2, Y_2,X_3, Y_3,X_4, Y_4;
    
    protected:
        void mousePressEvent(QGraphicsSceneMouseEvent *event);
        void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
        void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    
    private:
        QRectF mRect;
        QPointF y;
    
    
    private slots:
    
    };
    
    #endif // MYSQUARE_H
    
    

    Please guide me,
    Thanks


  • Lifetime Qt Champion

    hi and welcome
    You seem to have disabled your paintEvent??
    You shall paint ONLY in the paintEvent.
    U can call your paint function but do it from PaintEvent.

    also , this might be usefull
    http://www.walletfox.com/course/qgraphicsitemruntimedrawing.php



  • @mrjj thanks a lot. will go through the link you shared.



  • @mrjj the code shown above uses QGraphicsItem. QGraphicsItem has paint as given in this doccument see here. paintEvent is in Qwidget, as per this link.
    How do i Enable paintEvent in QGraphicsItem:?


  • Lifetime Qt Champion

    @Dayama-Pradeep said:

    QGraphicsItem

    Hi sorry
    You did right.
    For QGraphicsItem docs says
    "The paint() function is called by QGraphicsView "
    So no paintEvent involved. ( i thought it was a widget)

    So you should insert some qDebug << "func: 1" ;
    around in code and see if your paint is called.


Log in to reply