Results 1 to 7 of 7

Thread: Qt 4.3.0 clipping problem?

  1. #1
    Join Date
    Jul 2006
    Location
    Poprad/Prague
    Posts
    33
    Thanks
    4
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Qt 4.3.0 clipping problem?

    Hello everyone!

    I compiled Qt 4.3.0 when it was released and my application behaves quite weird...(it worked using Qt 4.2.3) I am not sure, but maybe I've found a bug in clipping, but I'd rather ask here first...

    so, the code is rather simple:
    Qt Code:
    1. int main(int argc, char *argv[]){
    2. QApplication app(argc, argv);
    3. int height = 500;
    4. int width = 600;
    5. QPixmap myPixmap(width, height);
    6. QPainter myPainter(&myPixmap);
    7. myPainter.translate(0,height);
    8. //make the coordinate system look like this:
    9. // -y ^
    10. // |
    11. // |
    12. // |
    13. // |
    14. // ----------->
    15. // 0,0 +x
    16. QRectF myRect( 100.001, -100, width-200, -(height-200) ); //**here lies the problem, I guess
    17. myPainter.setClipRect(myRect, Qt::ReplaceClip);
    18. myPainter.fillRect(0, 0, width, -height, Qt::cyan);
    19. myPainter.setClipRect(myRect, Qt::NoClip);
    20. myPixmap.save("out.png", "PNG");
    21. return 0;
    22. }
    To copy to clipboard, switch view to plain text mode 

    using this code I get an image with a cyan rectangle in the middle, as expected.
    However, if I change that one marked line to:
    Qt Code:
    1. QRectF myRect( 100, -100, width-200, -(height-200) );
    To copy to clipboard, switch view to plain text mode 
    the image is entirely black...

    Am I doing something wrong, or do you find it weird, too?

    Thanks for answer...

  2. #2
    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: Qt 4.3.0 clipping problem?

    I think this is not a bug in clipping. Or at least not a bug in clipping only. QRect suffers from a backward compatibility "feature" and you might be experiencing it right now. Quoting the docs:
    QPoint QRect::bottomRight () const
    Returns the position of the rectangle's bottom-right corner.
    Note that for historical reasons this function returns QPoint(left() + width() -1, top() + height() - 1).
    The thing you experience might be somehow related to that (don't ask me how, I have no idea), although it's a long shot (but it looks like an off-by-one issue).

    What happens if you first fill the pixmap with transparency? Is the image still black? Or is it transparent?

  3. #3
    Join Date
    Jul 2006
    Location
    Poprad/Prague
    Posts
    33
    Thanks
    4
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt 4.3.0 clipping problem?

    If I fill the pixmap with transparency using
    Qt Code:
    1. myPixmap.fill(QColor(0,0,0,0));
    To copy to clipboard, switch view to plain text mode 
    after I create it, then the image is transparent, not black...

    Ad QRect -- you're right, maybe the problem is there, but it gives me the same if I use myRect.bottom() and myRect.y() + myRect.height()...

    I tried to look, what does the clipping region look like using boundingRegion:
    Qt Code:
    1. QRectF myRect( 100, -100, width-200, -(height-200) );
    2. cout << "myRect: left: " << myRect.left() << " right: " << myRect.x() + myRect.width() << " top: " << myRect.top() << " bottom: " << myRect.y() + myRect.height() << endl;
    3. myPainter.setClipRect(myRect, Qt::ReplaceClip);
    4. QRectF clipRect = myPainter.clipRegion().boundingRect();
    5. cout << "clipRect: left: " << clipRect.left() << " right: " << clipRect.x() + clipRect.width() << " top: " << clipRect.top() << " bottom: " << clipRect.y() + clipRect.height() << endl;
    To copy to clipboard, switch view to plain text mode 

    The output is:
    Qt Code:
    1. myRect: left: 100 right: 500 top: -100 bottom: -400
    2. clipRect: left: 0 right: 0 top: 0 bottom: 0
    To copy to clipboard, switch view to plain text mode 

    Last edited by macbeth; 13th June 2007 at 09:22.

  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: Qt 4.3.0 clipping problem?

    So the bottom line is that clipping is not set at all. You could try expressing the clip coordinates in device metrics not logical ones (i.e. [100, 100, width-200, height-200]).

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

    macbeth (13th June 2007)

  6. #5
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt 4.3.0 clipping problem?

    I would be interested to see the output if you tried the following in place of what you have already tried:

    Qt Code:
    1. QRectF myRect,clipRect;
    2. myRect=( 100, -100, width-200, -(height-200) );
    3. cout << "myRect: left: " << myRect.left() << " right: " << myRect.x() + myRect.width() << " top: " << myRect.top() << " bottom: " << myRect.y() + myRect.height() << endl;
    4. myPainter.setClipRect(myRect, Qt::ReplaceClip);
    5. clipRect = myPainter.clipRegion().boundingRect();
    6. cout << "clipRect: left: " << clipRect.left() << " right: " << clipRect.x() + clipRect.width() << " top: " << clipRect.top() << " bottom: " << clipRect.y() + clipRect.height() << endl;
    7.  
    8. myRect=( 100.001, -100, width-200, -(height-200) );
    9. cout << "myRect: left: " << myRect.left() << " right: " << myRect.x() + myRect.width() << " top: " << myRect.top() << " bottom: " << myRect.y() + myRect.height() << endl;
    10. myPainter.setClipRect(myRect, Qt::ReplaceClip);
    11. clipRect = myPainter.clipRegion().boundingRect();
    12. cout << "clipRect: left: " << clipRect.left() << " right: " << clipRect.x() + clipRect.width() << " top: " << clipRect.top() << " bottom: " << clipRect.y() + clipRect.height() << endl;
    13.  
    14. myRect=( 99, -100, width-200, -(height-200) );
    15. cout << "myRect: left: " << myRect.left() << " right: " << myRect.x() + myRect.width() << " top: " << myRect.top() << " bottom: " << myRect.y() + myRect.height() << endl;
    16. myPainter.setClipRect(myRect, Qt::ReplaceClip);
    17. clipRect = myPainter.clipRegion().boundingRect();
    18. cout << "clipRect: left: " << clipRect.left() << " right: " << clipRect.x() + clipRect.width() << " top: " << clipRect.top() << " bottom: " << clipRect.y() + clipRect.height() << endl;
    19.  
    20. myRect=( 100, -100-(height-200), width-200, +(height-200) );
    21. cout << "myRect: left: " << myRect.left() << " right: " << myRect.x() + myRect.width() << " top: " << myRect.top() << " bottom: " << myRect.y() + myRect.height() << endl;
    22. myPainter.setClipRect(myRect, Qt::ReplaceClip);
    23. clipRect = myPainter.clipRegion().boundingRect();
    24. cout << "clipRect: left: " << clipRect.left() << " right: " << clipRect.x() + clipRect.width() << " top: " << clipRect.top() << " bottom: " << clipRect.y() + clipRect.height() << endl;
    25.  
    26. myRect=( 100.001, -100-(height-200), width-200, +(height-200) );
    27. cout << "myRect: left: " << myRect.left() << " right: " << myRect.x() + myRect.width() << " top: " << myRect.top() << " bottom: " << myRect.y() + myRect.height() << endl;
    28. myPainter.setClipRect(myRect, Qt::ReplaceClip);
    29. clipRect = myPainter.clipRegion().boundingRect();
    30. cout << "clipRect: left: " << clipRect.left() << " right: " << clipRect.x() + clipRect.width() << " top: " << clipRect.top() << " bottom: " << clipRect.y() + clipRect.height() << endl;
    31.  
    32.  
    33. myRect=( 99, -100-(height-200), width-200, +(height-200) );
    34. cout << "myRect: left: " << myRect.left() << " right: " << myRect.x() + myRect.width() << " top: " << myRect.top() << " bottom: " << myRect.y() + myRect.height() << endl;
    35. myPainter.setClipRect(myRect, Qt::ReplaceClip);
    36. clipRect = myPainter.clipRegion().boundingRect();
    37. cout << "clipRect: left: " << clipRect.left() << " right: " << clipRect.x() + clipRect.width() << " top: " << clipRect.top() << " bottom: " << clipRect.y() + clipRect.height() << endl;
    To copy to clipboard, switch view to plain text mode 

    ... perhaps it has something to do with the negative height???

  7. The following user says thank you to magland for this useful post:

    macbeth (13th June 2007)

  8. #6
    Join Date
    Jul 2006
    Location
    Poprad/Prague
    Posts
    33
    Thanks
    4
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt 4.3.0 clipping problem?

    [edited!]
    Thanks, guys, it seems that QPainter clips rectangles some other way that it clips paths... According to magland's suggestion, I've found out that rectangle must have positive height (and probably width, too)... so it won't be clipped as (I) expected, if you start rectangle at -100 and set height to -300, you must start rectangle at -400 at set height to 300...

    Have a look at this example (rather confusing difference, I think...):
    Qt Code:
    1. QRectF myRect( 100, -100, width-200, -(height-200) );
    2. cout << "myRect: left: " << myRect.left() << " right: " << myRect.x() + myRect.width() << " top: " << myRect.top() << " bottom: " << myRect.y() + myRect.height() << endl;
    3. myPainter.setClipRect(myRect, Qt::ReplaceClip);
    4. QRectF clipRect = myPainter.clipRegion().boundingRect();
    5. cout << "clipRect: left: " << clipRect.left() << " right: " << clipRect.x() + clipRect.width() << " top: " << clipRect.top() << " bottom: " << clipRect.y() + clipRect.height() << endl;
    6.  
    7. QPainterPath myPath;
    8. myPath.addRect(myRect);
    9. myPainter.setClipPath( myPath, Qt::ReplaceClip );
    10. clipRect = myPainter.clipRegion().boundingRect();
    11. cout << "clipPath: left: " << clipRect.left() << " right: " << clipRect.x() + clipRect.width() << " top: " << clipRect.top() << " bottom: " << clipRect.y() + clipRect.height() << endl;
    To copy to clipboard, switch view to plain text mode 

    output:
    Qt Code:
    1. myRect: left: 100 right: 500 top: -100 bottom: -400
    2. clipRect: left: 0 right: 0 top: 0 bottom: 0
    3. clipPath: left: 100 right: 500 top: -400 bottom: -100
    To copy to clipboard, switch view to plain text mode 


    @magland:
    Qt Code:
    1. myRect: left: 100 right: 500 top: -100 bottom: -400
    2. clipRect: left: 0 right: 0 top: 0 bottom: 0
    3.  
    4. myRect: left: 100.001 right: 500.001 top: -100 bottom: -400
    5. clipRect: left: 100 right: 500 top: -400 bottom: -100 //this is ok, because it is treated as a path, not as a rect (setClipRect calls setClipPath, if the coords are not integers)
    6.  
    7. myRect: left: 99 right: 499 top: -100 bottom: -400
    8. clipRect: left: 0 right: 0 top: 0 bottom: 0
    9.  
    10. myRect: left: 100 right: 500 top: -400 bottom: -100
    11. clipRect: left: 100 right: 500 top: -400 bottom: -100 //this is ok, because clipping rect has coords: x: 100; y: -400; width: 400; *height: 300*
    12.  
    13. myRect: left: 100.001 right: 500.001 top: -400 bottom: -100
    14. clipRect: left: 100 right: 500 top: -400 bottom: -100
    15.  
    16. myRect: left: 99 right: 499 top: -400 bottom: -100
    17. clipRect: left: 99 right: 499 top: -400 bottom: -100
    To copy to clipboard, switch view to plain text mode 

    So it seems like a bit of inconsistency there I guess... for example there is no problem with using negative proportions in fillRect(), etc.
    Last edited by macbeth; 13th June 2007 at 14:16.

  9. #7
    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: Qt 4.3.0 clipping problem?

    You can call QRectF::normalized() to get the proper rect. It might be worth suggesting Trolls to include that call in setClipRect() if it's not there.

Similar Threads

  1. Replies: 16
    Last Post: 7th March 2006, 15:57

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.