0
以下PyQt4應用程序的問題在於,當我拖動它們時,它移動的項目太多 - 實際移動鼠標指針的量增加了一倍,所以當我在畫布上拖動項目時,在一個方向上「足夠的移動」之後,鼠標指針在項目的邊界框外部結束。移動時QGraphicItem未正確繪製
我在做什麼錯?我不重寫任何大的改動,你可以看到:
主窗口:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from grapheditor.ui_mainwindow import Ui_MainWindow
from grapheditor.customwidgets import GraphViewer
from grapheditor.graphholder import GraphHolder
from grapheditor.graphnode import GraphNode
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.showMaximized()
QTimer.singleShot(0, self.setup)
def setup(self):
self.scene = GraphHolder()
self.ui.graphicsView.setScene(self.scene)
#self.ui.graphicsView.setSceneRect(0, 0, 1500, 1500)
self.drawItems()
def drawItems(self):
QGraphicsLineItem(0, -10, 0, 1000, None, self.scene)
QGraphicsLineItem(-10, 0, 1000, 0, None, self.scene)
item = GraphNode("hello", 0, 0, 30, 40)
self.scene.addItem(item)
def getCanvas(self):
return self.ui.graphicsView
視圖
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class GraphViewer(QGraphicsView):
def __init__(self, parent=None):
super(GraphViewer, self).__init__(parent)
self.setInteractive(True)
該項目
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class GraphNode(QGraphicsItem):
penWidth = 2
cornerRadius = 10
def __init__(self, ident, x, y, width, height, parent=None):
super(GraphNode, self).__init__(parent)
self.ident = ident
self.setPos(x, y)
self.width = width
self.height = height
self.setFlags(QGraphicsItem.ItemIsMovable|QGraphicsItem.ItemIsSelectable)
def boundingRect(self):
return QRectF(self.pos().x() - self.penWidth/2, self.pos().y() - self.penWidth/2,self.width + self.penWidth, self.height + self.penWidth)
def paint(self, painter, optiongraphicsitem, widget):
painter.drawRoundedRect(self.pos().x(), self.pos().y(), self.width, self.height, self.cornerRadius, self.cornerRadius)
painter.drawRect(self.boundingRect())
場景僅僅是現在爲空班:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class GraphHolder(QGraphicsScene):
def __init__(self, parent=None):
super(GraphHolder, self).__init__(parent)