Results 1 to 9 of 9

Thread: Not Scaling QBrush style fro qgraphics view item

  1. #1
    Join Date
    Jun 2008
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Not Scaling QBrush style fro qgraphics view item

    Hello,

    I am attempting to draw shapes qgraphics framework. I would like the brush style not to change as the viewport is zoomed. (ie I want the stippling pattern not to change)

    Does anybody have any suggestions on how I can achieve this?

    -Dan

  2. #2
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Not Scaling QBrush style fro qgraphics view item

    You'll have to scale the brush down. The QStyleOptionGraphicsItem option passed in paint() will give you the transform being used, and you can set a matrix on a QBrush. Put the two together and you can scale the brush opposite of the item.

    Qt Code:
    1. void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
    2. {
    3. QBrush brush(Qt::red, Qt::DiagCrossPattern);
    4. brush.setMatrix(option->matrix.inverted());
    5. painter->setBrush(brush);
    6.  
    7. painter->drawEllipse(-50, -50, 100, 100);
    8. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2008
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Not Scaling QBrush style fro qgraphics view item

    Hi,

    Thanks for you help but I cannot get your suggestion to work properly.
    I overloaded the paint method for a class derived from qgraphicsitem as you showed but this does not work for me. It seems that the brush pattern matrix does not have any effect at all.

    For example:
    Qt Code:
    1. void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
    2. {
    3. QBrush myBrush(Qt::red, Qt::DiagCrossPattern);
    4. QMatrix myMatrix(99,0,99,0,0,0);
    5. myBrush.setMatrix(myMatrix);
    6. painter->setBrush(myBrush);
    7. painter->drawEllipse(-50, -50, 100, 100);
    8. }
    To copy to clipboard, switch view to plain text mode 
    produces a shape the looks the same as:
    Qt Code:
    1. void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
    2. {
    3. QBrush myBrush(Qt::red, Qt::DiagCrossPattern);
    4. QMatrix myMatrix(1,0,1,0,0,0); //don't scale this time
    5. myBrush.setMatrix(myMatrix);
    6. painter->setBrush(myBrush);
    7. painter->drawEllipse(-50, -50, 100, 100);
    8. }
    To copy to clipboard, switch view to plain text mode 
    I am using QT4.3.
    Am I doing anything wrong here?
    I've tried both the Qt:DiagCrossPattern brush and also a custom brush made with a QImage.

    Thanks!
    Last edited by jpn; 3rd June 2008 at 08:22. Reason: disabled smilies

  4. #4
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Not Scaling QBrush style fro qgraphics view item

    Don't create your own matrix, use the one passed in option. That is the matrix the view uses. By taking the inverse matrix, you scale back down.

  5. #5
    Join Date
    Jun 2008
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Not Scaling QBrush style fro qgraphics view item

    Hi,

    Yes, I understand your solution but it does not work for me. I have traced the problem to the fact that brush transforms don't seem to work at all. That's what I was demonstrating in the previous code. Apparently this was a bug that was fixed in 4.3.1 (http://trolltech.com/developer/task-...ntry&id=129520).

    I was using 4.3.4 and am now downloading 4.4.0. Has anybody else used a qBrush transform? What version were you using? Am I not doing something proper?

    Thanks,
    Dan

  6. #6
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Not Scaling QBrush style fro qgraphics view item

    I am using 4.3.5 and 4.4.0. Which is why I didn't see that bug.

  7. #7
    Join Date
    Aug 2008
    Posts
    134
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Not Scaling QBrush style fro qgraphics view item

    Is this bug is fixed??? I ma using QT 4.4 and facing same problem. Any idea to resolve this?

  8. #8
    Join Date
    Sep 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Not Scaling QBrush style fro qgraphics view item

    Quote Originally Posted by Brandybuck View Post
    You'll have to scale the brush down. The QStyleOptionGraphicsItem option passed in paint() will give you the transform being used, and you can set a matrix on a QBrush. Put the two together and you can scale the brush opposite of the item.

    Qt Code:
    1. void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
    2. {
    3. QBrush brush(Qt::red, Qt::DiagCrossPattern);
    4. brush.setMatrix(option->matrix.inverted());
    5. painter->setBrush(brush);
    6.  
    7. painter->drawEllipse(-50, -50, 100, 100);
    8. }
    To copy to clipboard, switch view to plain text mode 
    there is no change after setting inverted matrix to brush? is there any other way?

  9. #9
    Join Date
    Apr 2012
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Not Scaling QBrush style fro qgraphics view item

    saics: you're not checking whether the matrix was inverted. If it is not invertible, the identity matrix is returned, which would give unexpected results.

    I am just guessing, but it might be just the scaling that needs to be inverted? The matrix from option is the cumulative transform (includes the viewing transform and all parent to child transforms) and could be complex. You might try extracting the scale, and creating a new matrix that just inverts the scaling. I would be more definitive, but I am working through the same problem now.

    saics: you're not checking whether the matrix was inverted. If it is not invertible, the identity matrix is returned, which would give unexpected results.

    I am just guessing, but it might be just the scaling that needs to be inverted? The matrix from option is the cumulative transform (includes the viewing transform and all parent to child transforms) and could be complex. You might try extracting the scale, and creating a new matrix that just inverts the scaling. I would be more definitive, but I am working through the same problem now.


    Added after 5 minutes:


    saics: you're not checking whether the matrix was inverted. If it is not invertible, the identity matrix is returned, which would give unexpected results.

    I am just guessing, but it might be just the scaling that needs to be inverted? The matrix from option is the cumulative transform (includes the viewing transform and all parent to child transforms) and could be complex. You might try extracting the scale, and creating a new matrix that just inverts the scaling. I would be more definitive, but I am working through the same problem now.


    Added after 12 minutes:


    More generally, in Qt, the pen and brush scale with an item. You must inverse scale them if you want the pen width to remain constant as you zoom the view. (For a brush, you must inverse scale them especially if you use a non-solid pattern, e.g. crosshatch or texture. Pattern solid seems to always work without inverse scaling?

    In my app, I know how an item is scaled (I am not reimplementing paint, just setBrush() on the item). Items may not be scaled uniformly in x and y. But I take the minimum for the itemScale, to take the inverse and scale the brush to that. (You probably don't want a brush scaled non-uniformly.) Code snippets that seem to work for me:

    Qt Code:
    1. if itemScale == 0.0:
    2. # For a new, infinitesmal item, scale may be 0.0.
    3. scaledWidthF = 9999999
    4. else:
    5. scaledWidthF = 1.0/itemScale * unscaledWidth
    6. self.scaleInvertBrush(brush, scaledWidthF)
    7.  
    8. def scaleInvertBrush(self, brush, scale):
    9. """
    10. Scale brush (both x and y scale) to given scale.
    11. """
    12. transform=QTransform()
    13. transform.scale(scale, scale)
    14. brush.setTransform(transform)
    To copy to clipboard, switch view to plain text mode 
    Last edited by bootchk; 20th September 2013 at 22:15.

Tags for this Thread

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.