Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Artificial MouseEvent and natural MouseEvent on QGraphicsView return different positions
Forum Updated to NodeBB v4.3 + New Features

Artificial MouseEvent and natural MouseEvent on QGraphicsView return different positions

Scheduled Pinned Locked Moved General and Desktop
positionqgraphicsviewqgraphicsscenemapfromglobal
2 Posts 2 Posters 1.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    deleted392
    wrote on 16 Oct 2015, 06:38 last edited by
    #1

    Dear All,

    Since I am kind of a Qt-novice I apologize if this has been asked/cleared before but my forum search didn't yield any results that could solve my problem.

    I am trying to create a drawing app where the user can enter basic shapes like points and polygons. The app contains a menubar on the left which is a QWidget with some nested layout. On the right I created a QWidget which contains a subclassed QGraphicsView called inputViewer and some labels below, arranged in a layout aswell. The scenes that are displayed in the inputViewer are subclasses of QGraphicsScene called inputModel.

    To handle the drawing, I overwrote the mousePressEvent-functions on both, the QGraphicsView and the QGraphicsScene. The actual drawing by mouse where only the mousePressEvent-functions get called work as I would expect it and therefore I assume these are correct.

    I want the user to be able to also draw a point at the cursor-position when the Qt::Key_Return is pressed. For that I overwrote the inputViewer::keyPressEvent() and artificially triggered a QMousePressEvent. For the position of the QMousePressEvent I created a QCursor object in the inputViewer and get the position by calling cursor->pos() (returns global coordinates afaik) and map them to the inputViewer with mapFromGlobal(). The inputViewer::mousePressEvent() is not overwritten and therefore should propagate the event right down to the scene if I am not mistaken.

    The problem is that if I let the program output the coordinates that I get from points being drawn by mouse and points being drawn by keyboard, there is a little offset. For example if I click on my canvas, don't move the mouse and press Return the output of the program is not the same.

    I hope the example is sufficient and thank all of you for your help and patience in advance.

    Regards

    ~Bazzog

    The program:

    void inputViewer::keyPressEvent(QKeyEvent *event)
    {
    ... some other if and elses to handle different keys ...
    else if(event->key() == Qt::Key_Return)
    {
    QMouseEvent event =QMouseEvent(QEvent::MouseButtonPress,this->mapFromGlobal(cursor->pos()),Qt::LeftButton,Qt::LeftButton,Qt::NoModifier);
    QGraphicsView::mousePressEvent(&event);
    }
    else
    {
    QGraphicsView::keyPressEvent(event);
    }
    }

    void inputModel::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
    std::cout << " Scene-coordinates are: " << event->scenePos().x() << " " << event->scenePos().y() << std::endl;
    ... some drawing functions like adding an ellipse ...
    }

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 16 Oct 2015, 11:36 last edited by
      #2

      Hi I dont know about mouse coors
      but Just wanted to make sure you saw
      Diagram Scene Example
      in case you could borrow stuff from that.

      1 Reply Last reply
      0

      1/2

      16 Oct 2015, 06:38

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved