Results 1 to 13 of 13

Thread: Draw rectangle on QImage

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

    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
    Thanks
    4
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    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
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    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,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: 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
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    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,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: 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
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    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,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: 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.


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

    Default Re: Draw rectangle on QImage

    Hi,

    It is 8 byte indexed image


    Added after 5 minutes:


    Hi,

    Thanks Wysota. This is the problem. I've transformed the image to a 32 bit RGB and now it works.
    I had not thought that the image was indexed.
    Last edited by ^NyAw^; 25th January 2013 at 10:59.
    Òscar Llarch i Galán

  10. #10
    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: Draw rectangle on QImage

    Ok, that explains everything In that case you need to use colours from the colour table assigned to the image. Whenever you want to pass a colour, you pass the index from the colour table (0-255) instead of the real colour. Since "red" translates to QColor("red") which in turn translates to QRgb = #00FF0000 = 16711680, the value you are passing is out of scope of the colour table and thus you get a transparent colour.

    The easiest solution I can think of is to use QPixmap instead of QImage. Other than that, you can either do a lookup in the colour table (QImage::color()) to find "red" there (if it exists) or convert the indexed image to 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.


  11. The following user says thank you to wysota for this useful post:

    ^NyAw^ (25th January 2013)

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

    Default Re: Draw rectangle on QImage

    Hi,

    Converting the image to a 32 bit RGB is working. For the moment I don't need speed up my application but I will cosider to use QPixmap instead of QImage.

    Thanks,
    Òscar Llarch i Galán

  13. #12
    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: Draw rectangle on QImage

    QPixmap will also convert your image to (most probably) 24 bit.
    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.


  14. The following user says thank you to wysota for this useful post:

    ^NyAw^ (25th January 2013)

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

    Default Re: Draw rectangle on QImage

    Hi,

    Finally I decided to change the way of displaying the rectangle on the image.
    Now I'm using the "paintEvent":
    Qt Code:
    1. void imageLabel::paintEvent(QPaintEvent *event)
    2. {
    3. QLabel::paintEvent(event);
    4. if (!m_qImage.isNull())
    5. {
    6. QImage qImageScaled = m_qImage.scaled(QSize(width(),height()),Qt::KeepAspectRatio,Qt::FastTransformation);
    7. double dAspectRatio = (double)qImageScaled.width()/(double)m_qImage.width();
    8. int iX = m_iX*dAspectRatio;
    9. int iY = m_iY*dAspectRatio;
    10. int iWidth = m_iWidth*dAspectRatio;
    11. int iHeight = m_iHeight*dAspectRatio;
    12.  
    13. QPainter qPainter(this);
    14. qPainter.drawImage(0,0,qImageScaled);
    15. qPainter.setBrush(Qt::NoBrush);
    16. qPainter.setPen(Qt::red);
    17. qPainter.drawRect(iX,iY,iWidth,iHeight);
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    Using the aspect ratio I can calculate the new position and dimension of the rectangle.
    I use this way because painting on the image can get a rectangle that is not visible depending on the widht of the pen and the new size of the image. In this way the rectangle always is 1px width and displayed well.

    Thanks anyway,
    Òscar Llarch i Galán

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.