ZetCode PyQt4 tutorial Drag and Drop

简介: #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This is a simple drag and drop example.
#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
ZetCode PyQt4 tutorial

This is a simple drag and
drop example. 

author: Jan Bodnar
website: zetcode.com
last edited: January 2015
"""

import sys
from PyQt4 import QtGui

# In order to drop text on the QtGui.QPushButton widget, we must reimplement some methods. Therefore, we create our own Button class which inherits from the QtGui.QPushButton class.
class Button(QtGui.QPushButton):
  
    def __init__(self, title, parent):
        super(Button, self).__init__(title, parent)
        
        # We enable drop events for the widget.
        self.setAcceptDrops(True)

    # First, we reimplement the dragEnterEvent() method. We inform about the data type that we accept. In our case it is plain text.
    def dragEnterEvent(self, e):
      
        if e.mimeData().hasFormat('text/plain'):
            e.accept()
        else:
            e.ignore() 

    # By reimplementing the dropEvent() method we define what we will do upon the drop event. Here we change the text of the button widget.
    def dropEvent(self, e):
        self.setText(e.mimeData().text()) 


class Example(QtGui.QWidget):
  
    def __init__(self):
        super(Example, self).__init__()
        
        self.initUI()
        
    def initUI(self):

        edit = QtGui.QLineEdit('', self)
        # The QtGui.QLineEdit widget has a built-in support for drag operations. All we need to do is to call setDragEnabled() method to activate it.
        edit.setDragEnabled(True)
        edit.move(30, 65)

        button = Button("Button", self)
        button.move(190, 65)
        
        self.setWindowTitle('Simple drag & drop')
        self.setGeometry(300, 300, 300, 150)


def main():
  
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.show()
    app.exec_()  
  

if __name__ == '__main__':
    main()   

--------------------------------------------------------------------------------

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
ZetCode PyQt4 tutorial

In this program, we can press on a button 
with a left mouse click or drag and drop the 
button with  the right mouse click. 

author: Jan Bodnar
website: zetcode.com
last edited: October 2013
"""

import sys
from PyQt4 import QtCore, QtGui


# We create a Button class which will derive from the QtGui.QPushButton. We also reimplement two methods of the QtGui.QPushButton: the mouseMoveEvent() and the mousePressEvent(). The mouseMoveEvent() method is the place where the drag & drop operation begins.
class Button(QtGui.QPushButton):
  
    def __init__(self, title, parent):
        super(Button, self).__init__(title, parent)

    def mouseMoveEvent(self, e):

        # Here we decide that we can perform drag & drop only with a right mouse button. The left mouse button is reserved for clicking on the button.
        if e.buttons() != QtCore.Qt.RightButton:
            return

        # The QDrag object is created. The class provides support for MIME-based drag and drop data transfer.
        mimeData = QtCore.QMimeData()

        drag = QtGui.QDrag(self)
        drag.setMimeData(mimeData)
        drag.setHotSpot(e.pos() - self.rect().topLeft())

        # The start() method of the drag object starts the drag & drop operation.
        dropAction = drag.start(QtCore.Qt.MoveAction)

    # We print 'press' to the console if we left click on the button with the mouse. Notice that we call mousePressEvent() method on the parent as well. Otherwise, we would not see the button being pushed.
    def mousePressEvent(self, e):
      
        super(Button, self).mousePressEvent(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)

        self.button = Button('Button', self)
        self.button.move(100, 65)

        self.setWindowTitle('Click or Move')
        self.setGeometry(300, 300, 280, 150)
        self.show()

    def dragEnterEvent(self, e):
      
        e.accept()

    def dropEvent(self, e):

        # In the dropEvent() method we code what happens after we release the mouse button and finish the drop operation. We find out the current mouse pointer position and move the button accordingly.
        position = e.pos()        
        self.button.move(position)

        # We specify the type of the drop action. In our case it is a move action.
        e.setDropAction(QtCore.Qt.MoveAction)
        e.accept()
        

def main():
  
    app = QtGui.QApplication([])
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

 

目录
相关文章
|
运维 Devops 持续交付
自动化运维的魔法:打造高效DevOps流水线
【10月更文挑战第34天】在数字化时代的浪潮中,DevOps成为企业追求敏捷、高效和稳定的关键。本文将通过一个真实案例,展示如何构建一个高效的DevOps流水线,实现从代码提交到部署的全自动化流程。我们将探讨流水线设计的哲学、工具选择以及面临的挑战,并分享实际的代码示例和操作步骤,帮助读者理解自动化运维的精髓。
309 2
|
算法 API 开发工具
阿里云百炼平台综合评测
阿里云百炼作为一站式大模型开发平台,提供了从模型服务到应用开发的完整工具链。本文将基于实际搭建流程能力和模型训练的体验,对阿里云百炼平台进行详细评测。
823 3
|
11月前
|
JSON 缓存 API
解析电商商品详情API接口系列,json数据示例参考
电商商品详情API接口是电商平台的重要组成部分,提供了商品的详细信息,支持用户进行商品浏览和购买决策。通过合理的API设计和优化,可以提升系统性能和用户体验。希望本文的解析和示例能够为开发者提供参考,帮助构建高效、可靠的电商系统。
410 12
|
存储 JSON 前端开发
需要token的原因----
需要token的原因----
503 0
|
机器学习/深度学习 人工智能 算法
人工智能在医疗诊断中的应用与前景
本文探讨了人工智能(AI)技术在医疗诊断领域的应用现状及其未来发展前景。通过分析AI技术如何辅助医生进行疾病诊断、提高诊断准确性和效率,以及面临的挑战和伦理问题,本文旨在为读者提供一个关于AI在医疗领域应用的全面视角。
|
存储 监控 安全
|
存储 安全 算法
静态路由与动态路由的区别及应用场景
【8月更文挑战第25天】
826 0
|
人工智能 算法 开发者
AIGC引发的版权风险
【2月更文挑战第9天】AIGC引发的版权风险
255 2
AIGC引发的版权风险