#๐Ÿ”’ Drag and drop behavior in PySide

82 messages ยท Page 1 of 1 (latest)

old nest
#

I would like to know if it's possible to get a file's path by dragging it and dropping it inside a window made with PySide6. If not, how would I got about reading its bytes (in case of images) or its contents (in case of txt).

I did not try anything yet because I'm having trouble just understanding the documentation. I saw that it maybe has something related to these (https://doc.qt.io/qtforpython-6/overviews/dnd.html) but I couldn't really understand what I'm supposed to do.

grim mothBOT
#

@old nest

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

old nest
#

any idea @halcyon mural. You helped me out a lot with other user interface things

granite sandal
#

can you not use self.setAcceptDrops(True)
then something like:

    def dragEnterEvent(self, event):
        if event.mimeData().hasFormat("text/plain"):
            event.acceptProposedAction()

    def dropEvent(self, event):
    
        textBrowser.setPlainText(event.mimeData().text())
        mimeTypeCombo.clear()
        mimeTypeCombo.addItems(event.mimeData().formats())
        event.acceptProposedAction()

based off what I see so you could do something like:

import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QTextEdit, QVBoxLayout, QWidget, QComboBox, QLabel

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Whatever name you'd prefer")
        self.setGeometry(100, 100, 500, 400)

        central_widget = QWidget()
        layout = QVBoxLayout(central_widget)

        self.text_browser = QTextEdit()
        self.text_browser.setReadOnly(True)
        self.text_browser.setPlaceholderText("Drag and drop text here")
        layout.addWidget(self.text_browser)

        layout.addWidget(QLabel("MIME Types:"))
        self.mime_type_combo = QComboBox()
        layout.addWidget(self.mime_type_combo)

        self.setCentralWidget(central_widget)

        self.setAcceptDrops(True)

    def dragEnterEvent(self, event):
        if event.mimeData().hasFormat("text/plain"):
            event.acceptProposedAction()

    def dropEvent(self, event):
        self.text_browser.setPlainText(event.mimeData().text())
        self.mime_type_combo.clear()
        self.mime_type_combo.addItems(event.mimeData().formats())
        event.acceptProposedAction()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())
#

@old nest

old nest
granite sandal
#

just tested again it does for the text?

old nest
#

here's the code

granite sandal
#

will look now just a few mins

granite sandal
old nest
#

I'm printing to the console to see what happens

#

I got it from a SO post

#

but it doesn't work

#

here

granite sandal
#

you want to display like this?

old nest
#

it doesn't even allow me to drop

#

no

#

I want to get, if possible

#

the file path of the file being dropped

#

if not

#

then store the bytes of an image, as if opening a file in 'rb' mode, or reading the contents of txt file

granite sandal
#

so to clarify something like this for output?

old nest
#

yep

#

that would be perfect

granite sandal
#

!paste

grim mothBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

granite sandal
old nest
#

reading

#

what did you add to allow the button to accept the file drop?

granite sandal
#

self.file_zone.setAcceptDrops(True)

old nest
#

mine has that

granite sandal
#

and the line right above it connects it to the main_logic method

old nest
#

it doesn't accept

#

I'm using your code

#

and it still doesn't allow me drop

old nest
#

can't drop

#

nothing is printed on terminal

#

oh

#

oh

#

nvm

#

this is dumb

granite sandal
#

what happened lol

old nest
#

so guess what

#

if I execute

#

the code from inside VSCode

#

I can't drag and drop

#

from the terminal inside VSCode

#

but if I execute from a regular terminal

#

then it works

#

how bizarre

#

even mine now works

granite sandal
#

hell yeah!

old nest
#

now

#

why the fuck it doesn't work with vscode lol

granite sandal
#

vscode terminal dont like drag and drop events

old nest
#

this seems like the sort of bug I should report, doesn't it?

#

oh

#

why not?

granite sandal
#

im honestly guessing on that when i looked it up that was what a few reddit posts said

#

although it may be something locally mine launches runs and functions might need to look into that a bit more

old nest
#

god, I hate a problem with my code

#

is not a problem with my code

granite sandal
#

from vscode that is

old nest
#

but something tangential to it

granite sandal
#

its frustrating fs fs!

old nest
#

feels like I was being set up for failure ๐Ÿ˜‚

granite sandal
#

I hate that fr fr

old nest
#

well

#

that's all I need

#

thank you so much man

#

for the help and the patience

granite sandal
#

no worries! best if luck with it all!

old nest
#

๐Ÿ‘

#

!close

grim mothBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.