I want to copy some string to clipboard in my C++ application. How can I do that? Also is it possible to copy the formatting as well? Thank You
Asked
Active
Viewed 1.2k times
4
-
4This is largely platform-dependent. – Luchian Grigore Apr 01 '13 at 10:37
-
1Check This : http://stackoverflow.com/questions/6436257/how-do-you-copy-paste-from-the-clipboard-in-c – HokaHelal Apr 01 '13 at 10:38
-
What operating system you want to do such thing ? – guanabara Apr 01 '13 at 10:39
-
1I am developing my application using Qt & it doesnt seem to have that functionality. Currently dealing with Windows, but will need for Ubuntu in future as well. – Cool_Coder Apr 01 '13 at 10:41
-
@Cool_Coder I edited your question to add the tags [Qt] and [clipboard]. – Synxis Apr 01 '13 at 11:07
-
@Cool_Coder This question I just asked might be relevant. http://stackoverflow.com/questions/40436045/in-qt-how-can-i-can-i-register-a-qstring-to-my-systems-clipboard-both-quoted#40436045 – Anon Nov 05 '16 at 09:21
1 Answers
14
You said you use Qt in one of your comments.
Qt has the class QClipboard, which is what you want (bonus: this is cross-platform).
You can put almost anything you want, data is managed via MIME types. That means you can use your own data formatting in the clipboard, with a 'custom' MIME type. Note that formatting is usually done with html text (text/html
), whereas plain text is in text/plain
(for plain text Qt provides the function text()
).

Synxis
- 9,236
- 2
- 42
- 64
-
Interesting. When you're saying it's cross-platform, I suppose it still needs to be initialized/configured to a specific platform? – SomeWittyUsername Apr 01 '13 at 11:07
-
1cross-platform because Qt is cross-platform. There still are some corner cases (ex: global mouse on X11), but for the common use it works the same on all supported platforms (which is at least Windows, Linux, Mac, and some phones). There is no manual initialization, all is done by Qt. – Synxis Apr 01 '13 at 11:10
-
I didn't mean it has to be manual and can be set by the QT itself according to environment settings but the underlying implementation surely varies. – SomeWittyUsername Apr 01 '13 at 11:17
-
1Yes, that's true. The user have nothing to initialize, but Qt have some large `#ifdef WIN ... #else ... #endif` parts in its code that do the work ;). – Synxis Apr 01 '13 at 11:19