Consider the following minimal example. How can I modify it, that it does something else, when a modifier key is pressed during the drop event as indicated in the comment in the code snipped below. Suppose you drop a file from nautilus or something like that to my example.
#!/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_())
You can, for example, set a
self.keyIsPressedstate byinstallEventFilter( QObject * filterObj )to filter theQEvent.KeyPressthat you define.This is an example using
QApplication.keyboardModifiers: