<SOLVED> QGraphicsView & mouse events.



  • Hi Great Gurus of C++ and Qt,
    I`m run in a new problem in my Qt learning quest ,like a blind horse charging a brick wall.

    Ok to the point, I trying to create a small simple graphic program, to draw flew lines, generate some Fonts Glyphs, beziers etc, on QGraphicsView. Problem I'm run in to is dreded MouseEvents, I'm found on this forum flew interesting examples, they work, more or les but.. I cant get any way map correctly coordinates from scene, I can't use mapToScene, in example I found on stackoverflow.

    http://stackoverflow.com/questions/7830054/how-to-draw-a-point-on-mouseclick-on-a-qgraphicsscene

    @ *QPointF pt = mapToScene(e->pos()); *@ i cant use this statement there is no mapToScene available to me only map (from or to global & parent ),

    I not simply coping code from others, I always try to find how to get idea form examples and use it but i cant get around this problem.

    I'm play a-bit with mapFrom and i got here. I can map mouse but only when is one of buttons is pres and it mapping coordinates according to whole window not from central point of scene, where I put small circle to show me where on QGraphicsView is center of scene.

    Simply idea is to get coordinates every time mouse pointer is over QGraphicsView, without any clicking on it.
    Another think is now when i click on it it didn't record event only when i click outside GraphicsView it start event.
    If I change how I mapping point pt to :
    @pt = ui->graphicsView->mapToScene (e->pos ());@ i can get different way of getting coordinates from GraphicsView but they seems to be offset by Top tool-bar and side gap to the edge of mainwindow.

    I have that silly feeling I missing somethings silly, but I can't get any idea where.

    Ok there is my lame simply code to test this.

    THX in advance for any ideas. ;)

    mainwindow.h

    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QMouseEvent>
    #include <QGraphicsScene>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    signals:

    public slots:
    void mouseMoveEvent (QMouseEvent *e);

    private:
    Ui::MainWindow *ui;
    QGraphicsScene *scene = new QGraphicsScene;
    };

    #endif // MAINWINDOW_H
    @
    Mainwindow.cpp
    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QGraphicsScene>
    #include <QGraphicsLineItem>
    #include <QGraphicsEllipseItem>
    #include <QGraphicsRectItem>
    #include <QMouseEvent>
    #include <QPoint>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    scene->addEllipse (0,0,2,2,QPen(Qt::black));
    scene->setSceneRect (-200,-200,200,200);
    ui->setupUi(this);
    ui->graphicsView->setScene (scene);
    }

    void MainWindow::mouseMoveEvent (QMouseEvent *e)
    {
    QPointF pt;
    QString x_coords;
    QString y_coords;
    pt = ui->graphicsView->mapFrom (ui->graphicsView ,e->pos ());
    x_coords = (QString::number (pt.x ()));
    y_coords = (QString::number (pt.y ()));

    ui->Display_X_Cords->setText (x_coords);
    ui->Display_Y_Cords->setText (y_coords);
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    @
    !http://ar2ur.com/files/MainWindow.jpg(Simplest window)!



  • Hi Ar2uR.
    Have you seen this
    "Diagram Scene Example":https://qt-project.org/doc/qt-5/qtwidgets-graphicsview-diagramscene-example.html ?



  • Thx Qxoz for suggestion, I didn't seen this one. Is interesting but not muche use to me.
    But I'm found how to do it correctly using ui.
    Basing on sample code from StackOverflow I'm subclass QGraphivsView and created own Class MyGraphicsView, then i can correctly mapping a mouse events from scene installed in it.
    To use own Class for GraphicView In Qt Creator you need Promote it to this Class ( MyGraphicsView ) and from now on it starting use this class to hold scene and events.
    In order to track Mouse each time cursor is over GraphicsView You need to for first enable Mouse tracking on it, then use QWidget mouseMoveEvent, and now each time you move mouse it sending a signal to your slot with mousevent.
    Next think is just connect Signal from MyGraphicsView to MainWindow class in order to send QPointF with coordinates.

    This is my sample code:
    MyGraphicsView.h

    @#ifndef MYGRAPHICSVIEW_H
    #define MYGRAPHICSVIEW_H

    #include <QWidget>
    #include <QGraphicsView>
    #include <QGraphicsScene>
    #include <QGraphicsEllipseItem>
    #include <QMouseEvent>

    class MyGraphicsView : public QGraphicsView
    {
    Q_OBJECT
    public:
    explicit MyGraphicsView(QWidget *parent = 0);

    signals:
    void sendMousePoint(QPointF point);

    public slots:
    void mousePressEvent(QMouseEvent * e);
    void mouseMoveEvent (QMouseEvent *move);

    private:
    QGraphicsScene * scene;
    };

    #endif // MYGRAPHICSVIEW_H@

    MyGraphicsView.cpp
    @#include "mygraphicsview.h"
    #include "mainwindow.h"
    #include <QPointF>
    #include <QDebug>

    MyGraphicsView::MyGraphicsView(QWidget *parent) :
    QGraphicsView(parent)
    {
    scene = new QGraphicsScene();
    this->setSceneRect(50, 50, 350, 350);
    this->setScene(scene);

    }

    void MyGraphicsView::mousePressEvent(QMouseEvent * e)
    {
    double rad = 1;
    QPointF pt = mapToScene(e->pos());
    scene->addEllipse(pt.x()-rad, pt.y()-rad, rad2.0, rad2.0,
    QPen(), QBrush(Qt::SolidPattern));

    }
    void MyGraphicsView::mouseMoveEvent (QMouseEvent *move)
    {
    QPointF movment;
    movment = mapToScene (move->pos ());
    emit sendMousePoint (movment);
    qDebug() << "x=" << movment.x () << " y=" << movment.y ();
    delete (&movment);

    }@

    MainWindow.h
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    public slots:
    void setMousePoint (QPointF point);

    private slots:

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H
    @
    and MainWindow.cpp
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "mygraphicsview.h"
    #include <QDebug>

    #include <QtCore>
    #include <QGridLayout>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    ui->centralWidget->setMouseTracking (true);

    QWidget::connect (ui->graphicsView, SIGNAL(sendMousePoint(QPointF)),this, SLOT(setMousePoint(QPointF)));
    

    }

    void MainWindow::setMousePoint (QPointF point)
    {
    ui->lValue_x->setNum (point.x ());
    ui->lValue_y->setNum (point.y ());
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }@

    Main.cpp is standart :0
    !http://ar2ur.com/files/WorkingMouseTGracking.jpg(http://ar2ur.com/files/WorkingMouseTGracking.jpg)!


Log in to reply
 

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