Qt5 paintEvent传播到子窗口小部件

时间:2017-06-30 06:06:19

标签: python-3.x events qt5 pyqt5

我正在尝试在PyQt5中编写一个基本的游戏循环。我的内部有QWidgetQGraphicsView。我使用QTimerupdate()来触发paintEvent()QWidget上的QGraphicsViewQWidget paintEvent()已更新,但QGraphicsView paintEvent()未更新。

这是我的代码:

import sys, random

from PyQt5.QtWidgets import QWidget, QGridLayout, QApplication, QGraphicsView, QGraphicsScene
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QPainter, QColor, QPen, QBrush, QColor



class Window(QWidget):

    WIDTH = 800
    HEIGHT = 480
    SPEED = 500

    def __init__(self):

        super().__init__()
        self.initUi()
        self.initDisplay()
        self.show()

    def initUi(self):

        layout = QGridLayout()

        scene = GraphicsScene()        
        self.view = GraphicsView()
        self.view.setScene(scene)

        layout.addWidget(self.view)
        self.setLayout(layout)


    def initDisplay(self):     

        self.resize(Window.WIDTH, Window.HEIGHT)

        self.timer = QTimer()
        self.timer.setInterval(Window.SPEED)
        self.timer.timeout.connect(self.timerEvent)
        self.isStarted = False
        self.isPaused = False

        self.timer.start()


    def start(self):

        if self.isPaused:
            return

        self.isStarted = True
        self.timer.start(Display.SPEED)


    def pause(self):

        if not self.isStarted:
            return

        self.isPaused = not self.isPaused

        if self.isPaused:
            self.timer.stop()            
        else:
            self.timer.start(Display.SPEED)

        self.update()


    def paintEvent(self, event):
        print('paint event ...')


    def timerEvent(self):
        self.update()
        self.view.update()
        print('timer event ...')



class GraphicsView(QGraphicsView):


    def __init__(self):
        super(GraphicsView, self).__init__()


    def paintEvent(self, event):
        self.scene().add_shapes()
        print('view paint event')



class GraphicsScene(QGraphicsScene):

    def __init__(self):
        super(GraphicsScene, self).__init__()


    def add_shapes(self):

        self.setSceneRect(0.0, 0.0, 800.0, 640.0)

        self.addRect(100, 100, 150, 150)

        pen = QPen(Qt.SolidLine)
        pen.setColor(Qt.red)
        brush = QBrush(Qt.Dense3Pattern)
        brush.setColor(Qt.darkGreen)
        self.addEllipse(300, 300, 100, 100, pen, brush)
        print('add shapes')        



if __name__ == '__main__':

    app = QApplication([])
    window = Window()    
    sys.exit(app.exec_())

当我运行脚本时,我得到以下输出:

paint event ...
add shapes
view paint event
paint event ...
add shapes
view paint event
timer event ...
paint event ...
paint event ...
timer event ...
paint event ...
paint event ...
timer event ...
paint event ...
paint event ...
timer event ...
paint event ...
paint event ...
timer event ...
paint event ...
paint event ...

1 个答案:

答案 0 :(得分:0)

解决方法是调用基类的var data = [ {name:'Alpha Hotel',price:400,star:3}, {name:'Beta Hotel',price:600,star:4}, {name:'Gamma Hotel',price:450,star:5}, ]; var htmlConstructed = ''; $.each(data,function(i,val){ // console.log(val.name); var rating=''; for(var i=0;i<val.star;i++){ rating = rating +'<span><i class="fa fa-star" aria-hidden="true" style="color:#D91E18;"></i></span>'; } htmlConstructed += '<p>Name : '+val.name+'</p>'+'<p>Price : '+val.price+'</p>'+'Rating :'+rating+'<hr/>'; }); $('#content-area').append(htmlConstructed); (在本例中为data Identifier = Identifier String String

paintEvent()