Results 1 to 13 of 13

Thread: Draw rectangle on QImage

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    70
    Thanked 59 Times in 57 Posts

    Default Draw rectangle on QImage

    Hi,

    I'm trying to draw a rectangle into a QImage using QPainter but I don't know what I'm doing wrong. This code is executed out of the paintEvent

    Qt Code:
    1. QPainter qPainter(&qImage);
    2. qPainter.setBrush(Qt::NoBrush);
    3. qPainter.setPen(Qt::red);
    4. qPainter.drawRect(iX,iY,iWidth,iHeight);
    5. ui.imageLabel->setPixmap(QPixmap::fromImage(qImage));
    To copy to clipboard, switch view to plain text mode 

    The image is showed but not the rectangle.
    "iX,iY,iWidth,iHeight" have values into the image size.

    Thanks,
    Òscar Llarch i Galán

  2. #2
    Join Date
    Jul 2012
    Posts
    123
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    4
    Thanked 2 Times in 2 Posts

    Default Re: Draw rectangle on QImage

    because you must at first set image and after that draw rectangle because you overflow your retangle with image
    Last edited by Viper666; 24th January 2013 at 18:25.

  3. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    70
    Thanked 59 Times in 57 Posts

    Default Re: Draw rectangle on QImage

    Hi,

    The image is readed before painting the rectangle
    Òscar Llarch i Galán

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Draw rectangle on QImage

    Try calling qPainter.end() before converting the image to a pixmap.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    70
    Thanked 59 Times in 57 Posts

    Default Re: Draw rectangle on QImage

    Hi,

    Try calling qPainter.end() before converting the image to a pixmap.
    Same result. I've also tryied to save the image on disk after calling "qPainter.end()" but the image is the same as the input image.

    Maybe there is a better way to do what I want:
    I have an image loaded from a Database(the image is loaded correctly). The Database also returns me a position and dimension of a rectangle that I want to show over the image. The image is painted on a QLabel scaling it to fill the QLabel dimension(mantaining the aspect ratio of the image), so the position to paint the rectangle are different. This is why I want to paint the rectangle directly into the image before scaling the image.

    Thanks,
    Last edited by ^NyAw^; 25th January 2013 at 09:12.
    Òscar Llarch i Galán

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Draw rectangle on QImage

    Please provide some minmal compilable example reproducing the problem.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    70
    Thanked 59 Times in 57 Posts

    Default Re: Draw rectangle on QImage

    Hi,

    I've tried this code in the constructor of a simple program.
    Qt Code:
    1. QImage qImage("a.bmp");
    2. QPainter qPainter(&qImage);
    3. qPainter.setBrush(Qt::NoBrush);
    4. qPainter.setPen(Qt::red);
    5. qPainter.drawRect(150,150,50,50);
    6. bool bEnd = qPainter.end();
    7. qImage.save("b.bmp");
    To copy to clipboard, switch view to plain text mode 

    With this I'll get the image "b.bmp" that is the same as "a.bmp" but with a red rectangle painted on position (150px,150px) with a widht and height of 50px.
    This don't work as "b.bmp" is identical as "a.bmp"

    Thanks,
    Òscar Llarch i Galán

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Draw rectangle on QImage

    What is the format of the BMP image? Is it 8b (indexed) or 24b (true color)?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 2
    Last Post: 9th November 2012, 17:16
  2. Replies: 2
    Last Post: 4th May 2012, 10:10
  3. How to draw a QPainter rectangle over Widgets
    By sureshdharmadurai in forum Qt Programming
    Replies: 1
    Last Post: 24th November 2011, 10:17
  4. How to draw rectangle with moveable edge
    By sagirahmed in forum Qt Programming
    Replies: 4
    Last Post: 2nd July 2010, 06:21
  5. how to draw columns in QPainter's rectangle
    By jyoti in forum Qt Programming
    Replies: 1
    Last Post: 24th August 2007, 10:50

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.