Quantcast
Channel: QtWebEngine
Viewing all articles
Browse latest Browse all 13965

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

$
0
0
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

Viewing all articles
Browse latest Browse all 13965

Trending Articles