Results 1 to 8 of 8

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

  1. #1

    Question how to get color at coordinate when i press mouse on canvasview or widget?

    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?

  2. #2
    Join Date
    Jan 2007
    Posts
    177
    Thanks
    8
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to get color at coordinate when i press mouse on canvasview or widget?

    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.

  3. #3
    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: how to get color at coordinate when i press mouse on canvasview or widget?

    Quote Originally Posted by kstking View Post
    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?
    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.

  4. #4
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to get color at coordinate when i press mouse on canvasview or widget?

    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
    Image Analysis Development Framework Using Qt (IADFUQ)

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to get color at coordinate when i press mouse on canvasview or widget?

    Depends how you access the coordinates, but mostly you will get scene coordinates.

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: how to get color at coordinate when i press mouse on canvasview or widget?

    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 docs explain the coordinate system in detailed level.
    J-P Nurmi

  7. The following user says thank you to jpn for this useful post:

    sincnarf (16th October 2007)

  8. #7

    Default Re: how to get color at coordinate when i press mouse on canvasview or widget?

    this is my code:
    Qt Code:
    1. void FigureEditor::contentsMousePressEvent(QMouseEvent* e)
    2. {
    3. QPoint p = inverseWorldMatrix().mp(e->pos());
    4. QPoint tmpPointEdit = e->pos();
    5. QCanvasItemList l=canvas()->collisions(p);
    6. QCanvasItemList::Iterator it = l.begin();
    7. ImageItem *item;
    8. for (it; it!=l.end(); ++it) {
    9. if ( (*it)->rtti() == imageRTTI ) {
    10. item = (ImageItem*)(*it);
    11. if ( !item->hit( p ) )
    12. continue;
    13. }
    14.  
    15. tmpPointEdit = p;
    16. moving = *it;
    17. moving_start = p;
    18. break;
    19. }
    20. int ix = tmpPointEdit.x();
    21. int iy = tmpPointEdit.y();
    22. QImage *img = (QImage*)(*it);
    23. QRgb newPixel = img -> pixel( ix, iy );
    24. }
    To copy to clipboard, switch view to plain text mode 
    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?

  9. #8
    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: how to get color at coordinate when i press mouse on canvasview or widget?

    But could you answer the question why you want to know the colour of the pixel under cursor?

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.