栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

pyqt在Qgraphicsscene中添加矩形

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pyqt在Qgraphicsscene中添加矩形

首先,QGraphicsItem不是QWidget,因此它具有那些事件并且不能直接处理它们,这就是QGraphicsView和QGraphicsScene所做的。例如,您说您想拥有一个可移动的矩形,因为该任务很简单,就是QGraphicsView,因此不必覆盖:

from PyQt5 import QtCore, QtWidgetsclass MainWindow(QtWidgets.QMainWindow):    def __init__(self, parent=None):        super(MainWindow, self).__init__(parent)        scene = QtWidgets.QGraphicsScene(self)        view = QtWidgets.QGraphicsView(scene)        self.setCentralWidget(view)        rect_item = QtWidgets.QGraphicsRectItem(QtCore.QRectF(0, 0, 100, 100))        rect_item.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)        scene.addItem(rect_item)if __name__ == '__main__':    import sys    app = QtWidgets.QApplication(sys.argv)    w = MainWindow()    w.resize(640, 480)    w.show()    sys.exit(app.exec_())

如果要更改绘制矩形的方式,则必须覆盖

paint()
如下所示的方法:

from PyQt5 import QtCore, QtGui, QtWidgetsclass RectItem(QtWidgets.QGraphicsRectItem):    def paint(self, painter, option, widget=None):        super(RectItem, self).paint(painter, option, widget)        painter.save()        painter.setRenderHint(QtGui.QPainter.Antialiasing)        painter.setBrush(QtCore.Qt.red)        painter.drawEllipse(option.rect)        painter.restore()class MainWindow(QtWidgets.QMainWindow):    def __init__(self, parent=None):        super(MainWindow, self).__init__(parent)        scene = QtWidgets.QGraphicsScene(self)        view = QtWidgets.QGraphicsView(scene)        self.setCentralWidget(view)        rect_item = RectItem(QtCore.QRectF(0, 0, 100, 100))        rect_item.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)        scene.addItem(rect_item)if __name__ == '__main__':    import sys    app = QtWidgets.QApplication(sys.argv)    w = MainWindow()    w.resize(640, 480)    w.show()    sys.exit(app.exec_())

更新:

from PyQt5 import QtCore, QtGui, QtWidgetsclass GraphicsScene(QtWidgets.QGraphicsScene):    def __init__(self, parent=None):        super(GraphicsScene, self).__init__(QtCore.QRectF(-500, -500, 1000, 1000), parent)        self._start = QtCore.QPointF()        self._current_rect_item = None    def mousePressEvent(self, event):        if self.itemAt(event.scenePos(), QtGui.QTransform()) is None: self._current_rect_item = QtWidgets.QGraphicsRectItem() self._current_rect_item.setBrush(QtCore.Qt.red) self._current_rect_item.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True) self.addItem(self._current_rect_item) self._start = event.scenePos() r = QtCore.QRectF(self._start, self._start) self._current_rect_item.setRect(r)        super(GraphicsScene, self).mousePressEvent(event)    def mouseMoveEvent(self, event):        if self._current_rect_item is not None: r = QtCore.QRectF(self._start, event.scenePos()).normalized() self._current_rect_item.setRect(r)        super(GraphicsScene, self).mouseMoveEvent(event)    def mouseReleaseEvent(self, event):        self._current_rect_item = None        super(GraphicsScene, self).mouseReleaseEvent(event)class MainWindow(QtWidgets.QMainWindow):    def __init__(self, parent=None):        super(MainWindow, self).__init__(parent)        scene =GraphicsScene(self)        view = QtWidgets.QGraphicsView(scene)        self.setCentralWidget(view)if __name__ == '__main__':    import sys    app = QtWidgets.QApplication(sys.argv)    w = MainWindow()    w.resize(640, 480)    w.show()    sys.exit(app.exec_())


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/669280.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号