Results 1 to 4 of 4

Thread: drawing an rectangle above a picture in a QLabel

  1. #1
    Join Date
    May 2007
    Posts
    33
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default drawing an rectangle above a picture in a QLabel

    hi,

    i have a QLabel and loaded a picture in it. Now i'd like to realize that a user can select a part of the picture. Therefore i captured the mouseevents over the label and could save the selection in another QLabel (like clipping).
    So far, everything works fine, but now i want to visualize the selection the user is doing while he has pressed the mouse over the picture. I tried using a QPainter like this:

    QRectF rectangle(leftUpperX, leftUpperY, rightLowerX, rightLowerY);
    QPainter painter(this);
    painter.drawRect(rectangle);


    In the mousemove-event i got no functionality and this error:

    QPainter::begin: Widget painting can only begin as a result of a paintEvent

    So i tried to put in into an paint-Event:

    void InitialLabel::paintEvent(QPaintEvent* event) {

    QRectF rectangle(10.0, 20.0, 200.0, 200.0);
    QPainter painter(this);
    painter.drawRect(rectangle); // drawing code

    }


    But now the picture is no longer shown, only the rectangle.

    What i would like is a variable rectangle while the mouse button is held that fixes it's position when the mouse button is released.
    This should be on top of the picture, so that a user can see the picture in the background while he makes his selection.

    Anyone who can help me with this?

    Thanks in advance.

  2. #2
    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: drawing an rectangle above a picture in a QLabel

    Yes.
    You should load the pixmap you want to draw, and in paintEvent first draw the pixmap then the rectangle over it, just as you did.

    The reason the pixmap disappeared is because you've overriden the paintEvent.

    Another solution is to leave your code just asi it is now, but before you draw anything in paint event, add the line QLabel:: paintEvent( event ). This way the base class will draw it's contents, and you can perform any custom drawing that you please.

    Regards

  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: drawing an rectangle above a picture in a QLabel

    First of all QLabel inherits QFrame, so you can just adjust the frameShape property. But to answer your question, you have to call the base class implementation to see both your drawing and the original one.

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drawing an rectangle above a picture in a QLabel

    I would suggest installing a filter on mousemovements and draw the rectangle using a rubberband. This will help u draw the rectangle independant of the picture being drawn.
    See QRubberBand for this... it will help u select the area in picture with the area being semi transparent showing the picture beneath it.

    Hope this helps

  5. The following user says thank you to aamer4yu for this useful post:

    harakiri (4th June 2007)

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.