Graphics view does not respond to clicks in the graphics view but only next to it



  • I want to be able to draw in the graphics view. Drawing in the graphics view works but only when i click somewhere in the widget and not in the graphics view itself. It does not seem to respond at all to clicks in the graphics view. What am i doing wrong? I am new to C++ and Qt..

    @
    #include "mywidget.h"
    #include "ui_mywidget.h"
    #include <QDebug>

    MyWidget::MyWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MyWidget)
    {
    ui->setupUi(this);
    scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);
    ui->graphicsView->setInteractive(true);
    ui->graphicsView->setMouseTracking(true);
    }
    void MyWidget::paintEvent(QPaintEvent * event)
    {
    QWidget::paintEvent(event);
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing,true);
    QPen linepen(Qt::blue);
    linepen.setCapStyle(Qt::RoundCap);
    linepen.setWidth(2);
    painter.setPen(linepen);

    scene->clear();
    
    if (firstmouseclicked)
    { 
        m_drawing << QPoint(x_mouse,y_mouse);
        QPainterPath path;
        path.addPolygon(m_drawing);
        scene->addPath(path);
    }
    

    }
    void MyWidget::mouseMoveEvent(QMouseEvent *event){

    if (event->button() == Qt::LeftButton)
    {
        setMouseTracking(true);
       }
    
    x_mouse= event->x();
    y_mouse= event->y();
    
    firstmouseclicked = true;
    
    update();
    

    }@

    and my header file

    @
    #ifndef MYWIDGET_H
    #define MYWIDGET_H

    #include <QWidget>
    #include <QMouseEvent>
    #include <QPainter>
    #include <QGraphicsView>
    #include <QPolygonF>
    #include <QDebug>
    #include <QtGui>
    #include <QGraphicsScene>
    #include <QGraphicsItem>
    #include <QPainterPath>
    #include <QGraphicsSceneMouseEvent>

    namespace Ui {
    class MyWidget;
    }

    class MyWidget : public QWidget
    {
    Q_OBJECT

    public:
    explicit MyWidget(QWidget *parent = 0);
    ~MyWidget();
    QSize sizeHint() const;

    protected:
    void mouseMoveEvent(QMouseEvent *event);
    void paintEvent(QPaintEvent *);

    private:
    Ui::MyWidget *ui;

    float x_mouse = 0;
    float y_mouse = 0;
    
    QPolygon m_drawing;
    
    bool firstmouseclicked = false;
    QGraphicsScene* scene;
    

    QGraphicsView* view;
    //QGraphicsItem* item;
    };

    #endif // MYWIDGET_H
    @



  • I don't know what your desired out come is, but you shouldn't be doing anything in the paint event. You aren't drawing anything. Adding the path should probably be done in the mouseMove event. Also, the topic is misleading. MyWidget is handling mouse move events not the graphics view. Have you verified the mouseMove event handler is being called?



  • I am not so familiar with qt.. My predefined pictures (which i left out ) do show in the graphics view. and my drawing is also only visible in the graphics view, but it does not respond to mouse events in the graphics view. If i should not use paint event for this, how/where do i allow for mouse movements in the graphics view?



  • This is what i tried now, it does not give errors but also does not draw at all.
    @void QGraphicsView::mouseMoveEvent(QMouseEvent *event)
    {

    if (event->button() == Qt::LeftButton)
    {
        setMouseTracking(true);
    }
    float x_mouse = 0;
    float y_mouse = 0;
     bool firstmouseclicked = false;
    array1= event->x();
    array2= event->y();
     firstmouseclicked = true;
        update();
    
     QPolygon m_drawing;
     QGraphicsScene* scene;
     scene->clear();
     if (firstmouseclicked)
    {
        firstmouseclicked=false;
        m_drawing<< QPoint(x_mouse, y_mouse);
        QPainterPath path;
        path.addPolygon(m_drawing);
        scene->addPath(path);
    
    }
    

    }@



  • First of all, don't overload QGraphicsView::mouseMoveEvent(). If you want to do that, write your own class deriving from QGraphicsView, and put your code there.

    However, I would suggest subclassing QGraphicsScene instead. Start simple, and try to create a simple item (e.g. a rectangle) when you click the mouse.
    For that, you reimplement mousePressEvent, create a new item (e.g. QGraphicsRectItem) with a fixed size, and positioned at the scene position (mouseEvent->scenePos()).

    You can find sample code in the DiagramScene example, in diagramscene.cpp.

    Once the items appear on press, you can go on with drawing a path:

    • In mousePressEvent, create a new QGraphicsPathItem, add it to the scene and remember it. Set a path that only contains the coordinate of the mousePressEvent's scenePos (see QPainterPath::moveTo method).
      Also, do not forget to accept the mousePress event.
    • In mouseMove event, add a point to the path of the QGraphicsPathItem you remembered. Get the existing path from the item, add the current position (see QPainterPath::lineTo method)

    That should do it.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.