linux - QClipboard 在 GNU/Linux 下工作很有趣

标签 linux pyqt4

#!/usr/bin/python
from PyQt4.QtGui import QApplication, QClipboard
import sys

app = QApplication(sys.argv)
QApplication.clipboard().setText('yo', mode=QClipboard.Clipboard)
input() #wait for input
  1. 当我设置 mode=QClipboard.Clipboard(默认设置)时,它不起作用。它将旧数据留在剪贴板和选择剪贴板中。
  2. 当我将其更改为 mode=QClipboard.Selection(特定于 X 的那个)时,它用 yo 替换了both 选择和主剪贴板>.

问题:为什么“主要的东西”(mode 毕竟默认为 QClipboard.Clipboard)不起作用,而应该只在条件下起作用的东西(QClipboard.supportsSelection()) 做这个工作?我怎样才能使它正常工作?

最佳答案

我实际上无法在我的 Linux 系统上重现该问题:它对我来说一切正常。

然而,docs说剪贴板需要 X11 上的事件循环:

the X11 clipboard is event driven, i.e. the clipboard will not function properly if the event loop is not running. Similarly, it is recommended that the contents of the clipboard are stored or retrieved in direct response to user-input events, e.g. mouse button or key presses and releases. You should not store or retrieve the clipboard contents in response to timer or non-user-input events.

因此,您可以通过强制处理这样的排队事件来让您的示例在您的系统上运行:

app.clipboard().setText('yo')
app.processEvents()
input()

显然这是一个人为的“解决方案”,不过,正确的做法是启动事件循环并遵循文档中的建议。

关于linux - QClipboard 在 GNU/Linux 下工作很有趣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30426115/

相关文章:

linux - 使用Tensorflow错误: "Resource exhausted: OOM when allocating tensor.."解决SVHN

linux - CentOS 网络服务器 : Best practice for creating SSH user that is friendly with Nginx

python - 有没有办法将按键重新分配给 Qt 中的不同功能?

java - 将在 linux 中从 windows 创建的 jar 文件作为可执行文件运行

c++ - 守护进程套接字在后台监听请求

linux - 通过从 Jenkins slave 执行的 bash 脚本向 Windows 机器发送 Ssh 命令

python - sip 模块实现了 API v11.0 到 v11.2 但 PyQt5.QtCore 模块需要 API v11.3

pyqt4 - 在 PyQt 中使用参数创建信号

python - 如何从 GUI 停止 QThread

python - QTableView 未在 dataChanged 上更新