Re: mouse tracking on image
Could you state what the exact problem is?
Re: mouse tracking on image
Quote:
Originally Posted by
wysota
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
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.
Re: mouse tracking on image
Quote:
Originally Posted by
wysota
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...
Code:
#include <qpixmap.h>
#include <qpainter.h>
#include <qimage.h>
#include <qcursor.h>
#include "PixmapWidget.h"
PixmapWidget
::PixmapWidget( const char* fileName,
QWidget* w,
QWidget *parent
) {
m_zoomWindow = w;
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() )
{
}
zoomFactor = 0.30001;
setMinimumSize( m_pm->width()*zoomFactor, m_pm->height()*zoomFactor );
setMouseTracking(true);
}
PixmapWidget::~PixmapWidget()
{
delete m_pm;
}
{
p.save();
p.scale( zoomFactor, zoomFactor );
p.drawPixmap( 0, 0, *m_pm );
p.restore();
}
{
//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 );
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
//
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
Re: mouse tracking on image
Hi wysota, I got it... I used grabWidget to get the image defined in QPixmap
Thanks...
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.
Re: mouse tracking on image
Quote:
Originally Posted by
wysota
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!!!!!
Re: mouse tracking on image
I meant to not use grabWidget to get the image as you already have it. Why duplicate it?
Re: mouse tracking on image
Quote:
Originally Posted by
wysota
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
1 Attachment(s)
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.
Re: mouse tracking on image
Quote:
Originally Posted by
wysota
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
Code:
void buildImage()
{
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.
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!!!!
Re: mouse tracking on image
Quote:
Originally Posted by
vermarajeev
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.
Quote:
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.
Re: mouse tracking on image
thank you guys for the QEventFilter, i never knew about it it solved my problem.
regards.