PySide:有一个SVG图像跟随鼠标没有拖放?

时间:2016-08-03 17:13:43

标签: svg pyqt pyside

我试图将SVG图像放在最终将成为另一个应用程序中的QGroupBox的位置。然后,在第一次单击SVG时,SVG"粘贴"鼠标移动(就像在拖放过程中按住鼠标按钮一样)。第二次点击将释放(" drop")图像。

我一直在阅读但不太了解小部件,项目,场景和视图之间的关系的性质。我的代码下面有点作品,但并不是很正确。图像几乎主动避免鼠标。对于非C,非C ++,使用PyQt / PySide的初学者,有没有明确的解释?或者是否有一个简单的解释我错在哪里?

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import sys
from PySide.QtCore import *
from PySide.QtGui  import *
from PySide.QtSvg  import *


class Image(QGraphicsSvgItem):

    def __init__(self, parent=None):
        super(Image, self).__init__("image.svg", parent)
        self.parent = parent

        self.setFlags(QGraphicsItem.ItemIsSelectable |
                      QGraphicsItem.ItemIsMovable)

        self.setAcceptsHoverEvents(True)

        self.svgSize  = self.renderer().defaultSize()
        self.width    = self.svgSize.width()
        self.height   = self.svgSize.height()
        self.absolute = None  # Image's absolute (global) position
        self.mobile   = 0     # Initially immobile
        self.scene    = None  # Not in a scene yet
        self.view     = None  # Not in a view yet

    def hoverEnterEvent(self, event):
        print "Enter"

    def hoverLeaveEvent(self, event):
        print "Leave"

    def hoverMoveEvent(self, event):
        print "Moving"
        self.absolute = QCursor.pos()
        if self.view:
            relative = self.view.mapFromGlobal(self.absolute)
            if self.mobile:
#               self.setPos(relative)
                self.setPos(self.absolute)


class Viewport(QGraphicsView):

    def __init__(self, parent=None):
        super(Viewport, self).__init__(parent)
        self.scene = QGraphicsScene()
        self.image = Image()
        self.image.setPos(100, 100)
        self.scene.addItem(self.image)
        self.setScene(self.scene)
        self.image.scene = self.scene
        self.image.view  = self

    def mousePressEvent(self, event):
        super(Viewport, self).mousePressEvent(event)

        self.image.mobile = (self.image.mobile + 1) % 2  # Toggle mobility

        x = self.image.x()  # + int(self.image.width  / 2)
        y = self.image.y()  # + int(self.image.height / 2)
        QCursor.setPos(x, y)

        relative = self.mapFromGlobal(self.image.absolute)

        print "absolute.x() = {0}  absolute.y() = {1}"\
              .format(self.image.absolute.x(), self.image.absolute.y())

        print "relative.x() = {0}  relative.y() = {1}"\
              .format(relative.x(), relative.y())


class MainWindow(QWidget):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.view = Viewport(self)

        hbox = QHBoxLayout()
        hbox.addWidget(self.view)

        self.setLayout(hbox)


app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

PySide邮件列表上的Jukka能够帮助我做出以下回应:

  

您无需在QGraphicsView中执行任何操作   mousePressEvent Image中的mousePressEvent来切换浮动。这些   事件获取QGraphicsMouseEvents作为参数,这些具有   您需要有关光标或鼠标按下位置的信息。所以,在   简而言之,从Viewport移除hoverMoveEvent并替换   Image中的def hoverMoveEvent(self, event): if self.mobile: print("Moving") self.setPos(event.scenePos() - QPoint(self.width / 2, self.height / 2)) def mousePressEvent(self, event): self.mobile = not self.mobile # Toggle mobility super().mousePressEvent(event) install.packages("XML") require("XML") data_table<-xmlToDataFrame("/users/datafile.xml") 有关,并且可以正常运行:

xmlParseCharRef: invald xmlChar value 19
Error: 1: xmlParseCharRef: invalid xmlChar value 19