AttributeError: 'NoneType' object has no attribute 'scenePos'

AttributeError: 'NoneType' object has no attribute 'scenePos'

这是我的示例代码,我想通过使用鼠标按下事件打印我的 x、y 位置坐标,我收到此错误任何人都可以帮助我,我想在中显示我的图形视图滚动区域的中间。

下面是我的代码:

from pyface.qt import QtGui, QtCore
import sys

class MyView(QtGui.QGraphicsView):
    def __init__(self):
        QtGui.QGraphicsView.__init__(self)
        self.row = 2
        self.cols = 4
        self.scene = QtGui.QGraphicsScene(0,0,500,500)
        self.List = []

        for i in range(self.row):
            for j in range(self.cols):
                item = self.scene.addRect(QtCore.QRectF(0,0,30,30))
                item.setPos(30+j*30,500-i*30-60)
                print item.scenePos()
                self.List.append(item)
        self.setScene(self.scene)
    def mousePressEvent(self,event):
        super(MyView,self).mousePressEvent(event)
        p = QtCore.QPointF(event.pos())
        print "positonnnnnnnnnnnnnnnn", p
        item = self.scene.itemAt(p)
        print "@@@@@@@@@@@@@@@@@@@@@@@@@2"
        print item.scenePos()
class Settings(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Settings, self).__init__(parent)
        spacer = QtGui.QWidget(self)
        spacer.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
        self.vbox = QtGui.QVBoxLayout()
        self.save = QtGui.QPushButton("save")
        self.open= QtGui.QPushButton("open")
        self.folder= QtGui.QPushButton("Folder")
        self.folder.clicked.connect(self.showSettings)
        self.vbox.addWidget(self.save)
        self.vbox.addWidget(self.open)
        self.vbox.addWidget(self.folder)
        self.grid = QtGui.QGridLayout()
        self.grid.addLayout(self.vbox,0,0)
        self.scrollArea = QtGui.QScrollArea()
        self.scrollArea.setBackgroundRole(QtGui.QPalette.Light)
        self.scrollArea.setWidgetResizable(True)
        self.grid.addWidget(self.scrollArea,0,1)
        self.setCentralWidget(QtGui.QWidget(self))
        self.centralWidget().setLayout(self.grid)
        self.setGeometry(200,100,300,300)
        self.show()
    def showSettings(self):
        self.newwidget = QtGui.QWidget()
        self.glayout = QtGui.QGridLayout(self.newwidget)
        self.MyView =  MyView()
        self.glayout.addWidget(self.MyView,0,1)
        self.scrollArea.setWidget(self.newwidget)
def main():
    app = QtGui.QApplication(sys.argv)
    ex = Settings()
    sys.exit(app.exec_())
if __name__ == '__main__':
    main()

项目的坐标与window的坐标不同,在event.pos()的情况下returns鼠标相对于[=12的位置=] 的 QGraphicsView 所以你必须用 mapToScene() 将它转换为场景的坐标,而另一方面当使用 itemAt() 时可以 return None 因为在场景的某些区域没有物品,因此建议验证

def mousePressEvent(self, event):
    super(MyView,self).mousePressEvent(event)
    p = self.mapToScene(event.pos())
    item = self.scene.itemAt(p)
    if item is not None:
        print(item.scenePos())

要了解处理 QGraphicsView、QGraphicsScene 和 QGraphicsItems 的不同坐标系,我建议阅读 Graphics View Framework