Results 1 to 2 of 2

Thread: Display cursor position in QGraphicsView

  1. #1
    Join Date
    Oct 2008
    Posts
    74
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Display cursor position in QGraphicsView

    Hi

    I need a bit of help displaying the cursor position of a pixel in a pixmap (4k x 1k pixels) using QGraphicsView . I have subclassed QGraphicsView

    Qt Code:
    1. class viewWidget : public QGraphicsView
    2. {
    3. Q_OBJECT;
    4. public:
    5. viewWidget(QWidget* parent =0);
    6. virtual ~ viewWidget();
    7.  
    8. void paintEvent(QPaintEvent* event);
    9. void mouseMoveEvent(QMouseEvent* event);
    10. void mousePressEvent(QMouseEvent* event);
    11.  
    12. signals:
    13. void grabPosition(const QPoint);
    14.  
    15. private:
    16. QPoint p;
    17. QString posString;
    18. float xScale;
    19. float yScale;
    20. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. viewWidget::viewWidget(QWidget* parent )
    2. :QGraphicsView(parent),
    3. xScale(1.0),
    4. yScale(1.0)
    5. {
    6. setMouseTracking(true);
    7. }
    8.  
    9. viewWidget::~viewWidget(){};
    10.  
    11. void
    12. viewWidget::paintEvent(QPaintEvent* event)
    13. {
    14. QGraphicsView::paintEvent(event);
    15. QPainter paint(this);
    16. paint.setPen(QColor(Qt::yellow));
    17. paint.drawText(p,posString);
    18. }
    19.  
    20. void
    21. viewWidget::mouseMoveEvent(QMouseEvent* event)
    22. {
    23. if (scene()==NULL)
    24. return;
    25.  
    26. p=mapToScene(event->pos());
    27. posString = QString::number( (p.x() ) + "," + QString::number( p.y() );
    28.  
    29. cout << qPrintable(posString) << endl;
    30. update();
    31. }
    To copy to clipboard, switch view to plain text mode 

    I create a QGraphicsScene from a pixmap and add it to the view as shown

    Qt Code:
    1. view=new viewWidget(this); //Create a view widget
    2. scene=new QGraphicsScene(view);
    3. scene->addPixmap(QPixmap::fromImage(*image));
    4. view->setScene(scene);
    5. view->show();
    To copy to clipboard, switch view to plain text mode 

    However the pixel position is not shown on the view, I get an error from the debugger

    warning: QPainter::setPen: Painter not active

    What am I doing wrong?

    Thanks
    Last edited by dbrmik; 2nd April 2009 at 14:44. Reason: updated contents

  2. #2
    Join Date
    Oct 2008
    Posts
    74
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Display cursor position in QGraphicsView

    Hi

    Do I need to subclass QGraphicsScene and re-implement the painttEvent and mouseMoveEvent ? I can display the pixel positon in the status bar, bit I would like it attached the the cursor.

    Thanks

Similar Threads

  1. Replies: 0
    Last Post: 5th March 2009, 06:54
  2. MDI display Item in QGraphicsView
    By wisconxing in forum Qt Programming
    Replies: 1
    Last Post: 7th November 2008, 00:11
  3. Replies: 0
    Last Post: 18th June 2008, 11:00
  4. Display only PNG image on desktop
    By durbrak in forum Qt Programming
    Replies: 32
    Last Post: 15th March 2008, 21:55
  5. QGraphicsView and QGraphicsScene speeds
    By jnk5y in forum Qt Programming
    Replies: 2
    Last Post: 20th October 2006, 07:13

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.