Results 1 to 3 of 3

Thread: I'm trying to write a QPainter update Interval

  1. #1
    Join Date
    Jun 2020
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default I'm trying to write a QPainter update Interval

    I'm going to use the QPainter update to make the shape appear and hit, but if I run the thread, it won't come up. What's wrong with it?
    the part of a problem
    while not e.wait(1):
    self.update()

    Qt Code:
    1. import sys
    2. from PyQt5.QtWidgets import QWidget, QApplication , QPushButton, QVBoxLayout
    3. from PyQt5.QtGui import QPainter, QPen, QColor, QBrush
    4. from PyQt5.QtCore import Qt
    5. import numpy as np
    6.  
    7. import threading
    8.  
    9. class MyApp(QWidget):
    10.  
    11. def __init__(self):
    12. super().__init__()
    13. self.initUI()
    14.  
    15. def initUI(self):
    16. self.setGeometry(300, 300, 400, 400)
    17. self.setWindowTitle('Rect')
    18. self.show()
    19.  
    20. def paintEvent(self, e):
    21. qp = QPainter()
    22. qp.begin(self)
    23. self.draw_rect(qp)
    24. e = threading.Event()
    25. #the part of a problem
    26. while not e.wait(1):
    27. self.update()
    28. qp.end()
    29.  
    30.  
    31. def draw_rect(self, qp):
    32. qp.setBrush(QColor(0, 0, 0))
    33. for i in range(5):
    34. rand_x = 100 * np.random.randn()
    35. rand_y = 100 * np.random.randn()
    36. qp.drawRect(self.width() / 2 + rand_x, self.height() / 2 + rand_y, 10, 10)
    37.  
    38.  
    39.  
    40. if __name__ == '__main__':
    41. app = QApplication(sys.argv)
    42. ex = MyApp()
    43. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 
    Last edited by yskioi; 19th June 2020 at 17:39.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,243
    Thanks
    303
    Thanked 866 Times in 853 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: I'm trying to write a QPainter update Interval

    In Qt, all GUI updates have to occur in the main thread. You can't implement drawing operations in a different thread. You also can't do anything in a paint event which will trigger another paint event (like call update or resize the widget, etc.). And finally, paint event handling is optimized to eliminate unnecessary repainting. If nothing has changed to require repainting, or if there are several paint events in the event queue, only the last one will be executed and the rest discarded.

    All on-screen QWidget painting is event driven, and you can only create a QPainter and draw during a paintEvent().

    In any case, this is the wrong way to go about it. If you want something to happen at given intervals, you implement a QTimer, and connect a slot to the QTimer::timeout() signal. In the slot, make the changes you want, then call update() (or if you have changed a widget property that triggers an update, like resizing, the widget will probably repaint itself).

    Finally, if you construct your QPainter instance with "self" as the argument, you do not need to call QPainter::begin() / QPainter::end(). When you use this constructor (qp = QPainter( self )), it calls begin() inside the constructor, and end() when it is destroyed when the paintEvent() exits.
    <=== 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. #3
    Join Date
    Jun 2020
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: I'm trying to write a QPainter update Interval

    The problem has been solved. Thank you.

Similar Threads

  1. Replies: 6
    Last Post: 10th October 2016, 19:03
  2. Replies: 0
    Last Post: 28th September 2011, 08:14
  3. Replies: 3
    Last Post: 28th August 2010, 13:42
  4. Replies: 13
    Last Post: 29th April 2009, 16:51
  5. QPainter update()
    By csvivek in forum Qt Programming
    Replies: 5
    Last Post: 24th March 2008, 10:42

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.