PDA

View Full Version : how to get color at coordinate when i press mouse on canvasview or widget?



kstking
14th October 2007, 05:21
I want to know colors at my coordinates when i press mouse on canvasview or widget. how can i do?
i don't know all function to do that. Would you please give me some examples?:crying:

kernel_panic
14th October 2007, 09:21
you could grab you widget and convert the pixmap into QImage. then you get the pixel under the mouse and get the color of the pixel. i don't know if it works, you have to try.

wysota
14th October 2007, 10:56
I want to know colors at my coordinates when i press mouse on canvasview or widget. how can i do?
i don't know all function to do that. Would you please give me some examples?:crying:

We tend not to do things this way. What exactly are you trying to achieve? If you use a canvas, you can access a pointer to the item under cursor and then you can often know the colour right away based on the item properties or you can implement your items in a smart way so that it can tell you what colour it has at a specified coordinate.

sincnarf
14th October 2007, 16:35
I am currently trying to implement the same thing only on a QGraphicsView. I have a question though, when you click on an item in the graphics view, what coordinates do you get? the coordinates of the image or the graphicsview's scene? i don't get it

marcel
14th October 2007, 17:09
Depends how you access the coordinates, but mostly you will get scene coordinates.

jpn
14th October 2007, 17:09
A QGraphicsItem receives a QGraphicsSceneMouseEvent which has methods to get the position in any coordinates you want.

QGraphicsSceneMouseEvent::pos():

Returns the mouse cursor position in item coordinates.
QGraphicsSceneMouseEvent::scenePos():

Returns the mouse cursor position in scene coordinates.
QGraphicsSceneMouseEvent::screenPos():

Returns the mouse cursor position in screen coordinates.
The Graphics View Coordinate System (http://doc.trolltech.com/latest/graphicsview.html#the-graphics-view-coordinate-system) docs explain the coordinate system in detailed level.

kstking
14th October 2007, 18:17
this is my code:


void FigureEditor::contentsMousePressEvent(QMouseEvent* e)
{
QPoint p = inverseWorldMatrix().mp(e->pos());
QPoint tmpPointEdit = e->pos();
QCanvasItemList l=canvas()->collisions(p);
QCanvasItemList::Iterator it = l.begin();
ImageItem *item;
for (it; it!=l.end(); ++it) {
if ( (*it)->rtti() == imageRTTI ) {
item = (ImageItem*)(*it);
if ( !item->hit( p ) )
continue;
}

tmpPointEdit = p;
moving = *it;
moving_start = p;
break;
}
int ix = tmpPointEdit.x();
int iy = tmpPointEdit.y();
QImage *img = (QImage*)(*it);
QRgb newPixel = img -> pixel( ix, iy );
}

this code is one part of example: http://doc.trolltech.com/3.3/canvas-example.html
i have edited code in pressmouse method .
RUN:
+ First: i draw Rect with Red color on canvas. and use canvasview to show it.
+Second: i click on Rect. I want to get color at position when i press mouse. And save it into newPixel variable.
--> i wrong?

wysota
14th October 2007, 23:04
But could you answer the question why you want to know the colour of the pixel under cursor?