1.圖片在QLabel上呈現
2.僅實作了drag&drop的代碼如下:
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
class label(QLabel):
def __init__(self, title, parent):
super().__init__(title, parent)
def mouseMoveEvent(self, e):
mimeData = QMimeData()
drag = QDrag(self)
drag.setMimeData(mimeData)
drag.setHotSpot(e.pos() - self.rect().topLeft())
dropAction = drag.exec_(Qt.MoveAction)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setAcceptDrops(True)
self.button = label('Button', self)
self.im1 = QPixmap('Scripts/p.ico')
self.button.setPixmap(self.im1)
self.button.move(100, 65)
self.button2 = label('Button2', self)
self.button2.move(200, 65)
self.setWindowTitle('Click or Move')
self.setGeometry(300, 300, 280, 150)
def dragEnterEvent(self, e):
e.accept()
def dropEvent(self, e):
btn = e.source()
position = e.pos()
btn.move(position)
e.setDropAction(Qt.CopyAction)
e.accept()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
ex.showMaximized()
app.exec_()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/131207.html
上一篇:matlab小白求救
下一篇:Python
