pyqt drawing on an exsiting widget of GUI

前端 未结 1 448
时光取名叫无心
时光取名叫无心 2020-12-17 03:39

I am new to pyqt. I am doing a program that allows you clicks on the picture and remember the coordinates of points you clicks and draw a stickfigure on a widget of the GUI.

相关标签:
1条回答
  • 2020-12-17 04:31

    Looks like you want to draw items on QGraphicsScene? In this case you could add items to the scene:

    #!/usr/bin/env python
    import sys
    
    from PyQt4 import QtCore, QtGui
    
    
    class MainWidget(QtGui.QWidget):
        def __init__(self, parent=None):
            QtGui.QWidget.__init__(self, parent)
            self.scene = QtGui.QGraphicsScene()
            self.view = QtGui.QGraphicsView(self.scene)
            layout = QtGui.QVBoxLayout()
            layout.addWidget(self.view)
            self.setLayout(layout)
            self.pixmap_item = QtGui.QGraphicsPixmapItem(QtGui.QPixmap('image.png'), None, self.scene)
            self.pixmap_item.mousePressEvent = self.pixelSelect
            self.click_positions = []
    
        def pixelSelect(self, event):
            self.click_positions.append(event.pos())
            if len(self.click_positions) < 4:
                return
            pen = QtGui.QPen(QtCore.Qt.red)
            self.scene.addPolygon(QtGui.QPolygonF(self.click_positions), pen)
            for point in self.click_positions:
                self.scene.addEllipse(point.x(), point.y(), 2, 2, pen)
            self.click_positions = []
    
    
    if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        widget = MainWidget()
        widget.resize(640, 480)
        widget.show()
        sys.exit(app.exec_())
    

    QGraphicsScene has many features.

    Read Graphics View Framework overview in Qt docs.

    0 讨论(0)
提交回复
热议问题