PySide Qt:拖放图像 [英] PySide Qt: Drag and drop of an image
本文介绍了PySide Qt:拖放图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从PySide.QtGui导入*
from PySide.QtCore import *
import sys
class Label(QLabel):
def __init __(self,title,parent):
super(Label,self).__ init __(title,parent)
self.setup()
def setup(self):
folder_pic = QPixmap(path)
folder_size = folder_pic.scaled(64,64)
self.label = QLabel(self)
self.label.setPixmap(folder_size)
def mouseMoveEvent(self,e )
如果e.buttons()!= Qt.LeftButton:
return
mimeData = QMimeData(self)
drag = QDrag(self)
drag.setMimeData(mimeData)
drag.setHotSpot(e.pos() - self.rect()。topLeft())
dropAction = drag .start(Qt.MoveAction)
class示例(QWidget):
def __init __(self):
super(Example,self).__ init __()
self.initUI()
def initUI(self):
self.setAcceptDrops(True)
self.button = label(,self)
self.button.move(100,65)
self.setWindowTitle('Click or Move')
self.setGeometry(300,300,280, 150)
def dragEnterEvent(self,e):
e.accept()
def dropEvent(self,e) :
position = e.pos()
self.button.move(position)
e.setDropAction(Qt.MoveAction)
e.accept()
如果__name__ ==__main__:
app = QApplication(sys.argv)
mywin = Example()
mywin。 show()
sys.exit(app.exec_())
我的代码是基于在本教程中 http://zetcode.com/gui/pysidetutorial/dragdrop/ 。
解决方案
您可以在拖动对象上设置像素图:
drag.setPixmap(self.pixmap())
但是你也需要这个修复:
def setup(self):
folder_pic = QPixmap(path )
folder_size = folder_pic.scaled(64,64)
self.setPixmap(folder_size)
The title basically says it all. I want to create an image that I can drag around inside a window. My code so far does all that but for some reason, the image appears really small even though, I used the scaled() function to resize it. Also, if I change the values inside the scaled() function, the image disappears.
from PySide.QtGui import *
from PySide.QtCore import *
import sys
class Label(QLabel):
def __init__(self, title, parent):
super(Label, self).__init__(title, parent)
self.setup()
def setup(self):
folder_pic = QPixmap("path")
folder_size = folder_pic.scaled(64, 64)
self.label = QLabel(self)
self.label.setPixmap(folder_size)
def mouseMoveEvent(self, e):
if e.buttons() != Qt.LeftButton:
return
mimeData = QMimeData(self)
drag = QDrag(self)
drag.setMimeData(mimeData)
drag.setHotSpot(e.pos() - self.rect().topLeft())
dropAction = drag.start(Qt.MoveAction)
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.setAcceptDrops(True)
self.button = Label("", self)
self.button.move(100, 65)
self.setWindowTitle('Click or Move')
self.setGeometry(300, 300, 280, 150)
def dragEnterEvent(self, e):
e.accept()
def dropEvent(self, e):
position = e.pos()
self.button.move(position)
e.setDropAction(Qt.MoveAction)
e.accept()
if __name__ == "__main__":
app = QApplication(sys.argv)
mywin = Example()
mywin.show()
sys.exit(app.exec_())
My code is based on this tutorial http://zetcode.com/gui/pysidetutorial/dragdrop/.
解决方案
You set a pixmap on the drag object:
drag.setPixmap(self.pixmap())
But for that to work, you will also need this fix:
def setup(self):
folder_pic = QPixmap("path")
folder_size = folder_pic.scaled(64, 64)
self.setPixmap(folder_size)
这篇关于PySide Qt:拖放图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文