我想使用图标来表示我的项目,同时将其拖到应用程序之外(如桌面)。只要我在父级别上使用小部件,它就可以正常工作。
工作示例:
modern.cast(my.df, .(x.2012, y.2012) ~ .(x.2013, y.2013), fun)
但是当我想在我的子窗口小部件中实现这个功能时,它只在应用程序内部工作:
import sys
from PyQt4 import QtGui, QtCore
class Button(QtGui.QPushButton):
def mouseMoveEvent(self, e):
if e.buttons() != QtCore.Qt.LeftButton:
return
mimeData = QtCore.QMimeData()
mimeData.setText('%d,%d' % (e.x(), e.y()))
pixmap = QtGui.QPixmap.grabWidget(self)
drag = QtGui.QDrag(self)
drag.setMimeData(mimeData)
drag.setPixmap(pixmap)
drag.setHotSpot(e.pos())
if drag.exec_(QtCore.Qt.CopyAction | QtCore.Qt.MoveAction) == QtCore.Qt.MoveAction:
print 'moved'
else:
print 'copied'
def mousePressEvent(self, e):
QtGui.QPushButton.mousePressEvent(self, e)
if e.button() == QtCore.Qt.LeftButton:
print 'press'
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.setAcceptDrops(True)
button = Button('Button', self)
button.move(100, 65)
self.buttons = [button]
self.setWindowTitle('Copy or Move')
self.setGeometry(300, 300, 280, 150)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.show()
app.exec_()
我怎样才能在第二个例子中使用它?
当我在不同的其他小部件中实现自定义class Item(QtGui.QListWidgetItem):
def __init__(self, parent=None):
super(Item, self).__init__(parent=parent)
def mouseMoveEvent(self, e):
if e.buttons() != QtCore.Qt.LeftButton:
return
mimeData = QtCore.QMimeData()
mimeData.setText('%d,%d' % (e.x(), e.y()))
pixmap = QtGui.QPixmap.grabWidget(self)
drag = QtGui.QDrag(self)
drag.setMimeData(mimeData)
drag.setPixmap(pixmap)
drag.setHotSpot(e.pos())
if drag.exec_(QtCore.Qt.CopyAction | QtCore.Qt.MoveAction) == QtCore.Qt.MoveAction:
print 'moved'
else:
print 'copied'
def mousePressEvent(self, e):
QtGui.QListWidget.mousePressEvent(self, e)
if e.button() == QtCore.Qt.LeftButton:
print 'press'
class ListWidget(QtGui.QListWidget):
def __init__(self, parent=None):
super(ListWidget, self).__init__(parent=parent)
self.setMovement(QtGui.QListView.Static)
self.setViewMode(QtGui.QListView.IconMode)
self.setResizeMode(QtGui.QListView.Adjust)
self.setWrapping(True)
self.setSpacing(5)
self.setDragEnabled(True)
self.setAcceptDrops(True)
for name in ["Item_01","Item_02", 'Item_03', 'Item_04']:
item = Item()
item.setText(name)
self.addItem(item)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
win = ListWidget()
win.show()
sys.exit(app.exec_())
时,它也应该有用。
由于