在两个Qgraphicscene之间的拖动和滴度期间,如何在鼠标下方创建项目覆盖层?
我正在尝试创建两个Qgraphicscene,可以在其中我可以从左场景中拖动一个项目并将其放在Rigth场景中。我成功地制作了drad和drop,但是我不明白如何在阻力过程中创建SVG项目的覆盖层。我想拥有的是至少显示鼠标在Rigth场景上时的覆盖层。
到目前为止,我有此代码:
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtSvg import QGraphicsSvgItem
class myQGraphicsScene(QGraphicsScene):
def __init__(self,parent=None):
super(myQGraphicsScene, self).__init__()
self.parent = parent
def dragEnterEvent(self, event):
event.accept()
def dragMoveEvent(self, event):
event.accept()
def dragLeaveEvent(self, event):
event.accept()
def dropEvent(self, event):
text = event.mimeData().text()
item = self.parent.getSelecedItemfromname(text)
br = item.boundingRect()
s = item.scale()
w = int(br.width() * s)/2
h = int(br.height() * s)/2
pos = event.scenePos()
item.setPos(pos - QPointF(w,h))
self.addItem(item)
event.accept()
class myQGraphicsScene2(QGraphicsScene):
def __init__(self, parent=None):
super(myQGraphicsScene2, self).__init__()
self.parent = parent
def dragEnterEvent(self, event):
event.setDropAction(Qt.MoveAction)
event.accept()
def startDrag(self, event):
item = self.itemAt(event.scenePos(), QTransform())
print(item)
if item is None:
return
# selected = self.model().data(item, Qt.DisplayRole)
name = self.parent.getSelecedNamefromitem(item)
mimeData = QMimeData()
mimeData.setText(name )
drag = QDrag(self)
drag.setMimeData(mimeData)
result = drag.exec(Qt.MoveAction)
if result: # == Qt.MoveAction:
pass
def mouseMoveEvent(self, event):
self.startDrag(event)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setMouseTracking(True)
self.names = ['fjJ.svg','fjJ.svg','fjJ.svg']
self. items = []
shift = 200
for i, n in enumerate(self.names):
item = QGraphicsSvgItem(n)
# item.setFlag(QGraphicsItem.ItemIsMovable)
item.setFlag(not QGraphicsItem.ItemIsMovable)
# item.setFlag(QGraphicsItem.ItemIsSelectable)
item.setScale(0.25)
item.setPos(0, i * shift)
self.items.append(item)
self.scene = myQGraphicsScene2(self)
for i in self.items:
self.scene.addItem(i)
self.scene2 = myQGraphicsScene(self)
self.view = QGraphicsView()
self.view.setScene(self.scene)
self.view2 = QGraphicsView()
self.view2.setMouseTracking(True)
self.view2.setScene(self.scene2)
HBOX = QHBoxLayout()
HBOX.addWidget(self.view)
HBOX.addWidget(self.view2)
widget = QWidget()
widget.setLayout(HBOX)
self.setCentralWidget(widget)
self.setFixedSize(QSize(800,600))
def getSelecedItem(self):
item = -1
for k, i in enumerate(self.items):
if i.isSelected():
item = k
if not item == -1:
new_item = QGraphicsSvgItem(self.names[item])
new_item.setFlag(QGraphicsItem.ItemIsMovable)
new_item.setScale(0.25)
return new_item
else:
return None
def getSelecedNamefromitem(self,itemfrom):
item = -1
for k, i in enumerate(self.items):
if i == itemfrom:
item = k
if not item == -1:
return self.names[k]
else:
return None
def getSelecedItemfromname(self,name):
item = -1
for k, n in enumerate(self.names):
if n == name:
item = k
if not item == -1:
new_item = QGraphicsSvgItem(self.names[item])
new_item.setFlag(QGraphicsItem.ItemIsMovable)
new_item.setScale(0.25)
return new_item
else:
return None
def getSelecedName(self):
item = -1
for k, i in enumerate(self.items):
if i.isSelected():
item = k
if not item == -1:
return self.names[item]
else:
return None
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec()
I'm trying to create two Qgraphicscene where I can drag an item from the left scene and drop it on the rigth scene. I succeed in making the drad and drop, but I don't understand how I can create an overlay of the svg item during the drag. What I would like to have is at least display the overlay when the mouse is on the rigth scene.
So far I have this code:
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtSvg import QGraphicsSvgItem
class myQGraphicsScene(QGraphicsScene):
def __init__(self,parent=None):
super(myQGraphicsScene, self).__init__()
self.parent = parent
def dragEnterEvent(self, event):
event.accept()
def dragMoveEvent(self, event):
event.accept()
def dragLeaveEvent(self, event):
event.accept()
def dropEvent(self, event):
text = event.mimeData().text()
item = self.parent.getSelecedItemfromname(text)
br = item.boundingRect()
s = item.scale()
w = int(br.width() * s)/2
h = int(br.height() * s)/2
pos = event.scenePos()
item.setPos(pos - QPointF(w,h))
self.addItem(item)
event.accept()
class myQGraphicsScene2(QGraphicsScene):
def __init__(self, parent=None):
super(myQGraphicsScene2, self).__init__()
self.parent = parent
def dragEnterEvent(self, event):
event.setDropAction(Qt.MoveAction)
event.accept()
def startDrag(self, event):
item = self.itemAt(event.scenePos(), QTransform())
print(item)
if item is None:
return
# selected = self.model().data(item, Qt.DisplayRole)
name = self.parent.getSelecedNamefromitem(item)
mimeData = QMimeData()
mimeData.setText(name )
drag = QDrag(self)
drag.setMimeData(mimeData)
result = drag.exec(Qt.MoveAction)
if result: # == Qt.MoveAction:
pass
def mouseMoveEvent(self, event):
self.startDrag(event)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setMouseTracking(True)
self.names = ['fjJ.svg','fjJ.svg','fjJ.svg']
self. items = []
shift = 200
for i, n in enumerate(self.names):
item = QGraphicsSvgItem(n)
# item.setFlag(QGraphicsItem.ItemIsMovable)
item.setFlag(not QGraphicsItem.ItemIsMovable)
# item.setFlag(QGraphicsItem.ItemIsSelectable)
item.setScale(0.25)
item.setPos(0, i * shift)
self.items.append(item)
self.scene = myQGraphicsScene2(self)
for i in self.items:
self.scene.addItem(i)
self.scene2 = myQGraphicsScene(self)
self.view = QGraphicsView()
self.view.setScene(self.scene)
self.view2 = QGraphicsView()
self.view2.setMouseTracking(True)
self.view2.setScene(self.scene2)
HBOX = QHBoxLayout()
HBOX.addWidget(self.view)
HBOX.addWidget(self.view2)
widget = QWidget()
widget.setLayout(HBOX)
self.setCentralWidget(widget)
self.setFixedSize(QSize(800,600))
def getSelecedItem(self):
item = -1
for k, i in enumerate(self.items):
if i.isSelected():
item = k
if not item == -1:
new_item = QGraphicsSvgItem(self.names[item])
new_item.setFlag(QGraphicsItem.ItemIsMovable)
new_item.setScale(0.25)
return new_item
else:
return None
def getSelecedNamefromitem(self,itemfrom):
item = -1
for k, i in enumerate(self.items):
if i == itemfrom:
item = k
if not item == -1:
return self.names[k]
else:
return None
def getSelecedItemfromname(self,name):
item = -1
for k, n in enumerate(self.names):
if n == name:
item = k
if not item == -1:
new_item = QGraphicsSvgItem(self.names[item])
new_item.setFlag(QGraphicsItem.ItemIsMovable)
new_item.setScale(0.25)
return new_item
else:
return None
def getSelecedName(self):
item = -1
for k, i in enumerate(self.items):
if i.isSelected():
item = k
if not item == -1:
return self.names[item]
else:
return None
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec()
Here the .svg
https://svgshare.com/i/fjJ.svg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己找到了解决方案。它不是完美的,但它确实有效。您可以放置其他更优化的解决方案。
I found a solution myself. It is not a perfect one but it works. You can put other solutions if they are more optimized.