Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9074507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:39:04+00:00 2026-06-16T18:39:04+00:00

Consider the following minimal example. How can I modify it, that it does something

  • 0

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_())
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T18:39:05+00:00Added an answer on June 16, 2026 at 6:39 pm

    You can, for example, set a self.keyIsPressed state by installEventFilter( QObject * filterObj ) to filter the QEvent.KeyPress that you define.

    This is an example using 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_())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following minimal example. #include <vector> class Data { std::vector<int>& v; public: Data(std::vector<int>&
Consider the following minimal example which reproduces a problem in a much bigger project:
Consider the minimal example below. It works perfectly until I uncomment the following lines:
Consider the following minimal example: #include <iostream> using namespace std; class myostream : public
Consider following code: My problem is: 1) I can't seem to cast the errors
Consider following scenario: I have RESTful URL /articles that returns list of articles user
Consider following SWT code example: http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet151.java?view=co How can I separate the inline defined class?
Consider following simple DAO example: public abstract class DAOFactory { public abstract AccountDAO getAccountDAO();
Consider following scenario during development - We change WCF service contracts very frequently. There
Consider following example: #include <iostream> #include <functional> #include <algorithm> #include <vector> #include <boost/bind.hpp> const

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.