0

I have multiple JPG files in a folder. I want to copy all of them to clipboard in one go, so when I press ctrl+v I can paste them.

Shutil will copy and past files from dict to dict:

from PyQt6 import QtCore, QtGui

for _ in range(10):
    app = QtGui.QGuiApplication([])
    data = QtCore.QMimeData()
    url = QtCore.QUrl.fromLocalFile(full_file_name)
    data.setUrls([url])
    app.clipboard().setMimeData(data)

This code will only copy one random image to the clipboard from the path I set. If I loop through all the files it crashes.

enter image description here

Jurgen Strydom
  • 3,540
  • 1
  • 23
  • 30
Akali DZ
  • 47
  • 6
  • 2
    What do you mean by "crashes"? Did it throw an error and stop? Or did it just do nothing at all? Or did it stop the program? – 12944qwerty May 05 '21 at 02:35
  • it copy the first images than say canot be a nonetype when i use if type(data) != None: it closes all the code suddenly with error 0xc0000005 – Akali DZ May 05 '21 at 02:45
  • 1
    What says cannot be a NoneType. Can you paste the entire traceback into your question? – 12944qwerty May 05 '21 at 02:46
  • i uploaded an image of the code and error – Akali DZ May 05 '21 at 03:16
  • my os : windows – Akali DZ May 05 '21 at 03:25
  • but it copy one image and i can past it but than crashes like shown in the pics ... want it to coppy all images – Akali DZ May 05 '21 at 03:26
  • @AkaliDZ The problem is not copying images but it seems that Qt has problems creating (or accessing) the native clipboard. Please use `@username`. You can provide a real [MRE] since by analyzing the image I can see that it is part of a more complex code that could be the cause of the error. – eyllanesc May 05 '21 at 03:28
  • @AkaliDZ We cannot give you any alternative if you do not provide an MRE, if you do not provide it then I will vote to close your post since it is not reproducible. – eyllanesc May 05 '21 at 03:33
  • @eyllanesc i opned a new python file and test this particular part of the code and it shows the same error . i wonder if there is a way to seperate urls and use them like this data.setUrls([url], [url2], [url3]...etc) – Akali DZ May 05 '21 at 03:35
  • @AkaliDZ Show the error message as text and match your new code as I don't believe you. – eyllanesc May 05 '21 at 03:36
  • @eyllanesc uploaded in the main topic – Akali DZ May 05 '21 at 03:37
  • @AkaliDZ move `app = QtGui.QGuiApplication([])` before for loop – eyllanesc May 05 '21 at 03:39
  • @eyllanesc it doesnt show any erros but it only keeps the last elemnt in the clipord – Akali DZ May 05 '21 at 03:41
  • @AkaliDZ Try my answer – eyllanesc May 05 '21 at 03:51
  • @eyllanesc i know that the answer is here : data.setUrls([url, url1, url2, url3]) but how can i get evry url alone from the loop – Akali DZ May 05 '21 at 03:53
  • @eyllanesc thank you very much it works like magic ^^ – Akali DZ May 05 '21 at 03:56

1 Answers1

1

The problem of the OP (shown in the image) is that he is creating several QGuiApplications and that cannot be done since it is a singleton.

The solution is to create a QGuiAplication before the for loop and then access through the static method QGuiApplication.clipboard():

import os

from PyQt6 import QtCore, QtGui

path = os.getcwd()

urls = []

it = QtCore.QDirIterator(path, ("*.jpg",), QtCore.QDir.Filters.Files)

while it.hasNext():
    url = QtCore.QUrl.fromLocalFile(it.next())
    urls.append(url)

if QtGui.QGuiApplication.instance() is None:
    app = QtGui.QGuiApplication([])

data = QtCore.QMimeData()
data.setUrls(urls)
QtGui.QGuiApplication.clipboard().setMimeData(data)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241