Results 1 to 4 of 4

Thread: QGraphicsItem doesn't overlap perfectly a background image

  1. #1
    Join Date
    Jul 2016
    Posts
    19
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QGraphicsItem doesn't overlap perfectly a background image

    Hello,

    I have a black rectangle whose coordinates are (90,90) , (90,100), (150, 100) and (150, 90) drawn on a white background ( see test_6.png) test_6.png
    I use this image as a background image on my qgraphicsScene.

    I want to draw a red QGraphicsRectItem that overlaps perfectly the black rectangle.



    Qt Code:
    1. #include <QApplication>
    2. #include <QGraphicsScene>
    3. #include <QGraphicsView>
    4. #include <QGraphicsRectItem>
    5. #include <QPen>
    6.  
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication a(argc, argv);
    11. QGraphicsRectItem* item = new QGraphicsRectItem(QRectF(90,90,60, 10));
    12.  
    13. QPen pen = QPen(Qt::red, 1, Qt::SolidLine);
    14. pen.setCosmetic(true);
    15. item->setBrush(QColor(255, 0, 0));
    16. item->setPen(pen);
    17. scene.addItem(item);
    18.  
    19. QPixmap image = QPixmap("path_to_image");
    20. scene.setSceneRect(image.rect());
    21. QGraphicsView view(&scene);
    22. view.setBackgroundBrush(QBrush(image));
    23. view.show();
    24. view.scale(3,3); // in order to see the problem
    25.  
    26. return a.exec();
    27. }
    To copy to clipboard, switch view to plain text mode 

    However I notice a small difference of one pixel at the right and at the bottom of my QGraphicsItem : indeed I shouldn't see any black pixel if the overlap was perfect ( see Capture.PNG)

    Do you understand why there is such a problem ?

    Thank you

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QGraphicsItem doesn't overlap perfectly a background image

    Dimensions of the black rectangle in the background is 61x11 (not 60x10). You have to count the pixels covered by the black color, it includes both start pixel and end pixel.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Jul 2016
    Posts
    19
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem doesn't overlap perfectly a background image

    Ok I understand that, but if I use 61*11 as the dimension of my QGraphicsRectitem when I do item->rect()->topRight(), I get (151, 90) which is the expected value, but it is weird because it doesn't match the top right pixel of my image : (150, 91).

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QGraphicsItem doesn't overlap perfectly a background image

    This deviation is because of the basic way how Qt Coordinate system works.

    Read http://doc.qt.io/qt-4.8/coordsys.html
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    nilot (17th July 2017)

Similar Threads

  1. QGraphicsItem doesn't update
    By Vyivrain in forum Qt Programming
    Replies: 0
    Last Post: 6th April 2014, 19:51
  2. Replies: 4
    Last Post: 27th November 2013, 15:15
  3. Replies: 3
    Last Post: 3rd August 2012, 10:35
  4. Replies: 0
    Last Post: 5th April 2011, 13:36
  5. Replies: 1
    Last Post: 25th June 2010, 18:31

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.