Results 1 to 1 of 1

Thread: [Solved] What's wrong with my grid drawing code?

  1. #1
    Join Date
    Apr 2015
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

    Cool [Solved] What's wrong with my grid drawing code?

    Here's the paint code:
    Qt Code:
    1. def paint(self, painter, option, widget):
    2. painter.setRenderHint(QPainter.Antialiasing)
    3. pen = self.pen
    4. painter.setPen(pen)
    5. box = self.boundingRect()
    6.  
    7. #Calculate first horizontal line at the top
    8. diffY = box.y() - self.anchorPoint.y()
    9. y = self.yDist - (diffY - math.floor(diffY / self.yDist)) + box.y()
    10. yNum = math.floor((box.height() - y) / self.yDist) + 1
    11.  
    12. #Calculate first vertical line on left
    13. diffX = box.x() - self.anchorPoint.x()
    14. x = self.xDist - (diffX - math.floor(diffX / self.xDist)) + box.x()
    15. xNum = math.floor((box.width() - x) / self.xDist) + 1
    16.  
    17. for i in range(0, yNum):
    18. y2 = y + i * self.yDist
    19. line = QLineF(box.x(), y2, box.x() + box.width(), y2)
    20. painter.drawLine(line)
    21.  
    22. for i in range(0, xNum):
    23. x2 = x + i * self.xDist
    24. line = QLineF(x2, box.y(), x2, box.y() + box.height())
    25. painter.drawLine(line)
    To copy to clipboard, switch view to plain text mode 

    boundingRect() returns the viewable scene area using the viewport() trick

    Some lines are drawn all the way across and some aren't.


    Added after 8 minutes:


    Fixed it. Here's the code:

    def paint(self, painter, option, widget):
    painter.setRenderHint(QPainter.Antialiasing)
    pen = self.pen
    painter.setPen(pen)
    box = self.boundingRect()

    #Calculate first horizontal line at the top
    diffY = box.y() - self.anchorPoint.y()
    y = self.anchorPoint.y() + math.floor(diffY / self.yDist)*self.yDist
    yNum = math.floor((box.y() + box.height() - y) / self.yDist) + 1

    #Calculate first vertical line on left
    diffX = box.x() - self.anchorPoint.x()
    x = self.anchorPoint.x() + math.floor(diffX / self.xDist)*self.xDist
    xNum = math.floor((box.x() + box.width() - x) / self.xDist) + 1

    for i in range(0, yNum):
    y2 = y + i * self.yDist
    line = QLineF(box.x(), y2, box.x() + box.width(), y2)
    painter.drawLine(line)

    for i in range(0, xNum):
    x2 = x + i * self.xDist
    line = QLineF(x2, box.y(), x2, box.y() + box.height())
    painter.drawLine(line)
    Last edited by enjoysmath; 9th August 2015 at 23:55.

Similar Threads

  1. Replies: 7
    Last Post: 15th May 2019, 09:44
  2. QWidget subclass drawing in wrong place
    By guidupas in forum Qt Programming
    Replies: 13
    Last Post: 7th June 2014, 15:03
  3. Drawing a grid
    By LevelFour in forum Newbie
    Replies: 1
    Last Post: 14th February 2010, 16:38
  4. What is wrong with my code?
    By Dante in forum Qt Programming
    Replies: 27
    Last Post: 6th April 2009, 07:56
  5. drawing a grid
    By dreamer in forum Qt Programming
    Replies: 2
    Last Post: 27th April 2008, 09:17

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.