复制到剪贴板的图像不会在 Linux 上保留

2022-01-12 00:00:00 python pyqt pyqt5 clipboard qimage

问题描述

我正在尝试将图像保存到系统剪贴板,所以我写了一些这样的代码:

I'm trying to save an image to the system clipboard, so I wrote some code like this:

#!/usr/bin/python3

from PyQt5.Qt import QApplication
from PyQt5.QtWidgets import QWidget, QPushButton
from PyQt5.Qt import QImage

import sys

class MyWidget(QWidget):
    def __init__(self):
        super(MyWidget, self).__init__()
        self.button = QPushButton(self)
        self.button.clicked.connect(self.copyPicToClip)

    def copyPicToClip(self):
        image = QImage('./test.jpg')
        QApplication.clipboard().setImage(image)
        self.close()

if __name__ == '__main__':
    a = QApplication(sys.argv)

    myW = MyWidget()
    myW.show()

    a.exec()

遗憾的是,我发现它根本不起作用.然后我试图找到解决方案.我尝试的第一件事是:

Sadly, I found it doesn't work at all. Then I tried to find a solution. The first thing I tried was this:

def copyPicToClip(self):
    image = QImage('./test.jpg')
    QApplication.clipboard().setImage(image)
    # self.close()

在这之后,我才发现它起作用了,但是窗口没有自动关闭.

After this, I just found that it worked, but the window does not close automatically.

然后我尝试复制文本:

#!/usr/bin/python3

from PyQt5.Qt import QApplication, QClipboard
from PyQt5.QtWidgets import QWidget, QPushButton
from PyQt5.Qt import QImage

import sys

class MyWidget(QWidget):
    def __init__(self):
        super(MyWidget, self).__init__()
        self.button = QPushButton(self)
        self.button.clicked.connect(self.copyPicToClip)
        QApplication.clipboard().dataChanged.connect(self.testFunc)

    def copyPicToClip(self):
        image = QImage('./test.jpg')
        QApplication.clipboard().setImage(image)

    def testFunc(self):
        print('Here')
        self.close()

if __name__ == '__main__':
    a = QApplication(sys.argv)

    myW = MyWidget()
    myW.show()

    a.exec()

很遗憾,它又失败了.

所以,如果我提前关闭应用程序,图像将不会保存到剪贴板.但我想在将图像复制到剪贴板后关闭它.

So, it seems that if I close the application to early, the image won't be saved to the clipboard. But I want to close it after copying the image to the clipboard.

有什么建议吗?

(PyQt5,ubuntu 16.10,如果有帮助的话).

(PyQt5, ubuntu 16.10, if helps).


解决方案

不幸的是,这是 Linux 上的正常"行为.默认情况下,当应用程序关闭时,剪贴板数据不会被保留.此问题的通常解决方法是安装剪贴板管理器.对于 Ubuntu,请参阅此 wiki 文章了解更多详细信息:

Unfortunately for you, this is "normal" behaviour on Linux. By default, clipboard data is not persisted when an application closes. The usual work-around for this problem is to install a clipboard manager. For Ubuntu, see this wiki article for more details:

  • Ubuntu Wiki:剪贴板持久性

(注意:我自己并没有实际测试过任何建议的解决方案,所以我不知道它们中的任何一个是否适用于 PyQt).

(NB: I have not actually tested any of the suggested solutions myself, so I don't know whether any of them will work with PyQt).

基本问题是在 Linux 上,剪贴板只存储对底层数据的引用.这在存储方面非常有效,因为只有在客户端程序实际请求数据时才会复制数据.但是当然,如​​果源应用程序关闭,引用将失效,剪贴板将变为空.

The basic problem is that on Linux, the clipboard only stores a reference to the underlying data. This is very efficient in terms of storage, because the data is only copied when the client program actually requests it. But of course if the source application closes, the reference will be invalidated, and the clipboard will become empty.

相关文章