Results 1 to 6 of 6

Thread: QGraphicsView - show mouse cursor position in status bar

  1. #1
    Join Date
    Nov 2011
    Location
    Brazil
    Posts
    10
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsView - show mouse cursor position in status bar

    First, thank you all for your questions and your answers, I've been learning a lot just by searching and finding perfect information in these forums.

    Now, my problem which I couldn't find the solution: I'm not successfully getting mouse cursor position in main window's status bar, based on its movement around a QGraphicsView. It was working perfect when I was using a QWidget instead of QGraphicsView, but I had to change it due to some project new requirements. Also, I should print to an external file the points that were clicked so far. That's not working anymore, too.

    The current symptom is: the mouse current coordinates are shown in the status bar, but only in the extremes, i.e., x<=2, y<=2, x>=right-2 or y>=bottom-2. Odd, isn't it?

    I've reimplemented QGraphicsScene and QGraphicsView this way:

    Qt Code:
    1. #ifndef WHITESPACESCENE_H
    2. #define WHITESPACESCENE_H
    3.  
    4. #include <QGraphicsScene>
    5.  
    6. class WhiteSpaceScene : public QGraphicsScene
    7. {
    8. Q_OBJECT
    9. public:
    10. WhiteSpaceScene (qreal x, qreal y, qreal width, qreal height, QObject * parent = 0);
    11.  
    12. protected:
    13. void mousePressEvent(QMouseEvent *event);
    14.  
    15. private:
    16. void printPoint(const QPoint &clickedPoint);
    17. };
    18.  
    19. #endif // WHITESPACESCENE_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "whitespacescene.h"
    2.  
    3. WhiteSpaceScene::WhiteSpaceScene (qreal x, qreal y, qreal width, qreal height, QObject * parent) :
    4. QGraphicsScene(x, y, width, height, parent)
    5. {
    6.  
    7. }
    8.  
    9. void WhiteSpaceScene::mousePressEvent(QMouseEvent *event)
    10. {
    11. if (event->button() == Qt::LeftButton)
    12. {
    13. printPoint(event->pos());
    14. }
    15. }
    16.  
    17. void WhiteSpaceScene::printPoint(const QPoint &clickedPoint)
    18. {
    19. ...
    20. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef WHITESPACEVIEW_H
    2. #define WHITESPACEVIEW_H
    3.  
    4. #include <QGraphicsView>
    5.  
    6. class WhiteSpaceView : public QGraphicsView
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit WhiteSpaceView(QGraphicsScene * scene, QWidget * parent = 0);
    11. };
    12.  
    13. #endif // WHITESPACEVIEW_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "whitespaceview.h"
    2.  
    3. WhiteSpaceView::WhiteSpaceView(QGraphicsScene * scene, QWidget * parent) :
    4. QGraphicsView(scene, parent)
    5. {
    6. setAttribute(Qt::WA_StaticContents); // won't change contents on widget resizing
    7. setRenderHint(QPainter::Antialiasing);
    8. setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
    9. setBackgroundBrush(QColor(255, 255, 255));
    10. setMouseTracking(true);
    11. setCursor(Qt::CrossCursor);
    12. }
    To copy to clipboard, switch view to plain text mode 
    In mainwindow.cpp I have an event filter which wasn't modified:
    Qt Code:
    1. bool MainWindow::eventFilter(QObject*, QEvent *event)
    2. {
    3. if (event->type() == QEvent::MouseMove)
    4. {
    5. QMouseEvent *mouseCursor = static_cast<QMouseEvent*>(event);
    6. statusBar()->showMessage(QString("(x,y) coordinates: (%1,%2)").arg(mouseCursor->x()).arg(mouseCursor->y()));
    7. }
    8. else if (event->type() == QEvent::Leave)
    9. {
    10. statusBar()->showMessage("");
    11. }
    12. return false;
    13. }
    To copy to clipboard, switch view to plain text mode 
    And finally:
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3. #include "whitespacescene.h"
    4. #include "whitespaceview.h"
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9. MainWindow *window = new MainWindow;
    10.  
    11. WhiteSpaceScene *whitespace = new WhiteSpaceScene(0, 0, 350, 350);
    12. WhiteSpaceView *whitespaceview = new WhiteSpaceView(whitespace);
    13. whitespaceview->installEventFilter(window);
    14. window->setCentralWidget(whitespaceview);
    15. window->show();
    16. return a.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 
    In the old main, I had a call to my subclassed QWidget (instead of two calls: QGraphicsScene and QGraphicsView) and the event filter was installed on it (subwidget->installEventFilter(window)).

    I've also tried to declare using standard QGraphicsView and QGraphicsScene to see if my subclasses were the problem, but the exact same symptom occurs.

    Any suggestions?
    Thanks a lot in advance.

  2. #2
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView - show mouse cursor position in status bar

    this is weird.. i have the same behaviour with the code you provided.
    I thought the event filter was called before the event handler (mouseMoveEvent in that case), but apparently this is not the case. If you overload this event handler, and ignore the event, then the event filter will receive what you want.

  3. #3
    Join Date
    Nov 2011
    Location
    Brazil
    Posts
    10
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView - show mouse cursor position in status bar

    Hi totem, thank you for reply.
    I should probably ask this in the newbie forum, but to stay on topic, how would I do what you suggested? Could you please share some code?

    Before I created the topic, I tried to overload mouseMoveEvent instead of using the filter, but then nothing happens at all (the same behaviour occured with QWidget, I had to use the event filter otherwise it didn't work at all).
    Last edited by mwgobetti; 19th November 2011 at 17:09.

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QGraphicsView - show mouse cursor position in status bar

    Try the following in main.cpp:
    Qt Code:
    1. whitespaceview->viewport()->installEventFilter(window);
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to norobro for this useful post:

    mwgobetti (19th November 2011)

  6. #5
    Join Date
    Nov 2011
    Location
    Brazil
    Posts
    10
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView - show mouse cursor position in status bar

    You're the man, worked flawlessly. Could you explain what's the deal with it?

  7. #6
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QGraphicsView - show mouse cursor position in status bar

    The QGraphicsView::viewport() is the widget that is actually displayed as the centralWidget in your MainWindow not the QGraphicsView itself. You can check this by adding the following to main.cpp:
    Qt Code:
    1. qDebug() << window->centralWidget()->children();
    2. qDebug() << whitespaceview->viewport();
    To copy to clipboard, switch view to plain text mode 


    With the above in mind, reread the QGraphicsView docs (here) and things should make sense.

  8. The following user says thank you to norobro for this useful post:

    mwgobetti (19th November 2011)

Similar Threads

  1. Replies: 5
    Last Post: 24th June 2011, 12:07
  2. Replies: 1
    Last Post: 10th July 2009, 09:54
  3. Display cursor position in QGraphicsView
    By dbrmik in forum Qt Programming
    Replies: 1
    Last Post: 2nd April 2009, 15:06
  4. Change cursor & status during Drag & Drop
    By ronlongo in forum Qt Programming
    Replies: 0
    Last Post: 1st December 2008, 16:56
  5. Replies: 7
    Last Post: 8th September 2006, 16:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.