博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Drag & drop a button widget
阅读量:7079 次
发布时间:2019-06-28

本文共 3566 字,大约阅读时间需要 11 分钟。

In the following example, we will demonstrate how to drag & drop a button widget.

#!/usr/bin/python# -*- coding: utf-8 -*-"""ZetCode PyQt4 tutorialIn 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 Bodnarwebsite: zetcode.comlast edited: October 2013"""import sysfrom PyQt4 import QtCore, QtGuiclass Button(QtGui.QPushButton):      def __init__(self, title, parent):        super(Button, self).__init__(title, parent)    def mouseMoveEvent(self, e):        if e.buttons() != QtCore.Qt.RightButton:            return        mimeData = QtCore.QMimeData()        drag = QtGui.QDrag(self)        drag.setMimeData(mimeData)        drag.setHotSpot(e.pos() - self.rect().topLeft())        dropAction = drag.start(QtCore.Qt.MoveAction)    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):        position = e.pos()                self.button.move(position)        e.setDropAction(QtCore.Qt.MoveAction)        e.accept()        def main():      app = QtGui.QApplication([])    ex = Example()    sys.exit(app.exec_())if __name__ == '__main__':    main()

In our code example, we have a QtGui.QPushButton on the window. If we click on the button with a left mouse button, the 'press' message is printed to the console. By right clicking and moving the button, we perform a drag & drop operation on the button widget.

class Button(QtGui.QPushButton):      def __init__(self, title, parent):        super(Button, self).__init__(title, parent)

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(). ThemouseMoveEvent() method is the place where the drag & drop operation begins.

if event.buttons() != QtCore.Qt.RightButton:    return

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.

mimeData = QtCore.QMimeData()drag = QtGui.QDrag(self)drag.setMimeData(mimeData)drag.setHotSpot(event.pos() - self.rect().topLeft())

The QDrag object is created. The class provides support for MIME-based drag and drop data transfer.

dropAction = drag.start(QtCore.Qt.MoveAction)

The start() method of the drag object starts the drag & drop operation.

def mousePressEvent(self, e):      super(Button, self).mousePressEvent(e)        if e.button() == QtCore.Qt.LeftButton:        print 'press'

We print 'press' to the console if we left click on the button with the mouse. Notice that we callmousePressEvent() method on the parent as well. Otherwise, we would not see the button being pushed.

position = e.pos()self.button.move(position)

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.

e.setDropAction(QtCore.Qt.MoveAction)e.accept()

We specify the type of the drop action. In our case it is a move action.

转载地址:http://xxdml.baihongyu.com/

你可能感兴趣的文章
Confluence 6 导入模板的定义
查看>>
java中迭代器的原理图解和源码浅析
查看>>
EditPlus如何设置保存时不产生.bak备份文件?
查看>>
机器学习到底是什么?
查看>>
phpstorm配置svn
查看>>
用ISAPI Filter设置HttpOnly属性
查看>>
DNS域名服务器
查看>>
springmvc4环境简单搭建和定时任务
查看>>
mybatis-generator扩展教程系列 -- 自定义generatorConfig.xml参数
查看>>
基本的IPX配置
查看>>
稳扎稳打Silverlight(32) - 2.0Tip/Trick之MessageBox, Popup, 循环的几种实现方法, 动态变换主题...
查看>>
SQL Server存储过程输入参数使用表值
查看>>
SQL Injection [ Bypassing WAF (403 Forbidden) ]
查看>>
拇指接龙游戏从WIN32向Android移植过程问题记录(2)
查看>>
开源安全技术的四大好处
查看>>
LoadRunner在移动端性能测试的应用
查看>>
Hello JMX!
查看>>
轻松学习Linux之认识Shell
查看>>
Golang之interface
查看>>
SQL Server T-SQL高级查询
查看>>