Results 1 to 11 of 11

Thread: PNG file is not rendered properly when using as texture for QBrush

  1. #1
    Join Date
    Sep 2010
    Posts
    11
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default PNG file is not rendered properly when using as texture for QBrush

    Hi all,

    I would like to add a rectangle with a textured brush pattern to a QGraphicsScene. However, the rendering is not working out for some reason.

    Here's the code where I add the rectangle to the scene.

    Qt Code:
    1. QPixmap pixmap = QPixmap(":/images/TexturePattern.png");
    2. QBrush brush = QBrush(QPixmap(pixmap));
    3.  
    4. mainRect = new QGraphicsRectItem(0,0,960,1300);
    5. mainRect->setBrush(brush);
    6. mainRect->setPen(QPen(QBrush(QColor("Gray")), 3));
    7. mainRect->setVisible(true);
    8. addItem(mainRect);
    To copy to clipboard, switch view to plain text mode 

    Here's a piece of the resulting image:
    BadPattern.png

    I substituted my image into the Painting example Basic Shapes, (substituted texturePattern.png for Brick.png) and it seems to render just fine:
    OkPattern.jpg

    Other than their using a Widget and I'm using a QGraphicsScene, I can't seem to find any difference in the code. Does anyone know why?

    Thanks in advance.

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

    Default Re: PNG file is not rendered properly when using as texture for QBrush

    Do you do any scaling of the scene/item/view?
    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.


  3. #3
    Join Date
    Sep 2010
    Posts
    11
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: PNG file is not rendered properly when using as texture for QBrush

    Yes, the QGraphicsView is scaled using a matrix.

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

    Default Re: PNG file is not rendered properly when using as texture for QBrush

    That's why your brush gets distorted - it's being scaled as well.
    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
    Sep 2010
    Posts
    11
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: PNG file is not rendered properly when using as texture for QBrush

    I am scaling the view by the same factor in x and y. I would expect that this would do the same to the brush, so there should be no distortions; But it appears not to be the case.

    Could you suggest a way to counter the scaling on the brush, or to compensate the scaling on the pixmap so that it does not get distorted?

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

    Default Re: PNG file is not rendered properly when using as texture for QBrush

    I think that if you have dotted pattern on the pixmap, you will alwys get distortions if the effective scaling factor is different than 1:1. You can apply the pattern after scaling the item but it requires a bit of calculations and code writing. You'd have to reverse the scaling factor in the painting routine of the item so that you fill its area as it weren't scaled while maintaining the scale of the item itself.
    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. The following user says thank you to wysota for this useful post:

    skeletor (23rd September 2010)

  8. #7
    Join Date
    Sep 2010
    Posts
    11
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: PNG file is not rendered properly when using as texture for QBrush

    Thanks for your ideas, but I'd rather not take that approach.

    I was expecting that Qt would have this support built in somehow; if not, it seems like a bug (or pretty big limitation) to me. They should probably have mentioned this kind of limitation in the documentation for QBrush, to save me wasting so much time trying to figure out why my simple pattern wouldn't render!

    I'll try to design a texture without little dots, or go with a solid fill.

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

    Default Re: PNG file is not rendered properly when using as texture for QBrush

    Quote Originally Posted by skeletor View Post
    if not, it seems like a bug (or pretty big limitation) to me.
    Blame God, this is a law of physics. If what you are experiencing is what I think it is (because the image you posted is quite small and I can't see the details).

    They should probably have mentioned this kind of limitation in the documentation for QBrush, to save me wasting so much time trying to figure out why my simple pattern wouldn't render!
    Actually you should know on your own that 1 divided by a number larger than 1 is less than 1 and "less than 0" often means "0" in computer science. This is not caused by Qt, this is caused by the fact that you scale the image. You will have the same effect if you take some graphics application and do a similar scaling there (especially if you use fast scaling algorithm).
    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.


  10. #9
    Join Date
    Sep 2010
    Posts
    11
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: PNG file is not rendered properly when using as texture for QBrush

    Thanks for clarifying. I thought you meant that only images with little dots would not be scaled properly. which seemed to match with my testing where other PNG images were being scaled down nicely. Now I understand what you meant, and can see that this file was quite small to start with, compared to others that I was working with.

    I tried creating a larger version (e.g. 8 times larger), and it is scaling down better than before and is recognizable, but still gets noticeably distorted. I will just have to try to avoid using dots, which seem to be more distorted after the scaling than the other images I am working with. Or to use your other suggested coding approach.

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

    Default Re: PNG file is not rendered properly when using as texture for QBrush

    Did you tell the view to use smooth scaling transformations (it's a render hint)? It might help a 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.


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

    skeletor (27th September 2010)

  13. #11
    Join Date
    Sep 2010
    Posts
    11
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: PNG file is not rendered properly when using as texture for QBrush

    Thank you wysota. This has solved my problem and really improved the rendering of the pattern in the brush.

    1. Increase the original PNG file so that the pattern can physically be scaled down by the view.
    2. Improve the rendering done by the view by calling setRenderHint(QPainter::SmoothPixmapTransform, true);

Similar Threads

  1. Replies: 6
    Last Post: 4th September 2010, 22:19
  2. Replies: 14
    Last Post: 20th March 2010, 19:43
  3. changing svg graphic attributes once rendered
    By barrygp in forum Qt Programming
    Replies: 0
    Last Post: 13th December 2008, 02:09
  4. Webkit rendered fonts
    By carl in forum Qt Programming
    Replies: 0
    Last Post: 5th September 2008, 16:49
  5. QBrush texture pattern
    By edb in forum Qt Programming
    Replies: 4
    Last Post: 17th April 2007, 13:18

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.