2015-06-22 86 views
0

我目前有一個QGraphicsScene中找到兩個項目之間的距離問題。我有距離公式已經下降,但問題是,當我在graphicsScene中選擇兩個點或項目時,它說它們處於同一點,並且由於它們在同一點,距離始終爲零。要選擇多個項目,我使用Ctrl +單擊可以一次選擇兩個點,並且我知道這兩個點位於場景中的不同區域。這裏是代碼:選擇幾個項目,然後將它們之間的距離

import sys 
from PyQt4 import QtGui, QtCore 
import math 
#this sets the scene for drawing and the microscope image 
class MyView(QtGui.QGraphicsView): 
    def __init__(self,window): 
     QtGui.QGraphicsView.__init__(self) 
     self.window = window 
     self.scene = QtGui.QGraphicsScene(self) 
     self.item = QtGui.QGraphicsRectItem(400, 400, 400, 400) 
     self.scene.addItem(self.item) 
     self.setScene(self.scene) 
    def paintMarkers(self,event): 
     ##self.cursor = QtGui.QCursor() 
     #self.cursor.setShape(2) 
     p = self.mapToScene(event.x(),event.y()) 
     if (p.x() > 400 and p.x() < 800) and (p.y() > 400 and p.y() < 800): 
      self.circleItem = QtGui.QGraphicsEllipseItem(p.x(),p.y(),5,5) 
        self.circleItem.setFlag(QtGui.QGraphicsItem.ItemIsSelectable,True) 
      self.scene.addItem(self.circleItem) 
      self.circleItem.setPen(QtGui.QPen(QtCore.Qt.red, 1.5)) 
     #self.setScene(self.scene) 
    def deleteMarkers(self,event): 
     p = self.mapToScene(event.x(),event.y()) 
     if self.scene.itemAt(p.x(),p.y()) != self.item: 
      self.scene.removeItem(self.scene.itemAt(p.x(),p.y())) 
     #print "Hello world" 
    #def mousePressEvent(self,QMouseEvent): 
     #self.paintMarkers() 
    def mousePressEvent(self,event): 
     if self.window.btnPaintDot.isChecked(): 
      self.paintMarkers(event) 
     if self.window.btnDeleteMarks.isChecked(): 
      self.deleteMarkers(event) 
     if self.window.btnPaintLength.isChecked(): 
      self.distanceFormula(self.scene.selectedItems()) 
     return QtGui.QGraphicsView.mousePressEvent(self,event) 
    def distanceFormula(self,list): 
     if len(list) == 2: 
      """for i in list: 
       p = self.mapToScene(i.x(),i.y()) 
       print p.x() 
       print p.y()""" 
      p = self.mapToScene(list[0].x(),list[0].y()) 
      q = self.mapToScene(list[1].x(),list[1].y()) 
      print p.x() 
      print p.y() 
      print q.x() 
      print q.y() 
      print list[0] 
      print list[1] 
      print len(list) 
      length = math.sqrt((p.x() - q.x())**2 + (p.y() - q.y())**2) 
      print length 

class Window(QtGui.QMainWindow): 
    def __init__(self): 
     #This initializes the main window or form 
     super(Window,self).__init__() 
     self.setGeometry(50,50,1000,1000) 
     self.setWindowTitle("Pre-Alignment system") 

     self.view = MyView(self) 
     self.setCentralWidget(self.view) 

self.btnPaintLength = QtGui.QPushButton("Len",self) 
     self.btnPaintLength.clicked.connect(self.paintLengthOnImage) 
     self.btnPaintLength.setCheckable(True) 
     self.btnPaintLength.resize(30,30) 
     self.btnPaintLength.move(330,50) 
#this creates the paint button allowing user to draw lines or anything on the image from the microscope 
     self.btnPaintDot = QtGui.QPushButton("*",self) 
     self.btnPaintDot.setCheckable(True) 
     self.btnPaintDot.clicked.connect(self.paintDotOnImage) 
     self.btnPaintDot.resize(30,30) 
     self.btnPaintDot.move(300,50) 

#creates the delete marker button 
     self.btnDeleteMarks = QtGui.QPushButton("del",self) 
     self.btnDeleteMarks.clicked.connect(self.paintDeleteMarks) 
     self.btnDeleteMarks.setCheckable(True) 
     self.btnDeleteMarks.resize(30,30) 
     self.btnDeleteMarks.move(390,50) 

    def paintLengthOnImage(self): 
      """create length on image""" 
      if self.btnPaintLength.isChecked(): 
       self.btnPaintDot.setChecked(False) 
       self.btnPaintPolygon.setChecked(False) 
       self.btnDeleteMarks.setChecked(False) 
       self.view.distanceFormula(self.view.scene.selectedItems()) 
      else: 
       self.btnPaintLength.setChecked(False) 

    def paintDotOnImage(self): 
      """create a dot paint button""" 
      if self.btnPaintDot.isChecked(): 
       self.btnPaintDot.setChecked(True) 
       self.btnPaintPolygon.setChecked(False) 
       self.btnPaintLength.setChecked(False) 
       self.btnDeleteMarks.setChecked(False) 
      else: 
       self.btnPaintDot.setChecked(False) 
    def paintDeleteMarks(self): 
      if self.btnDeleteMarks.isChecked(): 
       self.btnPaintDot.setChecked(False) 
       self.btnPaintPolygon.setChecked(False) 
       self.btnPaintPolygon.setChecked(False) 
       self.btnDeleteMarks.setChecked(True) 
      else: 
       self.btnDeleteMarks.setChecked(False) 
def run(): 
    app = QtGui.QApplication(sys.argv) 
    GUI = Window() 
    GUI.show() 
    sys.exit(app.exec_()) 

run() 

打印聲明只是爲了我的目的找到問題的地方。非常感謝!

+0

注意:如果你的代碼實際上是可運行的(缺少按鈕) –

+0

對@three_pineapples很抱歉。我現在要修復。 – MatthewS2494

回答

0

看起來會發生這種情況是因爲項目的創建方式。項目始終創建於(0,0)。當您在構建物品時指定(x,y,width,height)時,實際上會創建一個從(0,0)延伸到(x+width, y+height)的物品。您可以通過查看項目的大小boundingRect()來驗證此情況。

相反,您應該構建對象(0,0)並將其移動到正確的位置。與你當前的代碼,這意味着執行以下操作當您創建項目:

self.circleItem = QtGui.QGraphicsEllipseItem(0,0,5,5) 
self.circleItem.setPos(p.x()-self.circleItem.boundingRect().width()/2.0,p.y()-self.circleItem.boundingRect().height()/2.0) 

注意,這裏我用的寬度/高度的一半偏移的位置,以使項目的鼠標點擊居中。你可能會也可能不想要這個。

+0

感謝,似乎工作! – MatthewS2494

相關問題