Results 1 to 2 of 2

Thread: Unexpected Offset in paintEvent

  1. #1
    Join Date
    Feb 2021
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Unexpected Offset in paintEvent

    Does anybody know, why the Rect drawn in the paintEvent does not cover the exact area of my QWidget but instead starts at an weird offset?

    Qt Code:
    1. class ContainerWidget(QWidget):
    2. def __init__(self):
    3. super().__init__()
    4. pal = QPalette()
    5. pal.setColor(QPalette.Window, Qt.magenta)
    6. self.setAutoFillBackground(True)
    7. self.setPalette(pal)
    8. def paintEvent(self, event):
    9. opt = QStyleOption()
    10. opt.initFrom(self)
    11. painter = QPainter(self)
    12. painter.setBrush( Qt.red )
    13. painter.drawRect(self.geometry().x(), self.geometry().y(), self.geometry().width(), self.geometry().height())
    14.  
    15.  
    16. class TopWidget(QWidget):
    17. def __init__(self):
    18. super().__init__()
    19. pal = QPalette()
    20. pal.setColor(QPalette.Window, Qt.yellow)
    21. self.setAutoFillBackground(True)
    22. self.setPalette(pal)
    23. def paintEvent(self, event):
    24. painter = QPainter(self)
    25. painter.setBrush( Qt.green )
    26. painter.drawRect(self.x(), self.y(), self.width(), self.height())
    27.  
    28. class BottomWidget(QWidget):
    29. def __init__(self):
    30. super().__init__()
    31. pal = QPalette()
    32. pal.setColor(QPalette.Window, Qt.gray)
    33. self.setAutoFillBackground(True)
    34. self.setPalette(pal)
    35. def paintEvent(self, event):
    36. painter = QPainter(self)
    37. painter.setBrush( Qt.blue )
    38. painter.drawRect(self.x(), self.y(), self.width(), self.height())
    39.  
    40. def main():
    41. app = QApplication()
    42. containerWidget = ContainerWidget()
    43. containerLayout = QVBoxLayout(containerWidget)
    44. containerWidget.resize(800,600)
    45. containerLayout.setSpacing(0)
    46. containerLayout.setContentsMargins(0,0,0,0)
    47.  
    48.  
    49. containerWidget.show()
    50. sys.exit(app.exec_())
    51.  
    52. if __name__ == "__main__":
    53. main()
    To copy to clipboard, switch view to plain text mode 

    paintevent_off.jpg

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Unexpected Offset in paintEvent

    You are using the QWidget::geometry() to paint the rect instead of the widget's position and size (QWidget::rect())
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    shambalain61 (10th April 2021)

Similar Threads

  1. How to add a vertical offset to the plot
    By franco.amato in forum Qwt
    Replies: 0
    Last Post: 24th November 2019, 02:38
  2. Qt submenu offset issue
    By Alundra in forum Qt Programming
    Replies: 7
    Last Post: 10th October 2015, 11:17
  3. Timezone offset
    By pdoria in forum Qt Programming
    Replies: 5
    Last Post: 7th February 2014, 06:54
  4. qtextedit with offset
    By Michael_Levin in forum Newbie
    Replies: 1
    Last Post: 6th September 2012, 12:04
  5. QTableWidget, row offset
    By georgie in forum Qt Programming
    Replies: 2
    Last Post: 11th May 2006, 12:23

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.