@
@
@Painting2021-04-10 18:50:53
Qt
@Painting, 2021-04-10 18:50:53

Why isn't drawing happening?

Hello.
There is a graphic scene, drawing with the mouse should take place in it.
But the application starts, but the line is not drawn. The compiler only issues a couple of warnings.
What's the catch?

#pragma once
#include <QWidget>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QRectF>
#include <QPushButton>
#include <QGraphicsItem>
#include <QLineEdit>
#include <QMouseEvent>

class Paint : public QWidget
{
    Q_OBJECT
public:
    Paint(QWidget *parent = 0);
    QGraphicsScene *scene;
    QGraphicsView *view;

    QPushButton *btn;

private:
   QPointF  previousPoint;
   void mousePressEvent(QGraphicsSceneMouseEvent *e);
   void mouseMoveEvent(QGraphicsSceneMouseEvent *e);
};

#include "paint.h"
#include
#include
#include
#include

Paint::Paint(QWidget *parent):QWidget(parent)
{
scene = new QGraphicsScene(QRectF(10,10,1000,1000));

view = new QGraphicsView(scene, this);

view->show();

btn = new QPushButton("Draw!", this);
btn -> setGeometry(1020, 10, 70, 30);

}
void Paint::mousePressEvent(QGraphicsSceneMouseEvent *e)
{
QGraphicsEllipseItem *p = scene->addEllipse(e->scenePos().x() - 5,e->scenePos().y() - 5,
10,
10 ,
QPen(Qt::NoPen),
QBrush(Qt::red));
previousPoint = e->scenePos();
}
void Paint::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
{

QGraphicsLineItem *f = scene->addLine(previousPoint.x(),
previousPoint.y(),
e->scenePos().x(),
e->scenePos() .y(),
QPen(Qt::red,10,Qt::SolidLine,Qt::RoundCap));

previousPoint = e->scenePos();
}

<code lang="cpp">
QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    paint.cpp

HEADERS += \
    paint.h

FORMS +=

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

</code>

Thanks in advance...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ananiev, 2021-04-11
@SaNNy32

Because the mousePressEvent and mouseMoveEvent functions are never called by anyone.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question