zoukankan      html  css  js  c++  java
  • Drag & drop a button widget

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

    #!/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
    
    
    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):
          
            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.

  • 相关阅读:
    理解BSTR数据类型 神奇的BSTR
    char *
    _variant_t和_bstr_t
    数据库中创建表(包括创建主键,外键,非空列,唯一)
    使用ADO实现BLOB数据的存取 -- ADO开发实践之二
    sql server 2005 修改动态端口,连接字符串为:需要改成:IP地址+逗号+端口号才行
    Bilateral Filtering(双边滤波) for SSAO
    关于在Arduino中调用DS1302模块
    关于电机驱动扩展板 L293D 马达板Arduino
    Arduino教程资料汇总(8月22日悄悄跟新了一下)
  • 原文地址:https://www.cnblogs.com/hushaojun/p/4436931.html
Copyright © 2011-2022 走看看