PDA

View Full Version : mouse tracking on image



vermarajeev
27th March 2007, 05:32
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

wysota
27th March 2007, 07:53
Could you state what the exact problem is?

vermarajeev
27th March 2007, 08:23
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

wysota
27th March 2007, 10:10
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.

vermarajeev
27th March 2007, 10:19
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...


#include <qpixmap.h>
#include <qpainter.h>

#include <qimage.h>
#include <qcursor.h>
#include "PixmapWidget.h"

PixmapWidget::PixmapWidget( const char* fileName, QWidget* w, QWidget *parent )
: QWidget( parent )
{
m_zoomWindow = w;
oldRect = QRect();

pasteRect = QRect(this->geometry().left(), this->geometry().top(), 100, 100);
m_pm = 0;

QImage img(fileName);//fileName is the name of image

if( ! img.isNull() )
{
m_pm = new QPixmap( img );
}

zoomFactor = 0.30001;
setMinimumSize( m_pm->width()*zoomFactor, m_pm->height()*zoomFactor );
setMouseTracking(true);
}

PixmapWidget::~PixmapWidget()
{
delete m_pm;
}

void PixmapWidget::paintEvent( QPaintEvent *event )
{
QPainter p( this );
p.save();
p.scale( zoomFactor, zoomFactor );
p.drawPixmap( 0, 0, *m_pm );
p.restore();
}

void PixmapWidget::mouseMoveEvent( QMouseEvent *e )
{
//Show a rectangle for debugging. This code will be removed later
// The rectangle is shown on image to know how much area of image is
//covered
QPen pen( foregroundColor(), 1 );
QPainter paint;
paint.begin( this );
paint.setPen( pen );
paint.setRasterOp( NotXorROP );
// erase the oldRect
paint.drawRect( oldRect );
pasteRect.moveTopLeft( ( ((QMouseEvent *)e)->pos() ) );
paint.drawRect( pasteRect );
oldRect = pasteRect;
paint.end();

//Draw the image on zoomWindow which is window2

//I'm stuck here!!!!
//how do I get the part of image where the mouse pointer is
//
QPainter p;
p.begin( m_zoomWindow );
p.setRasterOp( NotXorROP );
p.drawPixmap( pasteRect, *m_pm );
p.end();
}

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

vermarajeev
27th March 2007, 11:07
Hi wysota, I got it... I used grabWidget to get the image defined in QPixmap

Thanks...

wysota
27th March 2007, 11:56
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.

vermarajeev
27th March 2007, 12:24
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!!!!!

wysota
27th March 2007, 12:26
I meant to not use grabWidget to get the image as you already have it. Why duplicate it?

vermarajeev
27th March 2007, 12:51
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

wysota
27th March 2007, 14:55
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.

vermarajeev
28th March 2007, 05:13
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


void buildImage()
{
const QPixmap *px = lab->pixmap();
int w = qMax(m_pt.x()-(width()/2), 0);
int h = qMax(m_pt.y()-(height()/2), 0);
img = px->copy(w, h, width(), height());
}

What I think is (might not be correct :D ) 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.

vermarajeev
28th March 2007, 07:39
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!!!!

wysota
28th March 2007, 07:55
What I think is (might not be correct :D ) 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.

Not Fast Enough
12th May 2010, 13:06
thank you guys for the QEventFilter, i never knew about it it solved my problem.

regards.