Results 1 to 15 of 15

Thread: mouse tracking on image

  1. #1
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default mouse tracking on image

    Hi all,
    I have an image and want to enable tracking the mouse as you move around the screen or image. When I move the mouse on the image, the portion of the mouse pointer on image has to be displayed on other window.

    I want some idea about how I can do it using qt3.3.x

    Some more info-->
    I have created a QMainwindow, a widget where the image is displayed, The mainwindow is the parent of widget. The zooming option on mouseWheel event is finished and I'm able to zoom the image.

    Please follow this link to get an idea about what I want
    http://www.iconico.com/magnifier/Magnifier.jpg

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: mouse tracking on image

    Could you state what the exact problem is?

  3. #3
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mouse tracking on image

    Quote Originally Posted by wysota View Post
    Could you state what the exact problem is?
    The problem is I want to implement the above discussed scenario but not getting how to achieve it.

    Whenever I move a mouse on the image, the mouse pointer area has to be shown on another window. Please see the link.

    In the image the person running is my image. When I move the mouse near his face, that portion of image is shown on another window. Can you tell me how I can do that????

    I'm trying out something and will send the code once it is ready. But do provide some initiation.

    Thanks

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: mouse tracking on image

    Get the pointer coordinates in the mouseMoveEvent and display a portion of the image on some other widget. There is a tool "pixelator" in Qt4 that does what you want. You may want to take a look at its code.

  5. #5
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mouse tracking on image

    Quote Originally Posted by wysota View Post
    Get the pointer coordinates in the mouseMoveEvent and display a portion of the image on some other widget. There is a tool "pixelator" in Qt4 that does what you want. You may want to take a look at its code.
    Here is the code I have tried.... But unlucky...

    Qt Code:
    1. #include <qpixmap.h>
    2. #include <qpainter.h>
    3.  
    4. #include <qimage.h>
    5. #include <qcursor.h>
    6. #include "PixmapWidget.h"
    7.  
    8. PixmapWidget::PixmapWidget( const char* fileName, QWidget* w, QWidget *parent )
    9. : QWidget( parent )
    10. {
    11. m_zoomWindow = w;
    12. oldRect = QRect();
    13.  
    14. pasteRect = QRect(this->geometry().left(), this->geometry().top(), 100, 100);
    15. m_pm = 0;
    16.  
    17. QImage img(fileName);//fileName is the name of image
    18.  
    19. if( ! img.isNull() )
    20. {
    21. m_pm = new QPixmap( img );
    22. }
    23.  
    24. zoomFactor = 0.30001;
    25. setMinimumSize( m_pm->width()*zoomFactor, m_pm->height()*zoomFactor );
    26. setMouseTracking(true);
    27. }
    28.  
    29. PixmapWidget::~PixmapWidget()
    30. {
    31. delete m_pm;
    32. }
    33.  
    34. void PixmapWidget::paintEvent( QPaintEvent *event )
    35. {
    36. QPainter p( this );
    37. p.save();
    38. p.scale( zoomFactor, zoomFactor );
    39. p.drawPixmap( 0, 0, *m_pm );
    40. p.restore();
    41. }
    42.  
    43. void PixmapWidget::mouseMoveEvent( QMouseEvent *e )
    44. {
    45. //Show a rectangle for debugging. This code will be removed later
    46. // The rectangle is shown on image to know how much area of image is
    47. //covered
    48. QPen pen( foregroundColor(), 1 );
    49. QPainter paint;
    50. paint.begin( this );
    51. paint.setPen( pen );
    52. paint.setRasterOp( NotXorROP );
    53. // erase the oldRect
    54. paint.drawRect( oldRect );
    55. pasteRect.moveTopLeft( ( ((QMouseEvent *)e)->pos() ) );
    56. paint.drawRect( pasteRect );
    57. oldRect = pasteRect;
    58. paint.end();
    59.  
    60. //Draw the image on zoomWindow which is window2
    61.  
    62. //I'm stuck here!!!!
    63. //how do I get the part of image where the mouse pointer is
    64. //
    65. p.begin( m_zoomWindow );
    66. p.setRasterOp( NotXorROP );
    67. p.drawPixmap( pasteRect, *m_pm );
    68. p.end();
    69. }
    To copy to clipboard, switch view to plain text mode 

    I'll go through "pixelator" in Qt4 and find out if I can solve this problem...
    Any idea in the above code about how I can fix.

    Thanks

  6. #6
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mouse tracking on image

    Hi wysota, I got it... I used grabWidget to get the image defined in QPixmap

    Thanks...

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: mouse tracking on image

    Why not inherit QLabel instead of QWidget and just get the pixmap?
    Or even access the image directly as you have it stored as a class member.
    Last edited by wysota; 27th March 2007 at 12:03.

  8. #8
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mouse tracking on image

    Quote Originally Posted by wysota View Post
    Why not inherit QLabel instead of QWidget and just get the pixmap?
    Or even access the image directly as you have it stored as a class member.
    I'm implementing zooming options also.. The mouse tracking option is one of the feature of the module. I'll get back to you if I get stuck again.

    Thanks and have a good day ahead!!!!!

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: mouse tracking on image

    I meant to not use grabWidget to get the image as you already have it. Why duplicate it?

  10. #10
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mouse tracking on image

    Quote Originally Posted by wysota View Post
    I meant to not use grabWidget to get the image as you already have it. Why duplicate it?
    The m_pm pixmap is the image in one window where I move the mouse. Then in other window 'zoomWindow', the area of the mouse pointer covered is shown. For this I need to have a part of m_pm pixmap only. This part is basically the 'pasteRect'

    I used grabwidget to get that part 'pasteRect' of area from m_pm.

    If still I'm wrong anywhere, please correct me. If possible with modification in above code.
    I'll be thankful to you.

    Thanks

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: mouse tracking on image

    Example attached. Scaling needs a little more work, as it ignores margins now (which causes garbage or doesn't allow to see all the image).

    The code is for Qt4, although it should work with Qt3 as well. Just change the included files and the painter scaling and translating code.
    Attached Files Attached Files

  12. The following user says thank you to wysota for this useful post:

    vermarajeev (28th March 2007)

  13. #12
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mouse tracking on image

    Quote Originally Posted by wysota View Post
    Example attached. Scaling needs a little more work, as it ignores margins now (which causes garbage or doesn't allow to see all the image).

    The code is for Qt4, although it should work with Qt3 as well. Just change the included files and the painter scaling and translating code.
    Hi wysota, thanks once again.

    I saw the example program. I was unable to see the ouput coz of this function

    Qt Code:
    1. void buildImage()
    2. {
    3. const QPixmap *px = lab->pixmap();
    4. int w = qMax(m_pt.x()-(width()/2), 0);
    5. int h = qMax(m_pt.y()-(height()/2), 0);
    6. img = px->copy(w, h, width(), height());
    7. }
    To copy to clipboard, switch view to plain text mode 

    What I think is (might not be correct ) the logic what you have writtten and mine is almost same except that you have used eventFiler and I used mouseMoveEvent.

    The major difference is in the above function. Instead of copy (which is not available in Qt3) I used grabWidget ( QWidget * widget, int x = 0, int y = 0, int w = -1, int h = -1 ), which I think is quite similar to Pixmap::copy in qt4.

    The other best thing about your code was that you used label which I never cared. Anyway it was a good learning experience. Thanks a lot.
    Last edited by vermarajeev; 28th March 2007 at 05:24.

  14. #13
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mouse tracking on image

    Hi wysota!
    I have installed qt4.2.2 on my system and is able to see the output for Example program. It looks good. Thanks!!!!

    Now I should first start porting qt3.x.x to qt4.x.x. What I'l do is first take a sample helloWorld program from qt3 and port it to qt4. Then see what changes I need to make to port qt3 to qt4.

    Have a great day!!!!

  15. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: mouse tracking on image

    Quote Originally Posted by vermarajeev View Post
    What I think is (might not be correct ) the logic what you have writtten and mine is almost same except that you have used eventFiler and I used mouseMoveEvent.
    That's not the point, although I think my approach is better as you can "spy" on a widget that doesn't know about it.

    The major difference is in the above function. Instead of copy (which is not available in Qt3) I used grabWidget ( QWidget * widget, int x = 0, int y = 0, int w = -1, int h = -1 ), which I think is quite similar to Pixmap::copy in qt4.
    No, it's not simmilar. You should use QImage::copy() or copyBlt().

    The main difference is I'm accessing the image in question directly, whereas you're grabbing the image by redirecting a paint event.

  16. #15
    Join Date
    Apr 2010
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: mouse tracking on image

    thank you guys for the QEventFilter, i never knew about it it solved my problem.

    regards.

Similar Threads

  1. Replies: 7
    Last Post: 8th September 2006, 16:19
  2. Replies: 2
    Last Post: 24th July 2006, 18:36
  3. setCanvas blocks mouse movement on QtCanvasView
    By YuriyRusinov in forum Qt Programming
    Replies: 8
    Last Post: 20th April 2006, 07:38
  4. How and when to repaint a widget ?
    By yellowmat in forum Newbie
    Replies: 7
    Last Post: 3rd April 2006, 16:36
  5. [QT3+XP] transparency and mouse tracking
    By incapacitant in forum Newbie
    Replies: 9
    Last Post: 17th February 2006, 18:49

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.