Results 1 to 3 of 3

Thread: select QPixmap area with mouse

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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: select QPixmap area with mouse

    You have to subclass QLabel and reimplement mousePressEvent, mouseMoveEvent and paintEvent.

    In mousePressEvent: store the crop coordinates in a QRect or two QPoint's. The local coordinates are stored in the event.
    When you receive the right click(you should implement some kind of click counting - a boolean flag, etc) you have the two local coordinates. Now you can crop the pixmap and set it in the label.

    In mouseMoveEvent: after you have the coordinate for the left mouse click you can update the second coordinate with the current mouse position stored in the event.

    In paintEvent: draw the crop rectangle specified by the left click position and right click position or current mouse position(if you didn't have a right click yet). Note that you first have to call QLabel:aintEvent and then draw your crop rectangle, because you want the label to be able to draw its initial contents.


    As for storing the pixmaps, you can do that with a 1 QPixmap member, for the original image.

  2. The following user says thank you to marcel for this useful post:

    tommy (16th November 2007)

  3. #2
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default thanks marcel

    Thanks for your useful answer, I now know where to kind of start. My only problem is that I'm not at that level yet. I don't fully understand all the aspects of the general outline you provided.
    May I ask you to be a bit more specific. My general questions are:
    1. OK, I subclass QLabel. I guess I do it exacly like I subclassed MyWidget in the above example. So I guess the QLabel subclass is going to hold (under protected mousePressEvent(), mouseMoveEvent() and paintEvent(). Is it also going to create the m_pixmapLabel instance? If I put this in the QLabel subclass it needs to be in the appropriate constructor, meaning that I cannot add it to my mainLayout (which is subclassed in MyWidget).
    2. Am I still going to have the MyWidget subclass for the pushbutton and everything else that is not the QLabel where the picture is going to appear?
    3. How do I make the objects of the two different subclasses to interact? I mean how do I ensure that the mouse is going to work on the right object?
    4. I guess my questions mostly all boil down to the fact that I am not getting how the mouse events work. Mouse seems to behave very differently from the usual signal/slot.
    5. Maybe the easiest thing for me would be to look at some example code where people have done a similar thing. Do you know of any such postings.
    Thanks a lot!

Similar Threads

  1. Weird behaviour of mouse events and QMenu pop-ups
    By Ishark in forum Qt Programming
    Replies: 1
    Last Post: 7th August 2007, 07:46
  2. select a particular area
    By vishesh in forum Qt Programming
    Replies: 4
    Last Post: 28th February 2007, 21:44

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
  •  
Qt is a trademark of The Qt Company.