读Pyqt4教程,带你入门Pyqt4 _010

在PyQt4教程的这部分中,我们讨论拖放操作

拖放(Drag-and-drop)指的是图形用户界面(Graphical user interface)中,在一个虚拟的对象上按着鼠标键将之拖曳到另一个地方或另一虚拟对象之上的动作(或是支持着这样的界面的技术)。一般而言,这可以用来产生很多动作,或是在两个抽象对象当中产生各式各样的连接。

拖放操作功能是图形用户界面最明显的方面之一。拖放操作能使用户直观的做复杂的事情。

通常,我们可以拖放两类东西:数据或者一些图形对象。如果我们从一个程序拖动图片到另一个,我们实际拖动了二进制数据。如果我们在Firefox中拖动标签页到另一个地方,我们拖动了一个可视化组件。

简单拖动

在第一个例子中,有一个 QLineEdit 和一个 QPushButton 。我们将从单行编辑器中拖动纯文本到按钮上。

#!/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: December 2010
"""

import sys
from PyQt4 import QtGui

class Button(QtGui.QPushButton):

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

        self.setAcceptDrops(True)

    def dragEnterEvent(self, e):

        if e.mimeData().hasFormat('text/plain'):
            e.accept()
        else:
            e.ignore()

    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)
        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()

简单拖放操作。

class Button(QtGui.QPushButton):

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

为了把文本放到 QPushButton 窗口组件上,必须重新实现一些方法。我们创建自己的 Button 类,从 QPushButton 继承。

self.setAcceptDrops(True)

为窗口组件设置可以接受放入事件。

def dragEnterEvent(self, e):

    if e.mimeData().hasFormat('text/plain'):
        e.accept()
    else:
        e.ignore()

首先重新实现 dragEnterEvent() 方法。接受了解的数据类型,在该例子中是纯文本。

def dropEvent(self, e):
    self.setText(e.mimeData().text())

在重新实现 dropEvent() 方法中,我们定义在拖入事件中处理什么任务。这里我们修改按钮的文字。

edit = QtGui.QLineEdit('', self)
edit.setDragEnabled(True)

QLineEdit 窗口组件有内置拖动操作支持,我们需要做的是调用 setDragEnabled 方法并激它。

拖放按钮

在接下来的例子中,我们将演示如何拖放一个按钮窗口组件。

#!/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: December 2010
"""

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore


class 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):

        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)

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

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

    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(sys.argv)
    ex = Example()
    ex.show()
    app.exec_()


if __name__ == '__main__':
    main()

该例子中,在窗口上有一个 QPushButton ,如果我们用鼠标左键按下按钮,在控制台打印“press”。用右键按下并移动按钮,对按钮执行拖放操作。

class Button(QtGui.QPushButton):

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

创建一个 Button 类,从 QPushButton 继承。重新实现 QPushButton 的两个方法: mouseMoveEvent()mousePressEvent()mouseMoveEvent() 方法是拖放操作开始的地方。

mimeData = QtCore.QMimeData()

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

创建一个 QDrag 对象。

def mousePressEvent(self, e):

    QtGui.QPushButton.mousePressEvent(self, e)
    if e.button() == QtCore.Qt.LeftButton:
        print 'press'

如果按下鼠标左键,在控制台打印“press”。注意我们同样调用了父类的 mousePressEvent() 方法,否则我们将看不到按钮被按下。

position = e.pos()

self.button.move(position)

dropEvent() 方法中编写在释放鼠标并且结束拖入操作后将发生什么。找出当前鼠标点的位置并把按钮移到相应位置。

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

指定拖入动作的类型,这里是移动动作。

在PyQt4教程的这部分中,我们专注于拖放操作。

 

 

 

 

本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 
转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4579211.html

 

 

原文地址:https://www.cnblogs.com/superdo/p/4579211.html