Results 1 to 4 of 4

Thread: QPainter and refresh window

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QPainter and refresh window

    I use PyQt4 and I note that when I change something on a QPainter (example : when I draw a point at different positions), I need to move my mouse (or do some other actions) ON the window if I want to see the modifications.

    Exemple :
    Qt Code:
    1. from socket import WiiSocket
    2. from general import main
    3. import threading
    4. import sys
    5. from PyQt4 import QtGui
    6. import time
    7.  
    8.  
    9. class WiiButtons:
    10. buttons = {"Up" : False,
    11. "Down" : False,
    12. "Left" : False,
    13. "Right" : False,
    14. "Plus" : False,
    15. "Minus" : False,
    16. "Home" : False,
    17. "One" : False,
    18. "Two" : False,
    19. "A" : False,
    20. "B" : False
    21. }
    22.  
    23. def parse(self, data):
    24. if data[:5] != "A1 30":
    25. return
    26.  
    27. data = data[6:].split()
    28. self.buttons["Left"] = bool(int(data[0],16)&0x01)
    29. self.buttons["Right"] = bool(int(data[0],16)&0x02)
    30. self.buttons["Down"] = bool(int(data[0],16)&0x04)
    31. self.buttons["Up"] = bool(int(data[0],16)&0x08)
    32. self.buttons["Plus"] = bool(int(data[0],16)&0x10)
    33. self.buttons["Minus"] = bool(int(data[1],16)&0x10)
    34. self.buttons["Home"] = bool(int(data[1],16)&0x80)
    35. self.buttons["One"] = bool(int(data[1],16)&0x02)
    36. self.buttons["Two"] = bool(int(data[1],16)&0x01)
    37. self.buttons["A"] = bool(int(data[1],16)&0x08)
    38. self.buttons["B"] = bool(int(data[1],16)&0x04)
    39.  
    40. def current(self):
    41. return self.buttons
    42.  
    43.  
    44. if __name__ == "__main__":
    45.  
    46. class MyWidget(QtGui.QWidget):
    47.  
    48. x = 0
    49. y = 0
    50.  
    51. def paintEvent(self, QPaintEvent):
    52. painter = QtGui.QPainter(self)
    53. painter.drawPoint(self.x, self.y)
    54.  
    55.  
    56. def get_info(handler, widget):
    57. buttons = WiiButtons()
    58.  
    59. x = 300
    60. y = 300
    61.  
    62. while handler.state == True:
    63.  
    64. data = handler.receive(32)
    65. if len(data):
    66. buttons.parse(data)
    67. if buttons.current()["Up"]:
    68. widget.y -= 1
    69. widget.repaint()
    70. print "Up"
    71. if buttons.current()["Down"]:
    72. widget.y += 1
    73. widget.repaint()
    74. print "Down"
    75. if buttons.current()["Left"]:
    76. widget.x -= 1
    77. widget.repaint()
    78. print "Left"
    79. if buttons.current()["Right"]:
    80. widget.x += 1
    81. widget.repaint()
    82. print "Right"
    83.  
    84.  
    85. wiimote = main()
    86.  
    87. app = QtGui.QApplication(sys.argv)
    88. widget = MyWidget()
    89. widget.resize(600, 600)
    90. window = QtGui.QMainWindow()
    91. window.setCentralWidget(widget)
    92. window.show()
    93. thread = threading.Thread(None, get_info, None, (wiimote,widget))
    94. thread.start()
    95. app.exec_()
    To copy to clipboard, switch view to plain text mode 

    I guess there is a function or other things for refresh window automatically.

    Thanks.
    Last edited by wysota; 17th February 2009 at 23:10.

  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: QPainter and refresh window

    You are starving the event loop with your while loop.

    http://doc.trolltech.com/qq/qq27-responsive-guis.html

    Hmm... wait... you are doing that from another thread which is even worse. You musn't call methods of QWidget from worker threads. You don't even need that thread at all. Read the article from the link above to see some of the possible alternatives.

  3. #3
    Join Date
    Jul 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter and refresh window

    Nice =D
    I just add
    Qt Code:
    1. QtCore.QCoreApplication.postEvent(widget, QtCore.QEvent(QtCore.QEvent.Move))
    2. QtCore.QCoreApplication.processEvents()
    To copy to clipboard, switch view to plain text mode 

    Unless it's doesn't work only with processEvents() (I think it need some event to refresh window )
    Thanks.

  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: QPainter and refresh window

    Why complicate things so much? The event you post is not needed for anything. Just use QWidget::move() and that's it. No need for threads, no need for repaints.

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.