Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt clipboard doesn't copy to system clipboard

The following snippet of code doesn't seem to affect the system clipboard at all:

clipboard = QtGui.QApplication.clipboard()
clipboard.setText(text)

According to the Qt documentation, this is how you copy text to clipboard,

Why isn't it working?

Googling turned this up.

It suggests adding this after the above code:

event = QtCore.QEvent(QtCore.QEvent.Clipboard)
app.sendEvent(clipboard, event)

But this one behaves odd: it only copies the text to the clipboard after the program exits. Plus, some people in that link reported that this doesn't work with linux.

UPDATE:

Nevermind, I was doing something wrong else where, instead of binding the copy slot to the copy button, I connected it to the "quit" button.

like image 420
hasen Avatar asked Jul 02 '09 10:07

hasen


3 Answers

Sorry for my English. I use linux. I wrote only this command

QApplication.clipboard().setText("This is text 2 clipboard")

like image 126
Sukit Tavesap Avatar answered Nov 06 '22 22:11

Sukit Tavesap


You may need to specify the mode.

This code worked for me on windows:

    cb = QtGui.QApplication.clipboard()
    cb.clear(mode=cb.Clipboard )
    cb.setText("Clipboard Text", mode=cb.Clipboard)
like image 26
Zv_oDD Avatar answered Nov 06 '22 22:11

Zv_oDD


I know you are not using Windows, but maybe this will give you some ideas... I used this in a PyQt program to copy URLs to the clipboard:

import win32clipboard

s = 'copy this to the clipboard'
try:
    win32clipboard.OpenClipboard()
    win32clipboard.EmptyClipboard()
    win32clipboard.SetClipboardText(s)
    win32clipboard.CloseClipboard()
except:
    print 'Could not copy clipboard data.'
like image 31
Jason Coon Avatar answered Nov 06 '22 22:11

Jason Coon