为什么我的QGraphicsItem无法选择?

时间:2015-01-02 15:59:24

标签: python pyqt4

我复制了一些代码片段并制作了我自己的版本。最初的片段(我不再拥有)允许移动并选择QgraphicsItem。我的修改版本允许移动,但不能选择。我做错了什么?

#!d:/python27/python -u

import sys
from PyQt4 import QtGui, QtCore

class GraphicsItem(QtGui.QGraphicsItem):
    #
    # QtGui.QGraphicsItem always needs to override its two public abstract methods
    # paint, boundingRect
    #
    def __init__(self, rect=None, parent=None):
        super(GraphicsItem, self).__init__(parent)

        self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True)
        self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True)

        self.pen = QtGui.QPen(QtCore.Qt.SolidLine)
        self.pen.setColor(QtCore.Qt.blue)
        self.pen.setWidth(8)
        self.brush = QtGui.QBrush(QtCore.Qt.red)

        self.rect = QtCore.QRectF(rect[0], rect[1], rect[2], rect[3])

    def mouseMoveEvent(self, event):
        # move object
        QtGui.QGraphicsItem.mouseMoveEvent(self, event)

    def boundingRect(self):
        return self.rect

    def paint(self, painter, option, widget):
        painter.setBrush(self.brush)
        painter.setPen(self.pen)
        painter.drawEllipse(self.rect)


class MyMainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MyMainWindow, self).__init__(parent)

        width = 1000
        height = 800
        scene = QtGui.QGraphicsScene(-width/2, -height/2, width, height)

        graphicsItem = GraphicsItem((-100, -100, 200, 200))
        scene.addItem(graphicsItem)

        view = QtGui.QGraphicsView()
         # set QGraphicsView attributes
        view.setRenderHints(QtGui.QPainter.Antialiasing |
                            QtGui.QPainter.HighQualityAntialiasing)
        view.setViewportUpdateMode(QtGui.QGraphicsView.FullViewportUpdate)

        view.setScene(scene)
        self.setCentralWidget(view)

    def keyPressEvent(self, event):
        key = event.key()

        if key == QtCore.Qt.Key_Escape:
            sys.exit(QtGui.qApp.quit())
        else:
            super(GraphicsView, self).keyPressEvent(event)

def main():
    app = QtGui.QApplication(sys.argv)
    form = MyMainWindow()
    form.setGeometry(700, 100, 1050, 850)
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:1)

你在GraphicsItem类中错过了这个方法:

def mousePressEvent(self, event):
    # select object
    QtGui.QGraphicsItem.mousePressEvent(self, event)
    print (self) # show the selected item