使用修改键在pyqt4中拖放

时间:2012-12-23 16:17:37

标签: python qt qt4 pyqt pyqt4

考虑以下最小例子。如果在drop事件期间按下修饰键,如下面的代码中的注释所示,我该怎么修改它呢?假设您从nautilus或类似的东西中删除了一个文件到我的示例中。

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

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *

class MyDropLabel(QLabel):
    myDroppedSignal = pyqtSignal(str)    
    myDroppedSignalModified = pyqtSignal()    

    def __init__(self,text):
        super(MyDropLabel, self).__init__(text)
        self.setAcceptDrops(True)

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

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

    def dropEvent(self, e):
        if e.mimeData().hasUrls():
            s = e.mimeData().urls()[0].path()
            self.myDroppedSignal.emit(s)
        e.accept()

##if a modifier key, for example shift or ctrl was pressed to something else and emit myDroppedSignalModified




class MyWidget(QWidget):


    def __init__(self, parent=None):
         QWidget.__init__(self, parent)
         self.hlayout = QHBoxLayout()
         self.setLayout(self.hlayout)
         self.label = MyDropLabel("Drop something here")
         self.hlayout.addWidget(self.label)
         #self.setCentralWidget(self.label)

         self.label.myDroppedSignal.connect(self.myDroppedHandler)

    def myDroppedHandler(self,s):
        self.label.setText(QString(s))

if __name__ == "__main__":
    app = QApplication(sys.argv)
    da = MyWidget()
    da.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:1)

例如,您可以按self.keyIsPressed设置installEventFilter( QObject * filterObj )州,以过滤您定义的QEvent.KeyPress

这是使用QApplication.keyboardModifiers

的示例
#!/usr/bin/env python
#-*- coding:utf-8 -*-

import os

import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class droppableLabel(QLabel):
    fileDropped = pyqtSignal(list)

    def __init__(self, type, parent=None):
        super(droppableLabel, self).__init__(parent)
        self.setAcceptDrops(True)

    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls:
            event.accept()

        else:
            event.ignore()

    def dragMoveEvent(self, event):
        if event.mimeData().hasUrls:
            event.setDropAction(Qt.CopyAction)
            event.accept()

        else:
            event.ignore()

    def dropEvent(self, event):
        if event.mimeData().hasUrls:
            event.setDropAction(Qt.CopyAction)
            event.accept()
            links = []
            for url in event.mimeData().urls():
                links.append(str(url.toLocalFile()))

            self.fileDropped.emit(links)

        else:
            event.ignore()

class droppableWidget(QWidget):
    def __init__(self, parent=None):
        super(droppableWidget, self).__init__(parent)

        self.label = droppableLabel(self)
        self.label.fileDropped.connect(self.on_label_fileDropped)
        self.label.setText("Press CTRL/SHIFT/None and drop some files here")
        self.label.setMinimumSize(QSize(40, 100))

        self.verticalLayout = QVBoxLayout(self)
        self.verticalLayout.addWidget(self.label)
        self.verticalLayout.setMargin(0)

    @pyqtSlot(list)
    def on_label_fileDropped(self, fileNames):
        droppedFiles = [    fileName
                            for fileName in fileNames
                            if os.path.exists(fileName)
                            ]

        if droppedFiles:
            keyModifiers = QApplication.keyboardModifiers()
            if keyModifiers == Qt.ShiftModifier:
                print "SHIFT"
                formatter = "\n"                

            elif keyModifiers == Qt.ControlModifier:
                print "CTRL"
                formatter = ","

            else:
                print "NONE"
                formatter = "|"

            self.label.setText(formatter.join(droppedFiles))

if __name__ == "__main__":
    import  sys

    app = QApplication(sys.argv)
    main = droppableWidget()
    main.show()
    sys.exit(app.exec_())