If I want a quoted string in my clipboard:
qDebug() << QString("Boat\nProgramming");
I then copy the output:
"Boat\nProgramming"
If I want a non-quoted string in my clipboard:
qDebug().noquote() << QString("Boat\nProgramming");
I then copy the output:
Boat
Programming
What is the proper way in Qt to register the quoted and non-quoted strings to my [Ubuntu] system's clipboard?
Backstory / Usecase:
I have built a command line application that renders me strings that I occasionally need to dump onto a website's string interpreter (Text to speech if you care) for debugging purposes. Dumping it to the clipboard instead of manually highlighting the text, copying, and pasting, really improves my workflow.
Here is a very barebones solution that I implemented:
As suggested by Basile Starynkevitch; these are not rigorously protected functions, but barebones solutions for small strings. Temporary in my case, and used purely for debugging purposes. Please do read his post, as he provides best practices to avoid code injection, and other security risks.
"Quoted string" is very ambiguous. For example, in shell the dollar sign is special (and often should be escaped, as some other characters). But in HTML the
<
,>
,&
,'
,"
are special (and often should be escaped). In SQL statements you should only escape the double-quote and the nul character. In C, you would escape control characters and the quote and double-quote and backslash, etc... In JSON rules are slightly different.So first code the appropriate quotation transformations. Perhaps you want to implement the following functions
and so on.
(perhaps you should also want to code the reverse unquote transformations; BTW quotations might be tricky: how would you quote my full name in Russian, Cyrillic letters: Василий Дмитриевич Старынкевич in C since not all C implementations use UTF-8, even if they should)
Once you have implemented your quotation machinery (and that is perhaps harder and more ill-defined than you think!), you "just" want to copy
QString
s to the clipboard. Then read documentation of QClipboard and perhaps the chapter on drag and drop.BTW, beware of code injection (which is partly why quoting is really important). Think of some malicious
rm -rf $HOME
string etc....Actually, clipboard processing is a delicate thing with X11. See ICCCM & EWMH. You very probably need some event loop running (notably for very long strings of many millions bytes, the selection processing has then to be incremental with several handshakes, and details could be tricky, but are handled by Qt). So you might need QApplication::exec