博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ZetCode PyQt4 tutorial Drag and Drop
阅读量:6291 次
发布时间:2019-06-22

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

#!/usr/bin/python# -*- coding: utf-8 -*-"""ZetCode PyQt4 tutorialThis is a simple drag anddrop example. author: Jan Bodnarwebsite: zetcode.comlast edited: January 2015"""import sysfrom 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 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, 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()

 

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

你可能感兴趣的文章
用户登录保存数据实例(慕课笔记 使用SharedPreferences保存用户名)
查看>>
如何判断一条sql(update,delete)语句是否执行成功
查看>>
JSONObject.parseObject(jsonStr);和JSONObject.fromObject(jsonStr);
查看>>
【集训队作业2018】小Z的礼物
查看>>
ClientScriptManager与ScriptManager向客户端注册脚本的区别
查看>>
js和php中几种生成验证码的方式
查看>>
android UI进阶之仿iphone的tab效果1
查看>>
这是我的第1个C#程序(向控制台输出一句话)
查看>>
html
查看>>
Xqk.Data数据框架开发指南:丰富的、灵活的查询方法(第三部分:SqlField)
查看>>
Java基本语法
查看>>
MapReduce对交易日志进行排序的Demo(MR的二次排序)
查看>>
online-compiler 在线编译器
查看>>
9. Palindrome Number - Easy
查看>>
使用vs2017编译live555
查看>>
洛谷——P1347 排序
查看>>
uboot2009 nandflash移植
查看>>
gulp-usemin 插件使用
查看>>
int数据类型的最大数
查看>>
OI养老专题02:约瑟夫问题求幸存者
查看>>