在特定Qframe上的PaintEvent

时间:2019-12-13 10:28:45

标签: python pyqt pyqt5

我有以下代码。我正在尝试绘制特定的qframe,并每16ms更新一次。

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_test_app(object):
    def setupUi(self, test_app):
        test_app.setObjectName("test_app")
        test_app.resize(800, 600)
        self.main_widget = QtWidgets.QWidget(test_app)
        self.main_widget.setObjectName("main_widget")

        self.verticalLayoutWidget = QtWidgets.QWidget(self.main_widget)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(9, 480, 781, 71))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")

        self.btn = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.btn.setContentsMargins(0, 0, 0, 0)
        self.btn.setObjectName("btn")

        self.pushButton = QtWidgets.QPushButton(self.verticalLayoutWidget)
        self.pushButton.setObjectName("pushButton")

        self.btn.addWidget(self.pushButton)

        self.canvas = QtWidgets.QFrame(self.main_widget)
        self.canvas.setGeometry(QtCore.QRect(120, 30, 541, 331))
        self.canvas.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.canvas.setFrameShadow(QtWidgets.QFrame.Raised)
        self.canvas.setObjectName("canvas")

        test_app.setCentralWidget(self.main_widget)

        self.menubar = QtWidgets.QMenuBar(test_app)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
        self.menubar.setObjectName("menubar")
        test_app.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(test_app)
        self.statusbar.setObjectName("statusbar")
        test_app.setStatusBar(self.statusbar)
        self.simulation_timer = QtCore.QTimer(
            interval=16, timeout=self.perform_simulation_iteration
        )
        self.simulation_timer.start()

        QtCore.QMetaObject.connectSlotsByName(test_app)

    def perform_simulation_iteration(self):
        print('run sim')
        self.canvas.update()

    def paintEvent(self,event):
        print('run paint')
        painter = QtGui.QPainter(self)
        painter.setPen(QtGui.QPen(QtCore.Qt.red))
        painter.drawArc(QtCore.QRectF(250, 250, 10, 10), 0, 5760)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    test_app = QtWidgets.QMainWindow()
    ui = Ui_test_app()
    ui.setupUi(test_app)
    test_app.show()
    sys.exit(app.exec_())

如果我没有主窗口就做同样的事情,只是一个qwidget和一个qframe就可以了,所以我假设这与不与qframe关联的paintevent有关,但我不知道该怎么做。

0 个答案:

没有答案