栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

PyQt4-拖放

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PyQt4-拖放

该教程已严重过时。

QDrag.start
从Qt 4.3开始已经过时了。
QDrag.exec_
应该改为使用。

从的文档中可以看到

exec
,它具有返回值。
setDropAction
in
dropEvent
确定此值。它不执行移动。这就是为什么您需要进行
self.button.move()
实际移动的原因。那么,a的意义是
setDropAction
什么?您可能需要知道您执行了哪种拖动操作。假设您要在两个列表小部件之间实现拖放。如果执行了移动操作,则意味着您需要从源窗口小部件中删除该项目,然后在目标中创建一个项目。如果是复制操作,则可以保留原件,而只需在目标中创建一个副本。

setHotSpot
/
hotSpot
是有关
setPixmap
QDrag
QPixmap
拖动项目时可以显示一个。
hotSpot
确定像素图的位置。像素图的位置将使光标位于
hotSpot
相对于像素图的左上角。因此,在该教程的情况下,由于没有要显示的像素图,因此毫无意义。

这是该教程的一些修改和更新的版本。希望我已经包含了足够的评论。你可以 移动 使用

Right-Click
复制
Shift + Right-Click

#!/usr/bin/python# -*- coding: utf-8 -*-import sysfrom PyQt4 import QtGui, QtCoreclass Button(QtGui.QPushButton):    def mouseMoveEvent(self, e):        if e.buttons() != QtCore.Qt.RightButton: return        # write the relative cursor position to mime data        mimeData = QtCore.QMimeData()        # simple string with 'x,y'        mimeData.setText('%d,%d' % (e.x(), e.y()))        # let's make it fancy. we'll show a "ghost" of the button as we drag        # grab the button to a pixmap        pixmap = QtGui.QPixmap.grabWidget(self)        # below makes the pixmap half transparent        painter = QtGui.QPainter(pixmap)        painter.setCompositionMode(painter.CompositionMode_DestinationIn)        painter.fillRect(pixmap.rect(), QtGui.QColor(0, 0, 0, 127))        painter.end()        # make a QDrag        drag = QtGui.QDrag(self)        # put our MimeData        drag.setMimeData(mimeData)        # set its Pixmap        drag.setPixmap(pixmap)        # shift the Pixmap so that it coincides with the cursor position        drag.setHotSpot(e.pos())        # start the drag operation        # exec_ will return the accepted action from dropEvent        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.initUI()    def initUI(self):        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)    def dragEnterEvent(self, e):        e.accept()    def dropEvent(self, e):        # get the relative position from the mime data        mime = e.mimeData().text()        x, y = map(int, mime.split(','))        if e.keyboardModifiers() & QtCore.Qt.ShiftModifier: # copy # so create a new button button = Button('Button', self) # move it to the position adjusted with the cursor position at drag button.move(e.pos()-QtCore.QPoint(x, y)) # show it button.show() # store it self.buttons.append(button) # set the drop action as Copy e.setDropAction(QtCore.Qt.CopyAction)        else: # move # so move the dragged button (i.e. event.source()) e.source().move(e.pos()-QtCore.QPoint(x, y)) # set the drop action as Move e.setDropAction(QtCore.Qt.MoveAction)        # tell the QDrag we accepted it        e.accept()if __name__ == '__main__':    app = QtGui.QApplication(sys.argv)    ex = Example()    ex.show()    app.exec_()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/506538.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号